Reputation: 2069
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
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
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