How to Create and manage variables in Google Tag Manager

Author:

Creating and managing variables in Google Tag Manager (GTM) allows you to dynamically capture and use data within your tags and triggers. Here’s a step-by-step guide on how to do this:

Creating Variables in Google Tag Manager

  1. Sign in to Google Tag Manager:
  2. Select a Container:
    • Choose the container where you want to create the variable.
  3. Navigate to Variables:
    • In the GTM dashboard, click on “Variables” in the left-hand menu.
    • Click the “New” button under the “User-Defined Variables” section.
  4. Configure the Variable:
    • Click on “Variable Configuration” to choose the type of variable you want to create. Common variable types include:
      • Built-In Variables: Pre-configured variables provided by GTM (e.g., Page URL, Click URL, Form ID).
      • User-Defined Variables: Custom variables that you create based on your specific needs. Examples include:
        • Constant: A fixed value.
        • Data Layer Variable: Pulls values from the data layer.
        • DOM Element: Extracts values from the Document Object Model.
        • First Party Cookie: Reads the value of a first-party cookie.
        • JavaScript Variable: Executes custom JavaScript to return a value.
        • URL: Extracts components from the page URL.
        • Auto-Event Variable: Useful for dynamic elements like click classes or IDs.
        • RegEx Table: Matches input against a table of regular expressions to return specified outputs.
  5. Set Up Variable Details:
    • Depending on the type of variable you select, fill in the required details. For example:
      • For a Data Layer Variable, specify the Data Layer Variable Name.
      • For a JavaScript Variable, enter the JavaScript code that returns the desired value.
  6. Save the Variable:
    • Click “Save” to add the variable to your container.

Managing Variables in Google Tag Manager

  1. View and Edit Variables:
    • In the GTM dashboard, navigate to “Variables”. Here, you’ll see a list of all built-in and user-defined variables.
    • Click on any variable to edit its configuration or update its settings.
  2. Enable Built-In Variables:
    • GTM provides several built-in variables that you can enable as needed.
    • In the “Variables” section, click “Configure” under the “Built-In Variables” section.
    • Check the boxes for the variables you want to enable.
  3. Delete Variables:
    • To delete a user-defined variable, click on the variable you want to remove.
    • Click on the three-dot menu (more actions) in the upper-right corner.
    • Select “Delete”.
  4. Use Variables in Tags and Triggers:
    • When configuring tags and triggers, you can use your variables to dynamically insert values.
    • In any tag or trigger configuration, click on the variable icon (looks like a Lego block) to insert a variable.
  5. Organize Variables with Folders:
    • To keep your variables organized, you can create folders. Click on “Folders” in the left-hand menu, then click “New” to create a folder.
    • Drag and drop variables into folders for better organization.

Additional Tips

  • Naming Conventions: Use clear and descriptive names for your variables to easily identify their purpose and usage.
  • Testing Variables: Use the “Preview” mode to test your variables and ensure they return the expected values.
  • Documentation: Document your variable configurations and logic to maintain clarity, especially useful for team collaboration.

By following these steps, you can effectively create, manage, and organize variables in Google Tag Manager, enhancing your ability to capture and use dynamic data in your tags and triggers.