Skip to content

Instantly share code, notes, and snippets.

@jeandcr
Created April 25, 2019 14:49
Show Gist options
  • Select an option

  • Save jeandcr/3b1c34233ff63455b2af65a52d98e449 to your computer and use it in GitHub Desktop.

Select an option

Save jeandcr/3b1c34233ff63455b2af65a52d98e449 to your computer and use it in GitHub Desktop.
A way to listen to pageview events from the ga js object through GTM
if(window.ga) {
window.ga(function (tracker) {
// Grab a reference to the default sendHitTask function.
var originalSendHitTask = tracker.get('sendHitTask');
// Modifies sendHitTask to send a copy of the request to a local server after
// sending the normal request to www.google-analytics.com/collect.
tracker.set('sendHitTask', function (model) {
originalSendHitTask(model);
var p = model.get('hitPayload');
var a = p.split('&');
var b = {};
a.forEach(function (e) {
b[e.split('=').shift()] = e.split('=').pop()
});
window.dataLayer.push({
event: 'ga_' + b.t, // hit type
dh: b.dh, // pagehost
dp: b.dp, // url set in pageview event
dl: b.dl, // page location
})
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment