
January 15, 2018
How to Insert Variables in Google Tag Manager: 7 Things to Keep In Mind
Tags, triggers, and variables, the holy trinity of Google Tag Manager. In case you didn’t know (which I doubt), Tags are various functions which can track visitor, inject something to website’s code, display a popup, etc., while Triggers are rules which define when tags should be fired.
What about variables? What are those? In fact, they are little pieces of information which can be used both in Tags and Triggers. For example, Page URL. You can use it as a trigger (e.g. fire Google Analytics Tracking code when Page URL contains “/successful-registration”) and you can also pass its value along with Google Analytics event tag to Google Analytics servers.
Although this is one of the core features and concepts in GTM, I still notice a lot of people not knowing how to utilize variables correctly and where they might (not) function properly.
As a result, this blog post was born where I’ll give you 7 quick tips on how to insert variables in Google Tag Manager correctly.

#1. All inserted variables are surrounded by double curly brackets {{ }}
No matter where you see those brackets (either in a tag, trigger, or whatever), you can be 100% sure, it’s a variable. In the screenshot below, I’ve created a Universal Analytics event tag. Whenever a new email subscriber is acquired, an event will fire and {{dlv – action}} variable will be replaced with some real information, e.g. the position of Subscribe form.
#2. You can insert variables in any field which has a LEGO brick button next to it
GTM developers have made it clear to understand where variables can or cannot be used in their interface (so you don’t have to guess). If there’s a button with a LEGO brick next to it, then you’re covered! Go ahead and insert variables.
By the way, you’re not limited to inserting just one variable. If you need, go ahead and add two or five! Whatever makes sense to you! Also, here are a couple of examples where variables won’t work:
1. Tag Firing Priority (in tag’s Advanced Settings section)
2. Trigger conditions. You cannot create a condition where one variable (chosen in the first drop-down list) equals (or contains, etc.) to another variable.
#3. You can insert a variable in Custom HTML tag or Custom JavaScript variable by surrounding it with {{ }}
This one is related to tip #1, where all variables in the interface are surrounded by double curly brackets. Custom scripts are also no exception. I have used this technique in multiple GTM recipes and blog posts, such as How To Transfer UTM From One page To Another. Remember, if you see a script in GTM which contains {{something like this}}, then you’ve just found an inserted variable.
When I’ve explained this tip to one of my students, a question popped up:
What if I change variable’s name (in its settings)? Do I have to manually check all tracking scripts within the container and change the names there as well?
I believe that some of my readers can relate to this question. Fortunately, GTM got you covered and I’ve explained the solution in the next tip.

