Google Tag Manager dataLayer variables are undefined

Helge picture Helge · Sep 4, 2017 · Viewed 7.2k times · Source

When my page is displayed, I push some data to Google Tag Manager (GTM) dataLayer:

dataLayer.push({
    event: "MY_EXPERIMENT", 
    variant: "A",
    result: "FORM_DISPLAYED"
});

When the form is submitted, I push some new data:

dataLayer.push({
    event: "MY_EXPERIMENT", 
    variant: "A",
    result: "FORM_SUBMITTED"
});

I've created dataLayer variables in GTM, and I've inserted those in the category/action/label fields of my tag in GTM. One of the variables is setup like this:

The tag is triggered by URL path, and in preview mode it works.

However, when I look the event up in the live dashboard of Google Analytics, only "undefined" is displayed in the category/label/event columns. It seems to me the variables are not set up correctly, but I've looked through everything and it seems just fine.

What am I doing wrong?

Answer

faridghar picture faridghar · Sep 5, 2017

It's hard to tell without any screenshots or further details but what's most likely happening is that your tags are firing too early. You said that the GA event tag is triggered based on a URL path. This most likely means that you're using the All Pages trigger in GTM with an optional filter to only fire on specific pages. However, you're most likely only executing those dataLayer pushes after the GTM container code. This means that at the time of tag firing, those dataLayer pushes haven't executed yet and that's why you're getting undefined in the values of the variables.

To fix this you should change the trigger of your GA event tag from All Pages to a Custom Event. In your case, the custom event name would be MY_EXPERIMENT.