האם ניתן איכשהו להעביר את אובייקט ה-json הראשוני של postStream לשכבת הנתונים של GTM?

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: Set up Google Tag Manager for use 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
2 לייקים