Fire a single analytics tag multiple times redux

Simo Ahva wrote a great post a couple of months ago in which he mentioned a way to trigger the same analytics tag multiple times

The approach is an awesome clean way of reusing a single universal analytics tag and its hitCallback feature to send data to multiple tracking ids. I recently needed to use this method and discovered a few kinks to the original approach, which cause it to only work once per pageload and only for one tag at a time.

tldr; checkout the way to use same hitCallback multiple times per page here.

Checkout the post and see if you can spot it (while you are at it read up on the other tricks outlined too :)).

The drawbacks I found:1- The method can only be used to trigger one tag multiple times per pageload. This is because the counter used is being set in the global scope and once it iterates its not reset. So you can only use it once. This is really on gtm since there is no other way to pass state in a good dynamic way.

2- The approach can only be used for one tag at a time. Since the rule to trigger is defined as event equals tagCallback only one tag can have this rule otherwise for every hitCallback all tags will be triggered.

Both of these are easy enough to fix (though it took me a lot longer 🙁 hitCallback refusing to work when I was in debug mode, which is story for another time).

Here is what we need


GTM tag callback function.png

1- tag callback function

    return function(){        
        var maxRepeat = 3;
        window.gtm_tag_counter = window.gtm_tag_counter || 0;
        if("{{event}}" != "tagCallback"){
            dataLayer.push({'chainStarter': '{{event}}'});
        if ( window.gtm_tag_counter < maxRepeat-1 ){
            dataLayer.push({'event' : 'tagCallback'});
            window.gtm_tag_counter += 1;
        else {
            // delete gtm_tag_counter from global scope
            delete gtm_tag_counter;
            dataLayer.push({'event' : 'allCalledOut'}); 
            // do something once all tags are done

In this code here is whats been changed. I have added an if statement that checks if the hitCallback is being triggered by the event tagCallback if it isn’t we store the event that got the ball rolling.

Finally after the tag counter is maxed out I have added an else statement to delete the counter so its back to default value and also push an event that tells you the entire iteration is done and you are free to do any other actions.

GTM chain starter.png

2- tag callback chain starter

This is a macro to keep track of event that started the chain its used to make sure only specific tags associated with the starting event are triggered.

GTM tag callback counter.png

3- tag callback counter

GTM tracker id.png

4- tracker id

GTM tracker name.png

5- tracker name

In the analytics tags itself we need to:

GTM hitCallback.png

1- set hitCallback

setting this as a field is where the magic happens.

GTM multiple tracker rule.png

2- Rules

Because we want to be specific we need to create a new rule for each tag that needs to use the callback approach, we define on which particular tagCallback events the tag should be triggered since this is pageload we want to trigger it when the chain starter (original event that called the callback) was gtm.js and event is tagCallback

So for any tag this would be
{{tag callback chain starter}}equalsoriginalEvent{{event}}equalstagCallback

GTM rules multiple tracker.png

Here is what it looks like

GTM tag tracker id.png

3- set tracker id

set the UA code of the property where you’d want to send the data.

GTM tag tracker name.png

4- set tracker name

This is optional gtm creates new trackers each time a tag is initiated but you can use it to control what these are named.

And there we have it reusing the same tag with multiple trackers while reusing the same callback with multiple tags. Though its slightly more tricky with us requiring to add a new rule each time its more robust.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.