Created
April 25, 2019 14:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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