What are Variables in Google Tag Manager and How to You Use them?

After configuring tags and triggers, we’ve now reached another critical component of the Google Tag Manager (GTM) toolbox: These are Google Tag Manager Variables.

GTM Variables are an essential component, serving as dynamic placeholders that allow your tags and triggers to function with precision and flexibility. 

They streamline the process by enabling you to capture, store, and utilize dynamic data across your analytics and tracking setup. 

Whether you’re working with pre-configured variables or creating custom ones, this tool enhances GTM’s efficiency by ensuring that your tags deploy accurately and only when needed.

This guide dives deep into GTM variables, explaining their purpose, types, and how to use them effectively to streamline your tracking and analytics.

Get in touch to learn about Analytico’s Digital Analytics Audit services or GA4 audit services.

What Are Google Tag Manager Variables?


Google-Tag-Manager-Variables

In simple terms, Google Tag Manager Variables act as placeholders for values. They store information that can be dynamically retrieved and used by tags, triggers, and other variables within GTM. 

Each variable holds a specific value, which can be reused across your setup, making it easy to maintain consistent tracking logic.

For Example! 

A variable could store your website’s domain name, the click text of a button, or the URL of a page. 

When you set up tags or triggers, you can call on these variables to fill in specific values. This makes GTM highly adaptable, reducing redundancy and minimizing the need for code changes.

Why Are GTM Variables Important?

Without variables, you would need to manually input values each time you wanted a tag to fire or a trigger to activate, which is impractical and error-prone. 

GTM variables streamline the process by storing information once and reusing it across tags and triggers, simplifying your entire GTM setup

They make it easier to configure complex tracking, reduce setup time, and maintain accuracy, especially as your tracking needs evolve.

Types of Google Tag Manager Variables

Google Tag Manager offers several types of variables, each serving a unique function in the tag management process. 


Google-Tag-Manager-Variables

Let’s break down the primary types of GTM variables you’re likely to use.

1. Built-in Variables

Built-in variables come pre-configured in GTM and cover common data points, making it easy to set up tracking without custom code. You can enable or disable these variables depending on your needs. Some popular built-in variables include:

  • Page URL: Captures the URL of the page a user visits.
  • Click Text: Captures the text of the element clicked by a user.
  • Scroll Depth Threshold: Tracks how far users scroll down a page.

For Example! 

If you want a tag to fire only when a user clicks a specific button, you can use the “Click Text” variable to identify the button by its text.

2. User-Defined Variables

When you need more control over the information GTM collects, user-defined variables allow you to create customized variables that suit specific tracking needs. These variables offer flexibility, and there are several types to choose from, including:

  • Constant Variables: Store static values that don’t change, such as API keys or campaign names.
  • Data Layer Variables: Retrieve values from the GTM Data Layer, such as product information on an e-commerce site.
  • JavaScript Variables: Execute JavaScript code and return the resulting value. Useful for custom calculations or logic.
  • Auto-Event Variables: Capture details about user interactions, like clicks or form submissions.

These variables are ideal for more complex setups, giving you the flexibility to collect and use exactly the data you need.

3. Custom JavaScript Variables

Custom JavaScript variables provide even greater customization. They allow you to add custom JavaScript code to retrieve specific values, perform calculations, or alter data as needed. 

With this feature, you can define custom scripts to perform tasks that are otherwise not possible with built-in or standard user-defined variables.

For instance, you might create a custom JavaScript variable to format data in a specific way before sending it to Google Analytics.

Examples of How GTM Variables Work

To better understand how GTM variables function, let’s walk through a few examples.

Example 1: Tracking Button Clicks

Imagine you want to track clicks on a “Sign Up” button on your website. 

Instead of hardcoding the button’s details each time you set up tracking, you can use the built-in Click Text variable. 

By setting up a trigger that fires only when the “Click Text” equals “Sign Up,” you can ensure the tag fires only when that specific button is clicked.

Example 2: Tracking Scroll Depth

For a blog or content-heavy page, you might want to know how far users scroll down the page. 

The Scroll Depth Threshold variable can help you track how many users reach different scroll points (25%, 50%, 75%, etc.). 

By combining this variable with a scroll trigger, you can gain insight into user engagement with your content.

How to Use Variables in Google Tag Manager

Setting up the GTM variable involves a few straightforward steps. Below is a step-by-step guide to help you get started.

  • Enable Built-in Variables


Google-Tag-Manager-Variables

Go to Variables in your GTM dashboard. Click on Configure to see the list of available built-in variables.

Check the boxes for the variables you want to enable. For example, you might enable “Page URL” or “Click Text” for standard tracking needs.

  • Create User-Defined Variables


Google-Tag-Manager-Variables

In the Variables tab, click on New. Select the variable type that suits your purpose, such as Constant, Data Layer, or JavaScript Variable.

Enter a name and configure the settings according to your requirements. For example, if you’re creating a constant variable to store an API key, simply enter the key as the value.

Save the variable.

  • Add Variables to Tags and Triggers

Once your variables are set up, you can use them within tags and triggers:

Tags: When configuring a tag, use variables to define dynamic values. For instance, in a Google Analytics tag, you might use the “Page Path” variable to track specific page views.

Triggers: Variables are often used as conditions for triggers. For example, you could set a trigger to fire only when the “Click Text” variable equals “Download,” ensuring that the tag only activates when users click on a download button.

Best Practices for Using GTM Variables

To maximize the value of GTM variables, keep these best practices in mind:

  • Name Variables Clearly

Give each variable a descriptive name to easily identify it later. This can be especially helpful in large GTM setups.

  • Organize and Audit Regularly

Regularly review and clean up unused variables to keep your GTM container organized and efficient.

  • Use Custom JavaScript Sparingly

While powerful, custom JavaScript variables should be used carefully, as they can slow down GTM if overused.

  • Take Advantage of Data Layer Variables:

If you have custom data stored in the GTM Data Layer, use Data Layer variables to capture and use that information within your setup.

Common Use Cases for GTM Variables

GTM variables support a wide range of tracking use cases. Here are some common ways they’re used:

Google-Tag-Manager-Variables

  1. E-commerce Tracking

Data Layer variables allow you to track transaction details, such as product IDs, prices, and user actions.

  1. Form Tracking

Auto-Event variables, like Click Text and Form ID, are ideal for tracking form submissions and capturing valuable lead data.

  1. Page-Specific Tracking

By using variables like Page URL or Page Path, you can implement tracking that only fires on certain pages, such as checkout or product pages.

  1. Campaign Tracking

Constant variables can be used to store consistent campaign values, ensuring your tracking setup aligns with marketing initiatives.

Final Thoughts on GTM Variables

Google Tag Manager Variables allow managing data dynamically, efficiently, and without extensive coding. 

By understanding the types of GTM variables and knowing when and how to use them, you can unlock GTM’s full potential for advanced tracking and analytics.

GTM variables are indispensable tools, from built-in variables that provide common data points to custom JavaScript variables that allow full control.

They streamline the process of creating dynamic tags and triggers, helping you capture precise insights into user behavior, optimize site performance, and fine-tune your marketing efforts.

Mastering GTM variables is a must for anyone looking to harness the full capabilities of Google Tag Manager. With a bit of practice, you’ll be able to design a flexible, efficient, and scalable tracking setup that meets your analytics goals.

Do you like what you read? Learn more about Digital Analytics on our blog here.

Topics:
This article was last updated on October 26, 2024

Don’t forget to share this post!

Twitter
LinkedIn
Facebook
Reddit