Google Tag Manager dataLayer gets clogged with values over time that make rules and tags too complex, especially on an AJAX-heavy site.
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:
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.
Here's how I test this.
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, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With