In this guide, I’ll be going over my top 10 Google Tag Manager variables and give you a brief overview of each one.
I won’t get too much into how each variable actually works but I will go into some of the ways you can use them in GTM.
What are Google Tag Manager Variables?
Variables are like placeholders within Google Tag Manager that let you fill your tags, triggers, and other variables with this placeholder information. These variables get resolved during the page load and can hold different values.
The Top 10 GTM variables are:
- Data Layer Variable
- Auto-Event Variable
- Constant Variable
- Google Analytics Settings Variable
- Refer URL Variable
- 1st Party Cookie Variable
- DOM Element Variable
- Lookup Table Variable
- Regex Lookup Table Variable
1. Data Layer Variable
The first variable is the Data Layer Variable. If you have a Data Layer already set up on your page, you’ll be able to pull out the data stored in the value of Data Layer and transfer it to a Data Layer Variable as shown in the visitorType example below:
Once you have set up your Data Layer Variable, you will see it under Variables.
How to Set Up a Data Layer Variable?
To set up your Data Layer Variable, simply go over to Google Tag Manager and on the variables panel, create a new variable of the type Data Layer Variables. In this case, we want to pull out the value of our visitor logged out and store it in a Data Layer Variable.
To do this, simply enter the key into the Data Layer Variable Name field and you’ll be able to then pull this out of the Data Layer and store it in a variable.
Once done, you will see that you’ve successfully set up the visitorType Data Layer Variable.
There are a few other tricks to this Data Layer Variable. You can check it out here:
2. Auto-Event Variable
If you don’t see auto-event triggers available in this list – for example, the Click Trigger – then you need to enable all your auto-event tracking variables before moving forward.
How to Enable Auto-Event Variables?
To enable Auto-Event Variables, go over to your variable menu and Configure your built-in variables.
Here you will find certain Auto-Event Variables that you can enable, just click them and they will be enabled for you in your account.
Head back and try your click trigger. If it’s already installed, just head over to your page and reload it. You should see in your variables menu, the enabled Auto-Event Variables.
Not all of them always get filled. But if they get filled, they have some important information that lets us distinguish our different clicks from each other. And make sure our filter within our auto-event trigger is correctly filled.
3. Google Analytics Settings Variable
The third is the Google Analytics Settings Variable. Google Analytics Settings Variable is simply a variable that will fill out any areas that use this variable with the same value. If the value of this variable is then changed at a later date all the areas that use this variable will be updated.
This saves a lot of time manually updating and keeping track of important fields in your account.
Why Is Using the Google Analytics Settings Variable so Useful?
If you have Google Analytics installed through Google Tag Manager, and you have several tags that are sending data over to Google Analytics, you can use the Google Analytics Settings variable to fill out fields in GTM with your Google Analytics Tracking ID from one centralised location, making it easy to swap out this value for something else down the road.
Setting Up a Google Analytics Variable
Below is an example of Tracking ID. This is something we would need to repeat all over again. For convenience, we can simply build a new Google Analytics Settings Variable that will prefill this Tracking ID field.
To build a new Google Analytics variable you should fill the Tracking ID and press Save.
Here in the settings variable drop down, choose the variable you made. Turn off the enable override settings in this tag so all the settings that would normally be put into these fields here are now governed by one central variable, the Google Analytics Settings Variable.
4. URL Query Variable
Next variable is the URL Query Variable. This variable is particularly useful if you see any kind of query string in your URL and would like to get its information. Learn more about URL variable in this post:
How to Pull Information through the URL Query Variable?
To pull out information through the URL Query Variable, go over and create a new variable of the type URL.
Choose Query as the component type, then type in the Query Key.
The key is whatever is in front of the equal sign of your desired value. In this sample, it would be simply s.
5. Referrer Variable
The Referrer Variable is a variable that is already built in and also enabled by default. You could find it under your variable menu shown below:
What is a Referrer Variable?
Referrer Variable is the URL that the user just came from, before he entered a page. We could utilize this to always fire a tag when somebody comes from google.com, for example, or we could also utilize this in our other tags to send that data to our analytics tools.
6. 1st Party Cookie Variable
If you navigate around with Google Tag Manager on your page, you’ll know that the preview mode reloads every time you go to a new page. That also means that the Page View, DOM Ready and Window Loaded, appear new in your preview mode, and the data layer gets filled with new data.
Unfortunately, Google Tag Manager cannot remember anything that was going on beforehand. Each page load means a new loading of Google Tag Manager. With web development, you can persist the data. And commonly there are cookies used to do this. You can set cookies with Google Tag Manager, but that will be for another post.
How to View Browser Cookies?
To view the cookies set on our browser, you need to open the chrome menu and go to Developer Tools.
Here we get some useful information under the application tab. And Cookies right here we can see what data was set through different code on our website.
Now in some cases, there’s more useful information. In other cases, it’s unusable because you don’t really know what it exactly means. But as I said, you could actually also set your own Cookies.
For example, this Cookie here is this user newsletterSubscriber was set to true. Probably he came from a newsletter source.
Building 1st Party Cookie Variable
To build a 1st Party Cookie Variable, all you need to do is go over to Google Tag Manager and under the user-defined Variables will choose a 1st Party Cookie Variable as our type.
In this sample, the Cookie Name is newsletterSubscriber. And this should now pull out the right value from our Cookie.
And voila, here we go.
7. DOM Element Variable
DOM stands for document object model. This is how our site is marked up and we can simply right click on any element on our website and click on Inspect in Chrome.
This will open up the elements pane here and give you the visual representation of the document object model. With the DOM Element Variable you’ll be able to pull out any kind of node that you are interested in and get either an attribute or the text in between those elements.
How Does It Work?
An example will be pulling out the cart menu, and the amount of the cart menu. You would need to either know the ID or the CSS Selector. Luckily in this sample there is a class called amount which you can utilize in your variable really easily.
So let’s head over to Google Tag Manager. And under variables, we’ll go into our new user defined variables and click on the DOM Element for variable type.
The selection method is CSS Selector because we don’t have an ID, and we have a class so in the element sector, you put a dot first then the amount. Note that you need to know a little bit about CSS Selectors in order to do this.
And then we can choose the attribute that we want to pull out. Since we want to pull out the text, we can leave this empty for now. Then press Save.
Screenshot of Google Tag Manager showing a variable configuration with CSS Selector as the Selection Method, .amount as the Element Sector and a blank for the attribute name.
Try it out!
8. Lookup Table Variable
Getting more advanced, we’re gonna take a look at the Lookup Table Variable. The Lookup Table Variable has different use cases, but the easiest would be to simply rewrite an existing variable.
How to Rewrite an Existing Variable?
An example would be the variable we created earlier that would pull out the value from our cookie newsletterSubscriber, true.
Now if you use this inside of an event tag, for example, you would simply see true in the action and you wouldn’t quite know what this means. So you would like to rewrite this variable into something more recognizable like subscriber. This is easily possible with the Lookup Table variable.
Now, let’s head over to Google Tag Manager and build a new user defined variable. And this time, we’ll go with the Lookup Table.
In the variable dropdown, choose cookie.
If the input is true, then we want to rewrite it into Subscriber. If the input is false, rewrite it into a Non Subscriber. And for the default value, we can just set it to Unidentified.
Alright, let’s try this out! Our variable that we had before shouldn’t change. We still have cookie newsletterSubscriber true, but rewritten, we should have this in our Lookup Table as Subscriber.
9. Regex Lookup Table Variable
The next variable is the Regex Lookup Table Variable. What you might have noticed is that when we created our Lookup Table subscriber variable, we need to be very precise with the names.
If I type in anything extra or put a white space, it wouldn’t match up anymore. And we’ll just get undefined in our field value.
Therefore, the matching option that we have in this first input column is similar to “equals true” so the cookie newsletterSubscriber needs “equals true” in order for it to go into the output subscriber.
But what we also have available is a variable type of Regex Lookup Table.
What is Regex?
Regex stands for regular expression. And if you’re familiar with Regex, you know that you can really get detailed in your search query, which can be very powerful.
How Does It Work?
For example, we would like to classify all product pages into a product category. So we would like to have an output of “product”, simply to classify our product pages. And we can have one for category pages as well. We can easily do this with a Regex Table Variable.
In the variable Configuration page, the input would simply be Page Path. If the Page Path contains anything like / product then the output should Product. And if there’s a /category in there, then the output should be Category.
So what I will do here is put around dot stars, which means everything multiplied. So let’s put this in the front and the back here and see if this works. Let’s save this, and refresh the page.
And in all variables we now see the Regex table has the name Category that matches up the category page based on the URL path.
And if you go to a product page, it also matches the name, Product, in the Regex Table.
You can become more sophisticated with Regex Table than you are with the normal Lookup Table. And this can be very valuable, for example, for content grouping and Google Analytics or to look at the Referrer.
You could also do channel grouping really effectively, and send this on to Google Analytics easily. So Regex Table is really an advanced feature, which can be quite powerful in Google Tag Manager.
How Does it Work?
Here, we need to adhere to some conventions. One is to write an ominous function that returns a value.
As an example, let’s go with the current time, which we can get by writing a new Date and it’s return value as time.
Refresh your page and in the variables, you’ll see a variable of the Return Type date with the current date and time as the Value. And this should dynamically change. You see here at the second level, that some of these are changing, depending on when the event loaded.
Alright, so there you have it. These are my Top 10 variables that I use within Google Tag Manager. There are more out there and there are also many other use cases for these specific variables.
I leave it up to you to give me your tips down in the comments below. I’d love to hear from you as always.
And for more advanced lessons like this consider checking out our MeasureMasters program here.