#4. If you change variable’s name, GTM will find all references to it (within a container) and rename them as well
Imagine a situation, you have a variable which is used in at least two or three triggers and in some tags too. What would you do if, for some reason, you are required to change the variable’s name?
Normally, you’d have to:
- Change the name in variable’s settings
- Then search in every corner of GTM container and manually change names of that {{inserted variable}}
Luckily, Google Tag Manager has this awesome and time-saving feature which checks all current insertions of a particular variable and once you rename the variable (in its settings), GTM scans all places (within a container) and changes the name accordingly.
#5. GTM will not allow you to preview/publish container if It contains an unknown variable
This is another feature which prevents marketers and analysts from making stupid mistakes. If you have inserted a variable ANYWHERE in the container (say, in a tag) but that variable is nowhere to be found in Variables section of your GTM account, you’ll get an error once you hit the Preview or Publish buttons.
The error is pretty informative therefore you’ll be able to solve it fast.
P.S. Keep in mind, that all GTM Recipes which include sending data to Google Analytics have a missing Universal Analytics Tracking ID (UA-XXXXXX) variable. But actually, it’s a feature because you are forced to create it and set a correct Google Analytics Tracking ID.
#6. Yes, you can use a variable within a variable
Where’s my meme? Oh, here it is!
You are not limited to use variables only in tags and triggers, variables can also contain other variables. To name a few examples: Lookup table, Regex Table, Google Analytics Settings Variable.
#7. Not all variables will work if JavaScript is disabled in a browser
Your question might be “what DOES work without JavaScript in Google Tag Manager?”. Almost nothing, except for Custom Image tag. If you plan to insert some variables into it, keep in mind that not all of them will work, for example, everything that is related to JavaScript (Custom JavaScript Variable, Data Layer Variable).
Here is a bit longer list of variables which will not work in Custom Image tag if JavaScript is disabled:
- Auto-event variables,
- 1st-party cookie variables,
- Custom JavaScript variables,
- JavaScript variables,
- Data Layer variables,
What about those variables which will work in Custom Image Tag when JavaScript is disabled? Here are some of them:
- Default URL variables (like Page URL, Page Path, etc.),
- Custom URL variables (e.g. utm_campaign),
- Constant variables,
- Container ID.
Conclusion: how to insert variables in Google Tag Manager
It’s not surprising why Google uses the icon of a LEGO brick to represent variables. These little pieces of information can be used in multiple situations (tags, triggers, other variables), you can even combine multiple variables in one field. It’s all up to you and your imagination. Just like with LEGO.
Here are some key takeaways from reading this blog post how to insert variables in Google Tag Manager:
- Variables are surrounded by double curly brackets {{ }}.
- You can insert them into tags, triggers, variables. If you want to use them in custom scripts, not a problem! Just don’t forget those brackets.
- Variables can be inserted in all fields within GTM which are next to a button with a LEGO brick.
- Not all variables will work if JavaScript is disabled in a browser.
That’s it for now. Did I miss anything? Are there any tricks you use to insert variables in Google Tag Manager? If yes, let me know in the comments.
12 COMMENTS
Hi,
I use on my site content gropuping from Google Analytics. Can I use variable from content grouping (fixed element in html) as variable in GTM?
For example I have in html of my site
ga('set', 'contentGroup1', VARIABLE-VALUE'')
and I want to use this VARIABLE-VALUE as category value on of of events. Is it possible?
I haven't tried that. Could you share a URL of that page? I'd like to run several tests.
Hi,
thanks for answer. You can try any article from fajnyogrod.pl - for example https://fajnyogrod.pl/porady/nalewka-z-aronii-z-liscmi-wisni-lub-bez-przepis-krok-po-kroku/
Go to html source code. In line 313 you see
" ga('set', 'contentGroup1', 'artykuł'); "
where 'article' is variable I would like to insert to event category
Try to create a Custom JavaScript Variable and paste the following code:
function(){
var contentGroup1 = ga.getAll()[0].get('contentGroup1');
return contentGroup1;
}
Thanks for answer. I have now Javascript complier error:
Error at line 3, character 40: Parse error. Character '‘' (U+2018) is not a valid identifier start char
No idea what is the meaning of this, in line 3 there are less than 40 characters
Looks like that the apostrophe was copy/pasted incorrectly. Use this code: copy and paste it to GTM https://pastebin.com/j04R2hew
It works, thank you!
I just do not understand one thing in the code - in code you put here in comment "contentGroup1" was used 3 times.
In the code from link you pasted there is 2 times "contentGroup5" and only one time with "1". Anyway variable has correct value (from contentGroup1)
Could you explain it somehow?
Thanks for all your effort!
Sorry, my bad :) I had a code in the notepad where I was playing around with other contentGroup and then forgot to edit it before adding to Pastebin. Rename all 3 to contentGroup1. If you want to read contentGroup4, then rename all to contentGroup4. You get the idea.
OK, thank you :)
Hi Julius - thanks for the article.
We have google tag manager setup, and a variable called TotalCostWithTax that is being pushed properly in our confirmation page.
Can we call that variable in other scripts from the data layer? For example, can we have a separate tracking script that says <script totalCost=[TotalCostWithTax]></script> and it will pull in that variable from our data layer?
We currently have some other 3rd party tracking scripts that need this variable info, but having difficulties with populating the data with the data layer variables.
Thanks!
Hi,
If you want to use TotalCostWithTax in other Custom HTML tags within your GTM container, just create a Data Layer Variable that reads TotalCostWithTax value and insert that variable in the code itself, e.g. {{dlv - TotalCostWithTax}}. Tip #3 in this blog post mentions that.
If you want to use the value TotalCostWithTax from the Data Layer, but you want to use that outside of GTM, then use the following command in your Javascript code:
google_tag_manager["YOUR_CONTAINER_ID"].dataLayer.get('TotalCostWithTax');
Replace YOUR_CONTAINER_ID with an actual id, e.g. GTM-XXXXXXX
Hi
How do I insert 1st party cookie variables into marketing pixels codes in custom HTML tags?