Creating and managing variables in Google Tag Manager (GTM) is a crucial part of tracking user interactions on your website. Variables are used to store information that can be used by tags and triggers, allowing for more dynamic and context-specific data tracking. Here’s a step-by-step guide on how to create and manage variables in GTM.
1. Accessing Google Tag Manager
Step 1: Log In to GTM
- Go to the Google Tag Manager website.
- Log in with your Google account.
- Select the appropriate GTM account and container associated with your website.
2. Understanding Variables
Variables in GTM serve as placeholders for dynamic values that you can use in your tags and triggers. There are three main types of variables:
- Built-in Variables: Predefined variables provided by GTM for common tracking needs, such as Page URL, Click Classes, and more.
- User-Defined Variables: Custom variables created by users to capture specific data.
- Data Layer Variables: Variables that pull data from the data layer, which is a JavaScript object used to store and manage data for GTM.
3. Creating a New Variable
Step 1: Navigate to Variables
- In the left sidebar, click on “Variables.”
- This will show you a list of existing variables in your container.
Step 2: Create a New Variable
- Click the “New” button to create a new variable.
- A variable configuration screen will open.
Step 3: Configure the Variable
- Name Your Variable: Click on the “Untitled Variable” field and give your variable a descriptive name (e.g., “User ID” or “Click URL”).
- Select Variable Type:
- Click on “Variable Configuration.”
- Choose the type of variable you want to create:
- Auto-Event Variable: Captures data from user interactions, such as clicks or form submissions.
- Constant Variable: Holds a static value you define (e.g., a specific string or number).
- Data Layer Variable: Retrieves values from the data layer.
- JavaScript Variable: Fetches values from a global JavaScript variable on your site.
- Lookup Table: Maps input values to output values.
- First Party Cookie: Reads values from cookies set by your domain.
- Set Variable Settings: Based on the variable type you selected:
- For Data Layer Variables:
- Enter the name of the data layer variable you want to capture (e.g.,
userId
).
- Enter the name of the data layer variable you want to capture (e.g.,
- For Lookup Table Variables:
- Define the input values and corresponding output values.
- For Auto-Event Variables:
- Choose the type of interaction you want to track (e.g., Click Element, Click Classes).
- For Data Layer Variables:
4. Using Built-In Variables
GTM includes several built-in variables that can be enabled for immediate use. Here’s how to activate them:
Step 1: Enable Built-In Variables
- In the “Variables” section, look for the “Built-In Variables” section.
- Click on the “Configure” button.
- Check the boxes next to the built-in variables you want to enable (e.g., Page URL, Click Classes).
5. Testing Your Variables
Before using your variables in tags and triggers, it’s important to test them to ensure they capture the intended values.
Step 1: Enter Preview Mode
- In the top right corner, click the “Preview” button.
- Enter your website URL to open it in a new tab with GTM preview mode activated.
Step 2: Check Variable Values
- Navigate through your website while the GTM debug console is open.
- Trigger the conditions you set for your variables.
- Check the console to see if your variable captures the correct values.
6. Managing Variables
Step 1: Edit Existing Variables
- Navigate to the “Variables” section in the GTM workspace.
- Click on the variable you want to edit.
- Make your changes in the variable configuration.
- Remember to save your changes and test the variable again before using it in tags or triggers.
Step 2: Disable or Delete Variables
- To disable a variable, click on the variable and change its status to “Disabled.”
- To delete a variable, click on the variable and select the “Delete” option in the upper right corner.
7. Organizing Variables
As you create more variables, organizing them can help you manage your GTM setup more efficiently.
Step 1: Use Naming Conventions
- Use clear and consistent naming conventions for your variables to quickly identify their purpose (e.g., “Page URL” or “Product ID”).
Step 2: Add Descriptions
- Provide descriptions for each variable, explaining its purpose and configuration. This helps when revisiting the container after some time.
8. Version Control
GTM allows you to track changes made to your variables through versions.
Step 1: View Version History
- Click on “Versions” in the left sidebar to see a history of all changes made to your container.
Step 2: Revert to Previous Versions
- If necessary, you can revert to a previous version by selecting it and choosing the option to publish it.
9. Best Practices for Variable Management
- Test Before Publishing: Always preview and test variables before making them live to avoid errors.
- Document Changes: Keep a log of changes made to variables for future reference.
- Regular Audits: Periodically audit your variables to ensure they’re functioning correctly and remove any that are no longer needed.
Conclusion
Creating and managing variables in Google Tag Manager is essential for effectively capturing and using data from user interactions on your website. By following the steps outlined above, you can set up variables that provide dynamic values for your tags and triggers, enhancing your tracking capabilities. With practice and adherence to best practices, GTM can significantly improve your data tracking and overall marketing efforts.