Google Tag Manager Data Layer Tutorial with examples

Index

#1 What is a data layer? #13 What are dynamic data layer variables?
#2 Understanding JavaScript arrays to create data layers #14 How you can rename a data layer (or data layer object)
#3 Understanding Javascript objects to create data layers #15 When do you need to rename your data layer?
#4 Why do you need a data layer? #16 What is a data layer snippet?
#5 Data layer vs DOM scraping #17 What is a container tag snippet?
#6 What are data layer variables? #18 Where should the data layer snippet be placed?
#7 How do you initialize a data layer? #19 Why the data layer snippet must be placed above the container snippet
#8 How information is pushed into a data layer? #20 Do you need a container tag to implement a data layer?
#9 What should be the format of the information you push into a data layer? #21 How do you implement a data layer on a web page?
#10 How you can overwrite the value of existing data layer variable? #22 What information you should store in a data layer?
#11 Naming conventions for data layer variables #23 How to see and interpret the data layer on a web page?
#12 What is an event data layer variable?

If you have used Google Tag Manager (GTM) for longer than a few days, you will know that GTM does require good knowledge of HTML, DOM and JavaScript as prerequisite. If you have implemented Ecommerce Tracking via GTM or enhanced ecommerce tracking via GTM, you know that you need the help of a web developer to create data layer for you. If you have used GTM long enough, you know that the real power of GTM lies in the usage of variables especially custom variables which require coding knowledge.

GTM is a really powerful tool and understanding the data layer is the key to getting the most out of Google Tag Manager.

If you are brand new to GTM, then read this article first: Beginners guide to Google Tag Manager

What is a data layer?

A data layer is a JavaScript array which is used to store information and send information to the Google Tag Manager container.

Example of an empty data layer:

Example of a data layer which contain variables:

In order to understand the concept of a data layer, you first need to understand JavaScript arrays and objects.

A data layer is also known as dataLayer queue, dataLayer object and data collection layer.

Understanding JavaScript arrays to create data layers

An array is a special variable which can store multiple elements of same or different data types at a time.

Variable is a location in the computer memory which is used for storing data.  A variable can store different data at different times. For example if ‘a’ is the name of a variable, then ‘a’ can be used to store a value like ’10’ and then at different point in time, can be used to store another value like ’20’.

However a variable can store only one value at a time. If you want a variable to store more than one value at a time, then you need to use a special variable called ‘array‘.

An ‘array’ is made up of one or more elements. These elements can be: strings (like ‘hello’), numeric values (like 10), undefined values, boolean values (like true, false), other arrays or objects. An array can be created, by either using the array function or array literal notation []. The array literal notation is preffered, for creating arrays.

For example, here is how the empty array created through array function will look like in JavaScript:

Here ‘a’ is the name of the array and ‘var’ is a command which is used to create a variable in JavaScript.

You can give any name to your array. For example:

You can also create new array variable through array literal notation [] like this:

Google tag manager uses the array variable name called ‘dataLayer’.

So if you want to create an empty data layer array, it would be like this:

Since this line of code is a JavaScript statement, you need to enclose it within the <script> and </script> tags, so that you can embed the code into your HTML document. So the final setup will look like the one below:

Congratulations, you just created your first data layer.

Now, let us create a new array variable called ‘a’, which has got three elements;

Here the array variable named ‘a’ has got three elements named: ‘hi’,’bye’ and ‘good bye’.

All of these three elements are of type ‘string’ as they are string values. We call such array as array of strings.

So you just created your first array of strings.

Similarly, we can create a new array variable, which has got three numeric values;

Here, all of these three elements have got numeric values. We call such array as array of numbers.

So you just created your first array of numbers.

You can also create an array variable which contains elements of different data types. For example, you can create an array which contains both string and number values:

Here, the array variable named ‘a1’ is made up of six elements. Out of these six elements, the first three elements are of type string and the last 3 elements are of type ‘number’.

