Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

If you have done anything with Google Tag Manager, you probably already know three key concepts: tags, triggers, and variables. But, actually, you should know there is one more key ingredient, the Data Layer. Google Tag Manager walks hand in hand with the Data Layer which ensures maximum flexibility, portability, and ease of implementation. Without it, there would be no variables and no triggers in GTM, therefore no tags would be fired. It is what keeps your tag management running properly.

Unfortunately, I’m not going to dive into what the Data Layer is because that’s a whole another story. In today’s blog post, I’ll take a look at what the Data Layer Variable is.

If you have no clue what Data Layer is, read this blog post first and then come back to this article.

 

The Context

Before we dive in, let’s quickly remember how the Data Layer (DL) works. DL is a virtual layer of a website where useful information is stored, e.g. Page URL, Referrer, Page Title, ID of a Clicked element, etc. This data can be later transferred to other tools like Google Analytics, Mixpanel, Adwords, etc.

In fact, Google Tag Manager already supports a bunch of default built-in variables. You can find a full list of them by going to Variables > Configure.

Default GTM Variables

After you enable these variables, they’ll appear in GTM Preview and Debug mode’s Variables Tab.

GTM variables tab

These data points can later be used in trigger conditions, tags, even other variables (like Lookup Tables). And that’s awesome! But what if we want to have some custom variables? For example, blog post author’s name, user’s registration country, etc.?

That’s where the Data Layer Variables comes in handy.

What is Data Layer Variable?

Data Layer Variable enables you to fetch a value from the Data Layer and turn it into a variable (which can be used in Tags, Trigger, and other variables). But first, let’s take a look at how the data is stored in the Data Layer.

Enable GTM Preview and Debug mode and go to the Data Layer tab. In the screenshot below I am viewing my blog’s data, yours might look different.

duracelltomi gtm event

Looks like I have some interesting data there: pagePostType, pagePostType2, pageCategory, pagePostAuthor. My blog is running on WordPress and I’m using DuracellTomi’s GTM Plugin which pushes this data to the Data Layer every time page loads.

If you want to get even more custom information, ask a developer to add it to the Data Layer.

Anyway, let’s go back to the example. As you can see in the screenshot above, there’s some interesting data I could employ in my tags and triggers. Unfortunately, if I navigate to the Variables tab of the Preview and Debug Console, those variables aren’t there. Why?

By default, Google Tag Manager does not recognize custom data in the Data Layer thus you cannot use it as variables unless you use the Data Layer Variable. In order to create this variable, you’ll need specify the Data Layer key whose value you want to retrieve. When the Variable is resolved, it will return whatever was most recently pushed into the key. Easy as that!

If I wanted to fetch pageCategory value, I’d just need to set the pageCategory key in variable’s settings. Let me show you how it works in action.

 

Let’s Create a Data Layer Variable

In my previous example, there were 4 custom keys and values in the Data Layer:

  • Key: pagePostType, value: post
  • Key: pagePostType2, value: single-post
  • Key: pageCategory, value: google-tag-manager-tips
  • Key: pagePostAuthor, value: Julius Fedorovicius

With help of Data Layer Variable, I can easily retrieve those values and re-use them in other tags, triggers, or variables.

Turn data layer variable into GTM variable

Say, you want to send a Google Analytics event when someone leaves a comment. With every event, you also want to push article author’s full name. This way you’ll see which authors drive the highest reader engagement.

In this example, I will not go into details how to create a tag, as I will only demonstrate how to pull the data from the Data Layer and turn it into a variable (within Google Tag Manager).

In Google Tag Manager account, you should go to Variables and create a new one with the following settings (dlv stands for data layer variable):

post author data layer variable

That’s it! Save this variable, refresh Preview and Debug (P&D) mode, and refresh the website you’re working on. You should then see your newly created variable in the Variables tab of P&D console.

Data Layer Variable in Preview and Debug console

 

Data Layer Version

Another setting available in the Data Layer Variable is Version. When you select the Version, you’re instructing GTM to treat the values in the data model in two different ways.

 

Version 1

It’s pretty limited and does not allow you to access nested values. If you create a DL variable and tried to access pagePostAuthor (in the example below), you’d fail because it’s not in the root of the object (instead, it’s a direct child of attributes key).

So if you wanted to fetch Post Author’s name, the object in the Data Layer should look like this:

See? There’s no attributes key and pagePostAuthor is in the root level.

That’s not the only limitation of Version 1. There’s also no merging available. Every time you push the data to the Data Layer, it will overwrite the entire object. Let me illustrate. Imagine that we have two Data Layer Pushes with different data. The first push contains only pagePostAuthor, the other one includes two more keys, pageCategory and pagePostType.

As a final result, you’d have only two values in the Data Layer: pageCategory and pagePostType because it has completely overwritten the data of the 1st push.

So what’s the point of the 1st Version? It sounds like a useless thing, you might say. That’s not entirely true. I’ve noticed that sometimes it’s really important to have a “clean” Data Layer without previous values, meaning that every time a dataLayer.push occurs, it completely wipes out the old data and stores the new one.

 

VERSION 2

The 2nd version is much more flexible. It allows you to access nested values, arrays, merge data.

Let’s go back to the previous example with two subsequent Data Layer Pushes. The first push contains only pagePostAuthor, the other one includes two more keys, pageCategory and pagePostType.

Contrary to Version 1, in Version 2 all three values would remain in the Data Layer because they were not conflicting.

If the 2nd Data Layer push also had pagePostAuthor key, as a result, the final data in the Data Layer would have looked like this:

This happened because pagePostAuthor from the 2nd push overwrote the pagePostAuthor from the 1st one. Simo Ahava has posted a detailed guide about Data Layer Versions. If you still have some questions, go check it out.

 

Default Value

The last setting in the Data Layer Variable is Default Value. If you’re trying to access the value of a particular key in the Data Layer AND that key does not exist, “undefined” will be returned. Sometimes you might need to get a different default value, e.g. (not set), empty, or anything else.  In that case, click the checkbox and set the default value.

Data Layer Variable Default Value

 

 

Different Data structures

Keep in mind that data can be stored in the data layer using different structures. Here’s an example (the key pagePostAuthor is in the root level)

Or this (pagePostAuthor key is now a descendant of the attributes key):

Or even like this (there are two arrays that are the descendants of the transactionProducts key):

Every structure requires a bit different way how you should define the key in the Data Layer variable. In case of the first example, the key should look like this:

1st level key in Data Layer Variable

In the 2nd example the correct variable name should be:

Child key in Data Layer Variable

In the 3rd example, you should pull first product’s category by entering the follow name:

Array in the Data Layer Variable

There’s a chance that the latter screenshot confused you a bit. Where did that 0 come from? If you want to better understand, how to pull the data from the Data Layer, read this blog post.

 

Further Reading

This is neither the first nor the last post about Data Layer in this blog. So if you have any questions, check out these posts (or subscribe to my newsletter and stay up-to-date):

 

FInal Words

Data Layer variable is one of the most (if not THE MOST) used type of variables in my Google Tag Manager accounts.

With its help, you can access data stored in the Data Layer and utilize it in your tags, triggers, or even other variables. In this blog post, I’ve explained how to set up a variable, how to access data (which is stored using different data structures), what’s the difference between Version 1 and Version 2, etc.

If you have any questions, feel free to leave a comment below or drop me a tweet/LinkedIn message.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Julius Fed

Product manager at Soundest. I am highly interested in Google Tag Manager, Google Analytics, Adwords, Email marketing, Email Deliverability, Digital Marketing in general. You can follow me on Twitter.