Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 550
Sofia Khwaja Avatar asked Aug 15 '16 16:08

Sofia Khwaja


People also ask

What is dataLayer variable?

Data layer variables enable Tag Manager to read values from your data layer implementation and pass those values to tags, triggers, and other variables. A data layer object is made up of a list of key/value pairs. A key is a category of things – a book's category, title, or author. Each key could have different values.

How do I get dataLayer value?

It's just as easy. Just head on over to your console, type in “dataLayer,” navigate to your key:value pair, and follow the nested path to the key you want to identify.

What is window dataLayer?

dataLayer is a JavaScript Array, which holds data in key-value pairs. The key is a variable name in String format, and values can be any allowed JavaScript type.


1 Answers

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
like image 81
nyuen Avatar answered Oct 25 '22 19:10

nyuen