How to create and manage variables in Google Tag Manager

Author:

Creating and managing variables in Google Tag Manager (GTM) is a crucial part of tracking user interactions and customizing tag behavior on your website or application. Variables allow you to store and retrieve dynamic values that can be used in tags and triggers. Here’s a detailed guide on how to create and manage variables in GTM.

Step-by-Step Guide to Create and Manage Variables in Google Tag Manager

1. Access Google Tag Manager

  • Log In to GTM: Go to the Google Tag Manager website and log in to your account.
  • Select Your Container: Choose the container where you want to create or manage variables.

2. Understanding Variables

  • What is a Variable?: A variable is a placeholder for a value that can change, such as a URL, user ID, or click text. Variables can be used in tags and triggers to customize their behavior.
  • Types of Variables:
    • Built-In Variables: Predefined variables that GTM provides for common use cases (e.g., Page URL, Click Classes).
    • User-Defined Variables: Custom variables that you create to capture specific data points.

3. Creating a User-Defined Variable

To create a new variable in GTM:

  1. Navigate to Variables:
    • In the left sidebar, click on “Variables.”
  2. User-Defined Variables Section:
    • Scroll down to the “User-Defined Variables” section and click on “New.”
  3. Name Your Variable:
    • Give your variable a descriptive name that reflects its purpose (e.g., “Button Click Text”).
  4. Choose Variable Type:
    • Click on “Variable Configuration” to select the type of variable you want to create. Common variable types include:
      • URL: Captures parts of the URL (e.g., full URL, host, path, query).
      • First Party Cookie: Captures values from cookies.
      • JavaScript Variable: Retrieves values from the JavaScript context on your page.
      • Data Layer Variable: Pulls values from the Data Layer, a key GTM feature for tracking custom events and information.
      • Constant: A fixed value you want to use in tags or triggers.
  5. Configure Your Variable:
    • Depending on the type of variable you selected, enter the required details. For example:
      • Data Layer Variable: Specify the Data Layer variable name.
      • JavaScript Variable: Enter the JavaScript variable name (e.g., window.someVariable).
  6. Save the Variable:
    • Click “Save” to create your variable.

4. Enabling Built-In Variables

Before using built-in variables, you may need to enable them:

  1. Go to Variables: Click on “Variables” in the left sidebar.
  2. Configure Built-In Variables: In the Built-In Variables section, click on “Configure.”
  3. Select Built-In Variables: Check the boxes next to the built-in variables you want to enable, such as Page URL, Click Element, or Form ID.

5. Using Variables in Tags and Triggers

Once you’ve created variables, you can use them in your tags and triggers:

  1. In Tags:
    • When configuring a tag (e.g., Google Analytics), you can reference variables to dynamically insert values. For instance, in the Google Analytics tag, you might use a variable to capture the page URL as the event label.
  2. In Triggers:
    • When setting up a trigger, you can use variables to define conditions for when a tag should fire. For example, you might set a trigger to fire only if the Page URL variable matches a specific value.

6. Managing Variables

To manage your variables effectively:

  1. View and Edit Variables:
    • Go to the “Variables” section in GTM to see a list of your user-defined variables.
    • Click on the name of a variable to edit its settings. Make necessary adjustments, then click “Save.”
  2. Delete a Variable:
    • To delete a variable, click on its name, then click on the three vertical dots in the top right corner and select “Delete.” Confirm the deletion.
  3. Organize Variables:
    • Use clear and descriptive names for your variables to keep them organized.
    • Consider grouping related variables with consistent naming conventions (e.g., “User – [Variable Name]” or “Event – [Variable Name]”).

7. Testing Variables

Before publishing changes, it’s crucial to test your variables:

  1. Click on “Preview”:
    • Activate the preview mode to test your GTM setup.
  2. Navigate Your Site:
    • Perform actions on your website that should trigger the variables.
  3. Check Debug Console:
    • The GTM debug console at the bottom of your screen will show whether the variables are capturing the correct values as you interact with the site.
  4. Verify Data in Analytics:
    • If the variables are tied to tags, check your analytics reports to ensure the correct data is being sent and recorded.

8. Best Practices for Using Variables

  • Descriptive Naming: Use clear and descriptive names for your variables to make them easily identifiable.
  • Limit Variable Types: Use only the variables you need to avoid clutter and confusion.
  • Regular Review: Periodically review your variables to ensure they are still relevant and being used correctly.
  • Utilize the Data Layer: If applicable, implement a Data Layer to manage complex data points effectively and dynamically pass information to GTM.

Conclusion

Creating and managing variables in Google Tag Manager is essential for customizing your tracking setup and ensuring accurate data collection. By following the steps outlined above, you can effectively create user-defined variables, enable built-in variables, and use them to enhance your tags and triggers. Regularly testing and reviewing your variables will help maintain a robust GTM configuration that accurately captures user interactions and informs your marketing strategies.