Max Max - 11 months ago 53
Javascript Question

sendHitTask not working with hits sent via GTM Universal Analytics tag template

I have implemented the Google Analytics

tracker method in order to push a copy of whatever hits (pageviews, events...) Google Analytics sends to itself to an array I can then inspect and use for debugging:

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-XXXXXXX-1', 'auto');

ga(function(tracker) {
var originalSendHitTask = tracker.get('sendHitTask');
tracker.set('sendHitTask', function(model) {
originalSendHitTask(model); // Send to GA as usual
window.gaDebugging.push(model); // Push copy to debugging array

ga('send', 'pageview');

This is working for:

  • Hits I push via the code (e.g.
    <button onclick="ga('send', 'event', 'Test'...

  • Hits I send via GTM with a
    Custom HTML Tag
    <script>ga('send', 'event', 'Test'...</script>

However this is NOT working for:

  • Hits I send via GTM with the built-in Universal Analytics templates (
    [1] Choose Products -> Universal Analytics

Any idea how I can track hits sent by GTM via the built-in Universal Analytics template tags and push those hits to a JavaScript array?

Answer Source

When you use the ga(function(tracker) { ... }) syntax, the function uses the default tracker created by the ga('create', 'UA-XXXXX-Y') command as its parameter.

In other words, any operations you perform within this callback will only apply to hits that are sent with this default tracker.

Google Tag Manager does not use the default tracker when you create a tag with a tag template. Instead, it gives each injected tag a unique tracker name. This is partly because GTM doesn't really support (yet) the distinction between fields you set on the tracker vs. fields you set on the tag.

To apply your sendHitTask operation on the tags created with GTM's tag templates, you will need to rename the tracker these templates use. You can find this setting under Advanced Configuration > Set Tracker Name. Just leave the field blank (make sure you've checked the checkbox). Now the tag template with this setting will use the default tracker name, and any operations you perform on the tracker object in ga(function(tracker) { ... }) will apply to the GTM tag templates as well.

HOWEVER! This is risky, since as I wrote above, GTM doesn't have separate fields for the tracker vs. the single tag. In other words, any Fields to Set and Custom Dimensions/Metrics you add for one tag template will now apply to all hits that share the tracker name. This is especially problematic if you want to send hit-scoped custom dimensions, or set fields for a single hit alone.

Sadly, there really is no proper workaround, except to nullify each tracker field between each hit, but that's a lot of overhead. One thing you could do is use only Custom HTML Tags to work with Google Analytics hits, using the inline ga('send'...)? syntax, but that really deflates the reason of using GTM in the first place.