You can also create an array, like the one below:

Here, the array variable named ‘a2’ is made up of 6 elements. Out of these six elements, three elements are of type string and the remaining three elements are of type ‘number’.

Note: Even when both a1 and a2 contain same elements, they are still different array variables, because the order of the elements is different.

Other array examples:

Following is an example of an array variable which contains only one object:

Following is an example of an array variable which contains two objects:

White spaces and line breaks are not important. So to improve readability you can write the same ‘a’ array as:

Following is an example of an array variable which contains multiple objects:

You can also create array which contains elements of all available data types:

“size” is an array element of type string.

10 is an array element of type number.

true is an array element of type boolean.

,, (empty value) is an array element of type undefined.

[“Clasicos”,57] is an array element of type array.

{“id”:”20″,”name”:”Zapatos”} is an array element of type object.

White spaces and line breaks are not important. So to improve readability you can write the same ‘a’ array as:

Since dataLayer is javascript array, you can create a data layer like the one below:

Congratulations, you just created your second data layer which is made up of five elements.

Note: The data layer used for GTM is usually an array of a single object: dataLayer = [{……}];

Understanding Javascript objects to create data layers

Javascript object is a variable which has got properties and methods. Following is the syntax to create a JavaScript object:

or

Here property can be any string or identifier. An identifier is a reserved word, which carry special meaning and can not be used as a variable name. For example ‘event’ is an identifier. A property’s value can be:

  1. string
  2. numeric value
  3. undefined value
  4. boolean value
  5. array
  6. multi dimensional array
  7. object
  8. array of objects.

The following example creates an empty object with no properties.

Here name of the JavaScript object is ‘a’. You can give any name to the JavaScript object.

The following example creates an object called “user” and adds 4 properties to it:

Since white spaces and line breaks are not important in JavaScript. So to improve readability you can write the same ‘user’ object as:

Other examples of JavaScript objects:

Here ‘color’ is the property of the object ‘a’ and ‘Caf\u00e9’ is the value of the ‘color’ property which is a string.

Here ‘price’ is the property of the object ‘a’ and ‘164900.00’ is the value of the ‘price’ property which is a numerical value.

Here the object ‘a’ has got two properties ‘color’ and ‘price’.

Similarly we can define an object with multiple properties:

Since data layer is a JavaScript array and an array can contain one or more objects, we can have a data layer like the one below:

Congratulations, you have just created your first complex data layer. 

The data layer can become even more complicated when it contains server side script. For example, here is how a data layer, with server side script may look like:

datalayer with server side script

Remember:

a=[]; // creates an array

a={}; // creates an object

a=[{}]; // creates an array of a single object

dataLayer in GTM is an array of a single object. So,

dataLayer = [{…}];

Why do you need a data layer?

In the context of Google Tag Manager, data layers are used to collect data from a website. GTM (aka container tag) can also directly pull data from the HTML of a web page by traversing the HTML DOM (structure of HTML) using JavaScript:

container tag data layer

By default a container tag pulls data from the HTML of a web page and push it into your analytics and marketing tags. Now the problem with this approach is that, the structure of HTML (HTML DOM) can change any time without your knowledge, as commercial websites update all the time and as a result, any tag can stop working any day, any time without any prior notice.

To fix the problem, we create and use data layers, which stores all the information we want to collect about a web page. Once you set up a data layer, the container tag pulls data from the data layer of the page instead of its HTML So no matter what happens to HTML DOM, the data layer will remain the same and your tags will continue to work (unless of course, someone/ something breaks the data layer itself).

Data layer vs DOM scraping

Each tag management solution (TMS) provider implement data layer in a slightly different way. So most likely, you won’t be able to use the same data layer with every (TMS) (if you use more than one TMS). Data layers should be hard coded on a website by your developer or IT team. So as long as you are using data layers, you may need to depend upon IT for its updates and maintenance.

