#GTMtips: Add the "Event" Key to DataLayer Pushes

Quick Google Tag Manager tip for adding the event key to all dataLayer push commands.

In Google Tag Manager, every single Tag requires a Trigger to fire. Every single Trigger requires an Event condition to activate. Sometimes, these Event conditions are obfuscated under template semantics, but you can also create a Custom Event Trigger, where you specify the value of the ‘event’ key in dataLayer that fires your tag. You can read more about the relationship between GTM events and Tags in these two posts:

The key takeaway is that only an ‘event’ key push into dataLayer has the power to fire a Tag. So, the tip of this post is to (always) include the ‘event’ key when you use the push() method of dataLayer.

Tip 17: Add the ‘event’ key to your dataLayer pushes

Now, the reason I suggested to always have the key in each dataLayer.push() is because there’s no way to fire your Tags if there’s no ‘event’ key present. In Enhanced Ecommerce, for example, this is critical. Each push() into the dataLayer with an Enhanced Ecommerce payload exists only until the next Enhanced Ecommerce payload push(). If you don’t have a Tag firing with an Enhanced Ecommerce push, you will not be able to access this particular state of dataLayer later, if you’ve already managed to push another ‘ecommerce’ object. To put it into perspective, take a look at the following code:

dataLayer.push({
  'ecommerce' : {
    'impressions' : [{
      'name' : 'product1',
      'id' : '12345'
    }]
  }
});
// Some code
dataLayer.push({
  'ecommerce' : {
    'impressions' : [{
      'name': 'product2',
      'id' : '23456'
    }]
  },
  'event' : 'impressionsPushed'
});

So you might think that having a Tag fire when Event equals impressionsPushed would send both impression objects (product1 and product2) to GA, but you’re wrong. Only the second push is processed. That’s why it’s important to have an ‘event’ key in the first push as well. Or, it would be even better to combine these into a single push, but there might always be technical reasons why this isn’t possible.

So that’s key takeaway 1:

An ‘event’ key in a push ensures that you can access the state of dataLayer at the time of the push in your tags.

The second thing I want to show you might be surprising to some. You can actually add the ‘event’ key to pre-container-snippet pushes as well! This means that you can have Tags fire before the Pageview event, i.e. gtm.js.

This is because GTM processes the past states of dataLayer as well, which have been defined before the container snippet started to process the data structure.

If you take a look at the #GTMtips picture for this post, you can see an example of this. I have a dataLayer.push() before the container snippet, and it also includes the ‘event’ key with value loggedIn.

As you can see in the debug panel, this Event is processed before the Pageview event in the message bus, meaning that the execution of any Tags that have Event equals loggedIn as their Trigger would start before Tags that fire on the All Pages Trigger, for example.

So, key takeaway 2: Pre-container-snippet ‘event’ pushes can fire Tags before the Pageview Event.