Imagine the following situation: you want to migrate a website’s tracking code form Google Analytics – _gaq.push()
– to Universal Analytics – ga('send')
. And while you’re at it, you also want to implement Google Tag Manager to run the new code. After a quick scan of the website, you notice that it has a lot of hardcoded events. You start thinking about the development hours it would take to change all of them. After discussing it with the client, you’d wish there was another way.
There is. Here’s how:
Firing GTM events with Google Analytics’ _gaq.push() tag###
If you want to migrate to Universal Analytics, you’ll have to change the code. Basically all _gaq.push()
function calls have to be changed to ga('send')
. Some tags are easy to move to GTM. For example basic page tracking, you only have to change the tag in GTM from Classic tracking, to Universal tracking:
Once you’ve done this, you’re all set as far as basic page tracking goes. If however, you have _gaq.push()
tags left on your page, there’s an issue. If one of these events or pageviews is triggered after this change, the developer console will show an error:
The best way to fix this, is by changing all onsite event code to new GTM events. But if there are a lot of hardcoded events, this will take a lot of time.
So to make life easy for you, here’s a fix that automatically captures the hardcoded _gaq.push()
events and sends them to GTM:
//check if _gaq is not available as it should be when traditional analytics is running
if (typeof _gaq === 'undefined' || typeof _gaq !== 'object') {
//define _gaq yourself
var _gaq = {};
//add the push() function to the object
_gaq.push = function(array){
//check the first item in the array
//check type of push
if(array[0] === '_trackPageview'){
//fire GTM virtual pageview
}else if(array[0] === '_trackEvent'){
//fire GTM event
}
}
}
This code basically checks if you’re not running traditional Google Analytics. If so, it translates all the function calls to a GTM call. Keep in mind that the values that you have to pass into your GTM functions are part of an array, because the standard GA trackers used an array for input.
To clarify, here’s a full exmaple with a sample pageview and event code for GTM:
<script type="text/javascript">
if (typeof _gaq === 'undefined' || typeof _gaq !== 'object') {
var _gaq = {};
_gaq.push = function(array){
if(array[0] === '_trackPageview'){
dataLayer.push({
'event': 'pageview',
'pagePath': array[1]
});
}else if(array[0] === '_trackEvent'){
dataLayer.push({
'event': 'event',
'category': array[1],
'action': array[2],
'label': array[3],
'value': array[4]
});
}
}
}
</script>
Now all you have to do is add this snippit as a custom HTML tag on all pages via GTM, and you’re all set. Don’t forget to change the pageview and event GTM functions to the match the structure you’ve set up for GTM (with triggers and variables).
In this post I’ve focused on Google Tag Manager, but the snippet works for other tag managers as well. If you can configure a custom HTML tag, and have a function available to send events and pageviews to the tag manager, you’re able to use it.
Leave a Reply
You must be logged in to post a comment.