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

January 3, 2024

Undefined Variable in Google Tag Manager

Updated: January 3rd, 2024

In this quick guide, I’ll shed some light on an Undefined variable in Google Tag Manager and where you can use it.

First of all, you can find this variable among other User-defined variables in your GTM container:

It is simple to configure. It has no settings at all 🙂 (if we ignore the standard “Format Value” feature).

And all this variable does is just return an undefined value. So why is it useful?

The answer: you can use it in your GTM stack to keep the data cleaner. Let me explain.

Previously, GTM introduced the Format Value option in variables’ settings that allows you to format the output of your variables thus make it more standardized and cleaner. The next step GTM took in data cleaning was the introduction of the Undefined variable.

If you did not know, Google Analytics tags (in GTM) drop a parameter from the payload (read request sent to Google Analytics) if that parameter’s value is undefined. And that’s where the Undefined Variable might become useful.

Example use case of the Undefined Variable

So, if you have a lookup or regex table with multiple rows, you might sometimes want that table to return an undefined value instead of a string. Let me illustrate.

Disclaimer: I could not develop a solid example here, so don’t be too hard about it.

Say that I run a website with multiple categories (total number of categories is 6). Those categories are visible in the Page URL, e.g. www.example.com/category/loremipsum. Now, for some reason, you want to see 5 out of 6 categories in your GA reports as a custom dimension, while one (not-so-important) should remain untouched. What should you do?

You could use a Regex Variable (or Lookup) here. It could check the Page Path (or Page URL) of the current page, parse it and if a certain condition is met, it would output the name of the Category. Later, that output could be sent over to Google Analytics as a custom dimension.

Here’s a screenshot of the sample Regex Variable where I also use the new Undefined Variable.

If the {{Page URL}} variable matches one of the first 5 rules, it will return a particular output value (e.g. Category 1, etc.). If the URL of the current page matches the 6th rule, then its value will be returned as undefined. Not the string (text) “undefined” but the actual undefined value (read no value at all).

***

Quick side note: you might say that an Undefined variable is obsolete here because you can just use certain Regular Expression patterns to exclude the 6th category. You are right. But as I have mentioned several paragraphs ago, I could not come up with a solid example for this case, therefore, I’m playing soft here. End of a sidenote.

***

Why can’t you just enter a plain word “undefined” in the output value of the lookup/regex table? Because if you do so, “undefined” will be passed as a text to GA and you’ll see “undefined” in your GA reports. However, if you passed the undefined value (not as a text/string but as an actual undefined value), then GA tag in GTM would drop this parameter from the request and your GA reports would stay cleaner.

Subscribe and Get the Ebook - Mastering GA4 event tracking

The difference between undefined and “undefined”

If you feel confused regarding the difference between undefined and “undefined” values, take a look at the screenshot below. Here I have created a sample Regex table with two possible outputs, “undefined” or undefined.

In the Output A, you can see a string “undefined”, which is a text that consists of one adjective, “undefined”. Output B, on the other hand, has no value, therefore, it is undefined.

If you pass a string “undefined” to GA, you’ll see that very exact text in your GA reports (for example, if you pass it as a Custom Dimension). But if you pass a parameter with no value (undefined), then it will be dropped before being sent to GA. So if you insert an Undefined Variable to a Lookup/Regex table, you could keep your GA reports slightly cleaner.

Another side note: Keep in mind that Regex and Lookup tables default to undefined value anyway, therefore, the screenshot above is just for demonstration purposes and, in fact, the 2nd row in the table (related to “blog”) is obsolete because it would not make any difference. If {{Page URL}} does not contain”post”, it would always output the undefined value. Why did I show it then? I just wanted to show you how a string and undefined values look in the Preview and Debug mode.

 

Undefined Variable in Google Tag Manager: Conclusion

This neat little GTM feature can be helpful when you need a variable with an actual undefined value. In this blog post, I shared an example of using an Undefined Variable in Regex or Lookup table.

However, it’s not an innovation that was impossible before. Before this feature, you could still create an Undefined Variable in GTM but with a Custom JavaScript Variable.

So you can treat the new Undefined Variable as a more elegant solution for non-developers who are allergic (?) to code 🙂

If you noticed that I missed something or have some other related questions, feel free to drop a comment below.

Julius Fedorovicius
In Google Tag Manager Tips
5 COMMENTS
Christopher Regan
  • Nov 13 2018
  • Reply

Great article! It's the small things, so many times, that can help us achieve smarter & better. Thanks for pointing this out.

Adil Khan
  • Nov 25 2018
  • Reply

In the Regex table, is it possible to leave the output as blank? What would that return in GA compared to {{undefined}}?

    Julius Fedorovicius
    • Jan 16 2019
    • Reply

    Hey, if you leave the output blank, it will return an empty string '' so it' better to insert the undefined variable

David
  • Oct 18 2021
  • Reply

Hi Julius,

Thanks for sharing this! Maybe you could help me out with something:

I´m working with a developer to do some dataLayer pushes for Enhanced eCommerce. He´s working with PHP. How should I instruct him to send undefined values like you described in this article?

Looking forward to your reply!

Thanks in advance :)

David
  • Apr 30 2024
  • Reply

You can use the text Undefined direct these days as well in conditional items.

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
  • What is Direct Traffic in Google Analytics 4 and How to Fix it
  • Google Tag Manager Preview Mode Not Working? 26 Ways To Fix It.
  • Google Analytics 4 Events Not Showing Up in Reports?
Analytics Mania - Google Tag Manager and Google Analytics Blog | Privacy Policy
Manage Cookie Settings