DOM scraping is a technique used to pull data directly from the HTML elements (buttons, links, forms etc) of a web page by using JavaScript and the knowledge of HTML DOM.

Almost all TMS vendors provide the functionality of scraping the HTML DOM.

DOM scraping comes handy when it is not possible to implement the required data layers in a reasonable amount of time, in order to set up certain tracking. Through DOM scraping you can implement TMS solutions faster, as there is little to no dependence on IT and you have more control over the implementation. However TMS solution implemented through DOM scraping is not a stable solution, as changes in the HTML DOM (like change in the ID of a HTML element) can quickly break the TMS solution. So TMS solution deployment through DOM scraping is more of a quick fix when you lack development resources or time. It is not something, you can/should rely upon for a long period of time and/or where lot of money is at stake (like airline websites).

What are data layer variables?

These are the variables used in data layer. It is in the format: ‘Property:value’.

Following is an example of data layer which contains no variable:

Following is an example of data layer which contains one variable:

Following is an example of data layer which contains two variables:

The same data layer can be re-written as:

Following is an example of data layer which contains three variables:

 

Here, PageCategory, visitorType and event are data layer variables.

As you can see they are basically properties of an object.

Statisitcs’, ‘High Value’ and ‘customizeCart’ are the values of PageCategory, visitorType and event data layer variables.

Note: Data layer variables are also known as data layer messages.

How you can initialise a data layer?

By setting up variables in your data layer.

Initialise a data layer with one variable:

Initialise a data layer with two variables:

Initialise a data layer with three variables:

 

Appending/passing messages onto the data layer queue => Adding data layer variables to the data layer.

Updating data layer => adding/updating/removing data layer variables.

Pushing information into the data layer => adding data layer variables to the data layer.

How information is pushed into a data layer?

If the data layer has been hard coded on a web page, then certain piece of information is already available in the data layer.  The other information can be pushed dynamically (during run time) through the ‘push’ method of dataLayer object.

Through the ‘push’ method of dataLayer object, you can dynamically add object(s) to your data layer. The pushed object can contain one or more data layer variables. Following is the syntax of the ‘push’ method:

 

For example, let us suppose following data layer is hard coded on a web page:

 

Now this data layer already contains one variable.

If you used the following push method:

 

The hard coded data layer will now look like this:

 

You can also push/add multiple variables at once, into the data layer. For example:

 

What should be the format of the information you push into a data layer?

The information that you push into a data layer, from either front-end or back-end must be in the format, GTM can understand. For example,if you try to push ecommerce data layer variables into a data layer which are not recognised/recommended by GTM, then your ecommerce tracking won’t work.

 

Here GTM won’t be able to recognize ‘Transaction Id’ as ecommerce data layer variable.

The correct data layer snippet would be:

Note: ‘transactionId’ is an example of an identifier (reserved word). So it carries a special meaning and it can not be used as a regular variable name, while using GTM.

How you can overwrite the value of existing data layer variable?

By pushing a variable of the same name as an existing variable, but with a new value to the data layer.

Let us suppose this is your existing data layer:

Now if you used the push method, like the one below:

The value of pageCategory data layer variable would be overwritten and now your data layer, would like the one below:

 

Naming conventions for data layer variables

You should use a consistent naming convention for data layer variables. If you use different variable names for the same variable on different pages, GTM may not be able to fire tags in all the desired locations. For example,

If you set up the page category on the ‘signup’ page by using the ‘pageCategory’ data layer variable, then page category on the product purchase page should also be setup by using the ‘pageCategory’ data layer variable and not by using data layer variable like: Pagecategory or PageCategory. This is because data layer variable names are case sensitive.

So the following set up won’t work the way you think it should:

But the following set up will work the way it should:

 

Data layer Variable names should be enclosed in quotes

 

The Data Layer javascript array itself is case-sensitive

So, dataLayer is different from datalayer, and using the latter won’t work.

For example:

 

What is an event data layer variable?

