About cookies on this site

We use cookies to collect and analyse information on site performance and usage, to provide social media features and to enhance and customise content and advertisements. Learn more

Cookie settings

About cookies on this site

Cookies used on the site are categorized and below you can read about each category and allow or deny some or all of them. Learn more

Necessary

Some cookies are required to provide core functionality. The website won't function properly without these cookies and they are enabled by default and cannot be disabled.

Preferences

Preference cookies enables the web site to remember information to customize how the web site looks or behaves for each user. This may include storing selected currency, region, language or color theme.

Analytical cookies

Analytical cookies help us improve our website by collecting and reporting information on its usage.

Marketing cookies

Marketing cookies are used to track visitors across websites to allow publishers to display relevant and engaging advertisements. By enabling marketing cookies, you grant permission for personalized advertising across various platforms.

  • Courses
    • Paid courses
    • Course bundles
    • Free courses
  • Blog
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
    • GA4 community
  • Services
  • About
    • About
    • Contact
  • Login
  • Courses
    • Paid courses
    • Course bundles
    • Free courses
  • Blog
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
    • GA4 community
  • Services
  • About
    • About
    • Contact
  • Login

August 15, 2022

How to Insert Variables in Google Tag Manager: 7 Things to Keep In Mind

Tags, triggers, and variables are the holy trinity of Google Tag Manager. In case you didn’t know (which I doubt), Tags are various functions that can track visitors, inject something into a website’s code, display a popup, etc., while Triggers are rules that define when tags should be fired.

What about variables? What are those? They are little pieces of information that 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.

Subscribe and Get the Ebook - Real Book Img - GTM for Beginners

#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.

GTM variables in tag

 

#2. You can insert variables in any field that 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.

GTM Variable and brick button

By the way, you’re not limited to inserting just one variable. If you need to, 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 the tag’s Advanced Settings section)

Tag firing priority

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.

Variable in Trigger

 

#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 that contains {{something like this}}, then you’ve just found an inserted variable.

When I 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.

Google Tag Manager Ebook Bundle

#4. If you change the variable’s name, GTM will find all references to it (within a container) and rename them as well

Imagine a situation where you have a variable that 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 the variable’s settings
    Change variables name
  • Then search in every corner of GTM container and manually change names of that {{inserted variable}}
    Change variable name in tag

Luckily, Google Tag Manager has this awesome and time-saving feature that 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 a container if It contains an unknown variable

This is another feature that 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.

GTM Container Error

P.S. Keep in mind that all GTM Recipes, which include sending data to Google Analytics 4, have a missing Measurement ID ID (G-XXXXXX) variable. But actually, it’s a useful feature because you are forced to create it and set a correct Measurement ID.

 

#6. Yes, you can use a variable within a variable

Where’s my meme? Oh, here it is!

GTM Xzibit meme - variables

You are not limited to use variables only in tags and triggers, variables can also contain other variables. A few examples are the Lookup table, Regex Table.

 

#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 that 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 that 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 on 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.

 

 

Julius Fedorovicius
In Google Tag Manager Tips
12 COMMENTS
Marcin
  • Oct 6 2018
  • Reply

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?

    Julius Fedorovicius
    • Oct 7 2018
    • Reply

    I haven't tried that. Could you share a URL of that page? I'd like to run several tests.

Marcin
  • Oct 7 2018
  • Reply

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

    Julius Fedorovicius
    • Oct 7 2018
    • Reply

    Try to create a Custom JavaScript Variable and paste the following code:

    function(){
    var contentGroup1 = ga.getAll()[0].get('contentGroup1');
    return contentGroup1;
    }

Marcin
  • Oct 7 2018
  • Reply

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

    Julius Fedorovicius
    • Oct 7 2018
    • Reply

    Looks like that the apostrophe was copy/pasted incorrectly. Use this code: copy and paste it to GTM https://pastebin.com/j04R2hew

Marcin
  • Oct 7 2018
  • Reply

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!

    Julius Fedorovicius
    • Oct 8 2018
    • Reply

    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.

Marcin
  • Oct 8 2018
  • Reply

OK, thank you :)

Matt
  • Jun 25 2019
  • Reply

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!

Julius Fedorovicius
  • Jun 26 2019
  • Reply

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

Shravya
  • Aug 19 2020
  • Reply

Hi

How do I insert 1st party cookie variables into marketing pixels codes in custom HTML tags?

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *


 

Hi, I'm Julius Fedorovicius and I'm here to help you learn Google Tag Manager and Google Analytics. Join thousands of other digital marketers and digital analysts in this exciting journey. Read more
Analytics Mania
  • Google Tag Manager Courses
  • Google Tag Manager Recipes
  • Google Tag Manager Resources
  • Google Tag Manager Community
  • Login to courses
Follow Analytics Mania
  • Subscribe to newsletter
Recent Posts
  • LinkedIn Conversions API with Google Tag Manager. The Guide.
  • How to Set Up Google Tag Manager Server-side Tagging with Cloud Run
  • Track HubSpot Forms with Google Tag Manager and Google Analytics 4
Analytics Mania - Google Tag Manager and Google Analytics Blog | Privacy Policy
Manage Cookie Settings