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>