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?
gaEvent
prefix, makinggaEventCategory
justcategory
. If you have 50 dataLayer pushes on your page, you just saved 1.4kb in redundant characters. – Mike Causer