Sofia Khwaja
Sofia Khwaja

Reputation: 2069

Resolving error dataLayer is not defined

I have created Google Tag Manager data layer variable and published the container. When I access the page I get the error in Chrome console

dataLayer is not defined

The datalayer push script is in the the HEAD element and the GTM code snippet is in the Body tag.

Upvotes: 20

Views: 59647

Answers (2)

Murli Pillamari
Murli Pillamari

Reputation: 1

the best way we can do for undefined datalayer error is, put it in conditions like,-

window && window.dataLayer && window.dataLayer.push({data:data});

hope this will helpful:)

Upvotes: 0

nyuen
nyuen

Reputation: 8907

If you are trying to use the dataLayer, you need to make sure it is defined before you use it:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
   'someKey': 'someValue'
})

// GTM Container here

Upvotes: 42

Related Questions