È possibile in qualche modo passare l’oggetto stream del post iniziale per le pagine degli argomenti al layer di dati GTM senza effettuare un’altra richiesta AJAX come faccio attualmente con questo workaround poco elegante?
<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>