It is a special data layer variable that is used with JavaScript event listeners.

It is used to fire a tag, when a user interacts with webpage elements such as: form, button, links etc.

Syntax for setting up an event:

Here ‘event’ and ‘event name’ are strings.

For example, following code update the data layer’s ‘event’ variable when a user clicks on the button:

Other examples of special data layer variables

  • hitType
  • nonInteraction
  • ecommerce etc

Q. Can each web page has its own unique data layer?

A. Yes and it should.

Q. Do you need to put the same set of variables in the data layer on every page?

A. No.

Q. When a data layer is updated in case of a tracked event?

A. When the tracked event occurs like: mouse click, form submission, page load etc.

What are dynamic data layer variables?

Dynamic data layer variables are data layer variables which are pushed dynamically (during run time) to the data layer.

Syntax for setting up dynamic data layer variables

For example let us suppose following data layer is hard coded on a web page:

If you used the following push method:

The hard coded data layer will now look like the one below:

Here ‘pageCategory’ is a data layer variable whereas ‘visitorType’ is a dynamic data layer variable, as it has been pushed dynamically into the data layer.

Note: Dynamic data layer variables are not hard coded on a web page.

How you can rename a data layer (or data layer object)?

The default name of data layer (or data layer object) is ‘dataLayer’.

By making a small change in your container code snippet you can rename your data layer:

For example:

 

When you need to rename your data layer?

When you are using multiple container codes on a same web page and you don’t want each container to use same data layer.

For example:

Note: By default, each container will use a common data layer.

 

Q. Do you need to be a web developer in order to use data layer?

A. I am often asked this question. The answer is “No”, not for the very basic data layers. But you still need a basic knowledge of HTML, DOM and JavaScript.

Q. When do you really need a web developer to write data layers for you?

A. When you have to create complex data layers, like the one which pull ecommerce data from the back-end (server side) and convert that data into a format, which GTM can understand.

What is a data layer snippet?

A data layer snippet is the data layer enclosed within <script> ….</script> tags.

Following is an example of a data layer snippet

 

What is a container tag snippet?

It is the code which is used to create a new container tag. A container tag holds all the marketing/analytics tags, triggers and variables for your website. When you are installing Google Tag Manager on your website, you are basically installing a container tag.

Following is an example of container tag snippet:

Note: The container tag snippet should be placed on each and every web page of your website.

Where should the data layer snippet be placed?

It should always be placed above your container tag snippet in the head section (<head>….</head>) of a web page:

For example:

 

Why should the data layer snippet be placed above the container snippet?

If you place the data layer snippet below the container tag snippet then two things can happen:

  1. Your GTM container tag may stop firing
  2. Data layer variables are no longer available to the container tag.

 

For example following set up may not work:

Here the data layer variables ‘pageCategory’ and ‘visitorType’ may not be available to the container, as the data layer code has been placed after the container snippet.

 

Q. When does Google Tag Manager function best?

A. When used alongside a data layer. You should know this by now!

Q. Is it mandatory to declare data layer while using GTM?

A. No. Explicitly declaring a data layer is optional.

Q. Can you use GTM without explicitly implementing data layer?

A. Yes. In that case GTM container code will automatically create its own empty data layer object for you.

Do you need a container tag to implement a data layer?

No. You can create a javascript array which store all the information you want to collect about a page and use it as a data layer.

For example:

Q. How you can selectively fire tags on page load?

A. Through GTM triggers.

Q. What is the downside of not using data layer?

A. You cannot use events without data layer and above all your whole GTM implementation will be pretty shaky.

Q. How you can access values from a page without implementing data layer?

A. By using GTM custom JavaScript variables.

How you can implement data layer on a web page?

by adding following snippet of code above the container tag snippet in the head section <head>….</head>:

For example, you might want to set data layer variables to indicate that the page is a ‘Maths’ page and that the visitor is a ‘high-value’ visitor. To do this, you initialise the data layer as follows:

 

