Last active
May 20, 2016 20:46
-
-
Save alexwilson/7571b100ffb9bb3eafed312ec2d29679 to your computer and use it in GitHub Desktop.
ga.js-style wrapper for compatibility with Google Tag Manager
This file contains 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
(function() { | |
var _prefix = 'ga'; | |
// Ensure we have a GTM Data Layer to push to. | |
window['dataLayer'] = window['dataLayer'] || {}; | |
// Instead of overdeclaring _gaq, only attempt to polyfill if it doesn't already exist. | |
if (window['_gaq'] === 'undefined '|| typeof window['_gaq'] !== 'object') { | |
window['_gaq'] = {}; | |
window['_gaq'].push = function(data) { | |
switch (data[0]) { | |
// Pass current page path for page view. | |
case '_trackPageView': | |
window['dataLayer'].push({ | |
'event': _prefix+'pageview', | |
'path': data[1] | |
}); | |
break; | |
// Events are natively handled in GTM. | |
case '_trackEvent': | |
window['dataLayer'].push({ | |
'event': _prefix+'event', | |
'category': data[1], | |
'action': array[2], | |
'label': array[3], | |
'value': array[4] | |
}); | |
break; | |
case '_setCustomVar': | |
window['dataLayer'].push({ | |
'event' : _prefix+'variable', | |
'index' : data[1], | |
'name' : data[2], | |
'value' : data[3], | |
'scope' : data[4], | |
}); | |
break; | |
// Default to storing in the GTM Data Layer. | |
default: | |
window['dataLayer'].push(data); | |
break; | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment