google-tag-manager

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.


Solution

  • 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