Q. What is the scope of data layer variables?

A. Data layer variables are page specific. That means they exist, as long as a user remains on the same page.

Q. How you can declare data layer variables that are relevant across pages?

A. By using the same data layer variable name on multiple pages of your website.

 

What information you should store in a data layer?

This is the most important question of all.

If you want to avoid the hassle of modifying data layers every time you deploy a new TMS solution then you should aim to hard-code one universal data layer on each web page of your website. Each universal data layer should include all the key attributes of the web page, on which it is embedded. 

Your data layer should include all the key information your marketing and analytics tags currently need. It should also include all the key information you may need in the near future, for additional tracking.

This information could be:

#1 Page attributes

  • page title
  • page URL
  • page category etc

#2 Product(s)’ attributes – attributes of a product(s) listed on a web page like:

  • product name
  • product id
  • product price
  • product category
  • product image URL
  • product variant (color, size, dimension) selected
  • product quantity selected
  • product views
  • product clicks (clicks on a product listing on a category page)
  • product availability (available, out of stock)
  • product coupon
  • product coupon used

#3 Users’ attributes

  • User ID (log in ID)
  • Client ID
  • User type (logged-in user, logged-out user, member, non-member, customer, non-customer, repeat customer, high value repeat customer etc)
  • User’s browsing behaviour
  • User’s prefences
  • User’s purchase history
  • Users’ interactions (clicking a button, submitting forms, signups, purchase, watching videos etc.)
  • Users’ web browser
  • Users’ operating system
  • Users’ device (mobile, desktop, tablet)

Any information that might be useful either today or in the near future for additional tracking, should be included in the universal data layer.

Following is a real life example of a data layer being used on a website:

And here is how this data layer looks like in Google developers tool’s console tab:

data layer example

If you look at this screenshot closely, you can see that all data layer variables are reported in alphabetical order in the developer’s tool console tab.

That’s how you should write your data layer variables, in alphabetical order. So that debugging data layer becomes easy later.

Another thing worth noting is that, the data layer above contains some dynamic data layer variables, near the end like: VisitorsAge, VisitorsEmail, VisitorsGender etc. These data layer variables weren’t hard coded on the web page but were dynamically pushed into the data layer via the push method.

How to see and interpret the data layer on a web page?

To do this, follow the steps below:

Step-1: In Google Chrome, right click on a web page (the page must have the GTM container tag installed) and then select ‘Inspect’. This will open up the developers’ tool window at the bottom of your browser.

Step-2: Click on the ‘console’ tab at the top right of the tool window and then type ‘dataLayer’ at the command prompt and press ‘enter’ key:

type data layer at the prompt

Once you press the enter key, you will see following three default JavaScript event objects:

javascript objects

gtm.js is a javascript library file used by the container tag. If this library file doesn’t load then your GTM won’t work. Once gtm.js library file is loaded, the container tag passes the event ‘gtm.js’ to the data layer.

gtm.dom is a DOM file used by the container tag. If this file doesn’t load then it means, DOM has not loaded. Once the DOM is loaded, the container tag passes the event ‘gtm.dom’ to the data layer.

gtm.load is a load file used by the container tag. If this file doesn’t load then it means, window and all its contents are not loaded. Once the window and all its contents are fully loaded, the container tag passes the event ‘gtm.load’ to the data layer.

These events are fired in the following sequence: gtm.js > gtm.dom > gtm.load

Every data layer get these three events from the container tag. If your data layer doesn’t get any/all of these events then it means something has gone wrong with your tag manager implementation. It could either be a broken container tag or broken data layer or both.

If you have hard coded data layer on a page then developers’ tool window will show you at least four JavaScript objects. One JavaScript object would be your data layer and the other three will be the default JavaScript event objects:

javascript objects 2

Another article you will find useful: Bare Minimum Statistics for Web Analytics

Announcement about my books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Content is protected !!