Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Tag Manager dataLayer persistency

Summary

Google Tag Manager dataLayer gets clogged with values over time that make rules and tags too complex, especially on an AJAX-heavy site.


Question

I'm implementing GTM on a tracking-heavy site and looking for a clean solution to the following issue. I'll take Google Analytics as en example.

It lets you set and use 4 macros for event tracking:

  • Category {{ga event category}}
  • Action {{ga event action}}
  • Label {{ga event label}}
  • Value {{ga event value}}

Value and label are optional, but dataLayer is persistent, isn't it? So how do I handle the following scenario:

dataLayer.push({
  'event':'gaEvent', 
  'gaEventCategory':'my-category', 
  'gaEventAction':'my-action',
  'gaEventLabel':'my-label',
  'gaEventValue':'my-value'
});

But then on the same page later I have to track another event, but one without Label and Value:

dataLayer.push({
  'event':'gaEvent', 
  'gaEventCategory':'another-category', 
  'gaEventAction':'another-action',
});

If I set up a rule like "Event is gaEvent" and on that I fire off the "Google Analytics" tag that looks like the following (using HTML tag here instead of built-in one):

<script type="text/javascript">
  _gaq.push(['_trackEvent', '{{ga event category}}', '{{ga event action}}', '{{ga event label}}',  {{ga event value}}]);
</script>

The second event would be pushed to _gaq with previous event's label and value.

How to handle cases like these?

For those wondering, I need to be able to fire off different tags on the same event at certain points, that's why I want this "dynamic" solution, but dataLayer being persistent like that would screw up the Rules.

Edit 1: testing routine

Here's how I test this.

  • I have Google Analytics Debugger installed in Chrome
  • I have GTM in debug mode
  • Once the site is loaded, in the console I do the first dataLayer push from description
  • I see from GA Debug output that the GA event is fired with the 4 values
  • I do the second push from the console
  • GA Debug output tells me that the GA event is fired with new category and action, but with previously pushed label and value

That's the persistency I speak of. To circumvent this I could only come up with different event types like gaCustomEvent, gaCustomEventWithLabel, gaCustomEventWithOption etc, and then rules and tags for each one of those. That's absurd, don't you agree?

like image 628
Alexander Filatov Avatar asked Mar 23 '23 07:03

Alexander Filatov


1 Answers

You can push an object into the dataLayer containing all of the information relevant to the individual event, e.g.:

dataLayer.push({
    'gaEvent':{
        'category': 'foo',
        'action': 'bar',
        'label': 'blah blah blah',
        'value': 1,
        'nonInteraction':false
    }
});

You can then set up the relevant macros in GTM to collect & use the relevant fields, which will not be carried over to any future events as only the last value of an object pushed into the dataLayer is used.

like image 53
Duncan Avatar answered Apr 05 '23 00:04

Duncan