Resolving error dataLayer is not defined

Sofia Khwaja picture Sofia Khwaja · Aug 15, 2016 · Viewed 23k times · Source

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.

Answer

nyuen picture nyuen · Aug 15, 2016

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