#GTMTips: Remember to Flush Unused Data Layer Variables

Remember to set unused variables in generic dataLayer pushes to undefined when they are not used. This way the values will not "leak" from hit to hit.

Here’s a tip that’s especially important to anyone working with a single-page application. Google Tag Manager persists items in its data model until you either manually delete the variable and/or its value from the data model, or until the user browses away from the page. There’s nothing as annoying as the example in the image below, where a value that was set for an earlier Tag is resent with a new Tag, even though the purpose was to leave it out.

Tip 26: Flush unused Data Layer Variables

Let’s extend the example in the image above. Say you have a Google Analytics Event Tag, which fires on the makeMoney Custom Event Trigger, and has the following Tag settings:

Event Category: Make Money
Event Action: {{DLV - userId}}
Event Label: {{DLV - criminalStatus}}
Event Value: {{DLV - howMuch}}

As you can see, the naming convention I use for the Variables is pretty self-explanatory. It’s just the variable name in the Data Layer prefixed by “DLV - “.

So, this Tag fires when the first payload is pushed. It gets the following values:

Event Category: Make Money
Event Action: abcb-1234
Event Label: true
Event Value: 10000

This Event signifies that a user with ID “abcb-1234” has scored 10000 dollars in a nefarious scheme, and this event is thus labelled as a criminal act. I will send this information to the proper authorities, after first taking my cut.

Next, the same person, on the same page, decides to make amends and scores a far more appropriate amount of cash in this cyber-scam:

Event Category: Make Money
Event Action: ddff-2211
Event Label: true
Event Value: 2000

But what’s that? Criminal status is still true?! But you left it out of the dataLayer.push(), why would it still be there? Shouldn’t it be empty? Is there no way for this vigilante to escape the searchlights of the police helicopter patrolling the abandoned brick factory?

You see, because there was no reload, the value of the criminalStatus variable in GTM’s data model remains true until overwritten, or until a page (re)load is executed.

This is why the following bit is important. Whenever you push a payload to dataLayer that makes a Tag Trigger, make sure you’ve accounted for any other Data Layer Variables the Tag might use. In the example we’ve used until now, if you want to make sure that criminalStatus is flushed for the second push, you would use the following syntax in the push:

dataLayer.push({
  'event' : 'makeMoney',
  'howMuch' : '2000',
  'userId' : 'ddff-2211',
  'criminalStatus' : undefined
});

This stores the value undefined for the variable criminalStatus, effectively making it so that the variable does not resolve, dropping the parameter from the Google Analytics hit entirely.

So remember to flush those unused variables!

One other thing. You might be concerned that when using Enhanced Ecommerce, this means that your impressions (which you send with the Page View Tag, right?) will be sent with every single other Enhanced Ecommerce hit on the page as well. Worry not! Enhanced Ecommerce is special. It leverages an older version of GTM’s data model, where objects are not merged together. With Enhanced Ecommerce, only the most recent hit is ever processed by your Tags.

That’s it. Go enjoy the summer!