Is it somehow possible to pass the initial postStream json object into the GTM datalayer?

AI-generated summary

The discussion revolves around passing the initial post stream JSON object to the Google Tag Manager (GTM) data layer without making an additional AJAX request. Terrapop is currently using a workaround and is seeking a better solution. simon suggests using an approach outlined in a Meta Discourse post to catch specific events, such as post:created or topic:created, to send associated data to the GTM data layer.

Is it somehow possible to pass the initial post stream object for topic pages to the GTM data layer without doing another ajax request like I do right now as a bad workaround?

<script type="text/discourse-plugin" version="0.8">
const currentUser = api.getCurrentUser();
let userId;

if (currentUser) {
    userId = currentUser.id;
}
else
{
    userId = 0;
}

var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status === 200) {
        callback(null, xhr.response);
      } else {
        callback(status, xhr.response);
      }
    };
    xhr.send();
};

api.onPageChange((url, title) => {
    if (url.match(/^\/t\//)) {

	    getJSON(url + '.json',
        function(err, data) {
          if (err !== null) {
              
            window.dataLayer.push({
                'event': 'pageChangeData',
                'userId': userId,
                'PageType': 'topic',
                'PageJson': false
            });
            
          } else {
        
            window.dataLayer.push({
                'event': 'pageChangeData',
                'userId': userId,
                'PageType': 'topic',
                'PageJson': data
            });
          }
          
        });
	    
	} else {
	    
	        window.dataLayer.push({
                'event': 'pageChangeData',
                'userId': userId,
                'PageType': 'notopic',
                'PageJson': false
            });
	}

});
</script>

If you’re trying to send the data that is associated with a Discourse event, you might be able to use the approach that’s outlined in this post: Integrate Google Tag Manager with Google Analytics - #44.

It should work if you’re trying to catch any of the following events:

  • post:created
  • topic-notifications-button:changed
  • topic:created
  • page:bookmark-post-toggled
  • page:like-toggled