简体   繁体   中英

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?

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

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.

I believe you issue is with using _gaq.push in a Custom HTML tag and the rule {{event}} equals gaEvent :

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

So when you're doing the dataLayer.push, the rule is getting fired and thus causing the Custom HTML tag to send the _gaq.push tracker to GA. When you go do the second dataLayer.push, the rule is firing again, but sense it doesn't have a value for neither label or value, it is using the previous values for both label and value for that dataLayer.push.

What I would do is this:

Setup a new macro called events - this is a Google Analytics tag with the track type being event. Next put in your macros for the event tracking parameters, and set the tag to the rule that your currently using for gaEvent ( {{event}} equals gaEvent ).

在此输入图像描述

By handling the event this way, you'll be using GTM's method of sending events to GA, and you'll prevent yourself from sending along additional information such as labels, values, etc.

Each dataLayer.push() is granular, independent and triggers tags with matching firing rules. If you want your 2nd event to inherit dataLayer variables from the first, it's probably best handled in your application and not GTM.

Another option is to use a different event name on the 2nd push and use macros for dataLayer variable defaults. You can configure your GA event tracking tag to listen on multiple firing rules. It's an any, so either will trigger the _gaq.push() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM