Here we are again, revisiting an old theme. When using Google Tag Manager, we often want to send the contents of the same tag to multiple Universal Analytics properties. With on-page GA, this used to be quite simple, as all you had to do was create a new tracker and then just remember to run the ga('trackerName.send'...)
commands to all the trackers (or you could use my duplicator plugin). With GTM, your options are more limited, since Google Tag Manager abstracts the tracker object, giving you far fewer tools to work with.
Even though there are workarounds, only the very recent release of the customTask
gave us a way to do this economically with minimum risk to our existing tracking.
The Simmer Newsletter
Follow this link to subscribe to the Simmer Newsletter! Stay up-to-date with the latest content from Simo Ahava and the Simmer online course platform.
Tip 60: Use customTask to duplicate your GA hits
With this solution, you’re overriding customTask
in the GA tags that you want to distribute to multiple properties. The new customTask
modifies the Universal Analytics task queue, so that the original hit is first sent, then the payload is duplicated with a new tracking ID, and then the modified payload is sent to GA, too.
The way it works is simple. In all your Universal Analytics tags that you want to duplicate, scroll down to Fields to set, and add a new field:
Field name: customTask
Value: {{JS - customTask hit duplicator}}
The {{JS - customTask hit duplicator}} is a new, user-defined Custom JavaScript Variable that you need to create. The variable content should look like this:
function() {
// Replace newTrackingId value with the UA property to which you want to duplicate this tag
var newTrackingId = 'UA-XXXXX-Y';
var globalSendTaskName = '_' + newTrackingId + '_originalSendTask';
return function(customModel) {
window[globalSendTaskName] = window[globalSendTaskName] || customModel.get('sendHitTask');
customModel.set('sendHitTask', function(sendModel) {
var hitPayload = sendModel.get('hitPayload');
var trackingId = new RegExp(sendModel.get('trackingId'), 'gi');
window[globalSendTaskName](sendModel);
sendModel.set('hitPayload', hitPayload.replace(trackingId, newTrackingId), true);
window[globalSendTaskName](sendModel);
});
};
}
Remember to change the value of the newTrackingId
variable to contain the Property ID you want to send your duplicated hit data to!
Now, when your Universal Analytics tag with this customTask
field is run, the solution runs through the following steps:
-
First, the tag with its original settings is executed (line 10).
-
Next, the original Tracking ID in the hit payload is replaced with the
newTrackingId
(line 11). -
Finally, the modified hit payload is sent to Universal Analytics (line 12).
If you want, you can run this sendModel.set('hitPayload'...) --> window[globalSendTaskName](sendModel)
multiple times to send the data to even more Universal Analytics properties.
When you add the customTask
field with this variable to your Universal Analytics tags, these tags will automatically duplicate the entire hit payload to the new tracking ID you specified.
Because you are overwriting the sendHitTask
task, the hit payload has already been generated, which is why you need to modify it with regular expressions rather than being able to simply change values in the model
object itself. It’s not the most elegant option when you want to make other changes to the hit payload (e.g. change the index of a custom dimension), but it does its job.