How to use regex tables for advanced tag management in Google Tag Manager

Author:

Using regex tables for advanced tag management in Google Tag Manager (GTM) can significantly enhance your ability to control when and how tags are fired based on specific conditions. Regular expressions (regex) allow you to create more dynamic, flexible, and complex triggers and variables. Here’s a detailed guide on how to use regex tables for advanced tag management in GTM.

1. Understanding Regex and Regex Tables

Before diving into GTM, it’s essential to understand what regex is and how it can be applied to tag management:

  • Regex (Regular Expressions) is a powerful tool for pattern matching within strings. It allows you to search for and match strings based on patterns rather than exact text.
  • Regex Tables in GTM allow you to create a rule-based structure where regex is applied to a set of conditions or URL patterns, making the tag firing logic much more robust.

A Regex Table is typically used in GTM to create conditions based on URL, referrer, or other variables. This allows GTM to determine the appropriate action (e.g., firing a tag) based on how specific patterns or conditions are met.

2. Use Cases for Regex Tables in GTM

You can use regex tables for various purposes, such as:

  • Firing tags based on URL patterns: For instance, firing a tag only on product pages, checkout pages, or specific sections of a site.
  • Capturing dynamic parameters from URLs: You can extract URL parameters like “user_id” or “product_category” dynamically and use them in tags or triggers.
  • Excluding certain pages or actions: Preventing tags from firing on specific pages or under certain conditions.
  • Customizing triggers based on referrer: Firing tags only if a user comes from a specific referral URL.

3. Setting Up Regex Tables in GTM

Let’s walk through a practical example of how to use regex tables in Google Tag Manager.

Step 1: Create a New Variable

  1. Go to your GTM container and click on Variables in the left-hand menu.
  2. Click New to create a new variable. This variable will store the value that will be evaluated against your regex table.
  3. Choose URL (or another appropriate built-in variable such as Referrer, Page Path, etc.).
  4. Select the Variable Type (e.g., “Page URL” or “Page Path”) based on what you want to track.
  5. Save the variable.

Step 2: Add a Regex Table

  1. Create a New Trigger to fire tags based on the regex table.
  2. In the Trigger Configuration, select Page View (or whichever trigger type you require) as the trigger event.
  3. Click on the Choose Trigger Type dropdown and select Page URL (or another built-in variable if using something else like Referrer).
  4. In the condition section, instead of using the regular “equals” or “contains” operators, select Matches Regex.

Step 3: Use Regex Table for Advanced Matching

  1. Create a Custom JavaScript Variable for the Regex Table:
    • Go to Variables and click on New.
    • Choose Custom JavaScript as the variable type.
    • In the code editor, add the following example code:
    javascript
    function() {
    var url = {{Page URL}};
    var regexTable = [
    { regex: /^\/product\/(.*)$/, value: 'ProductPage' }, // Match product pages
    { regex: /^\/checkout\//, value: 'CheckoutPage' }, // Match checkout pages
    { regex: /\/category\/([^/]+)\//, value: 'CategoryPage' } // Match category pages
    ];
    for (var i = 0; i < regexTable.length; i++) {
    if (url.match(regexTable[i].regex)) {
    return regexTable[i].value;
    }
    }
    return 'OtherPage';
    }

    This code uses a regex table to match URLs to different categories like product pages, checkout pages, and category pages.

  2. Test the Regex Table: Before using it in triggers, you can preview the variable in GTM to ensure it returns the correct value based on your URL structure.

Step 4: Use the Regex Table in Triggers

  1. Create a New Trigger:
    • Go to Triggers and click New.
    • Select the Page View trigger type.
    • Set up the trigger condition using your Custom JavaScript Variable. For example:
      • Variable: The custom variable you created in Step 3.
      • Condition: “equals” or “contains” the value you’re looking for (e.g., “ProductPage” or “CheckoutPage”).
  2. Apply the Trigger to a Tag:
    • Once the trigger is created, go to the Tags section.
    • Select the tag you want to fire.
    • Under Triggering, select the newly created trigger based on your regex logic.

4. Advanced Examples of Regex Table Use in GTM

Example 1: Firing Tags on Product and Category Pages

Let’s say you want to fire a specific tag only on pages that belong to a certain category or product. You can use a regex table to check the URL:

javascript
function() {
var url = {{Page URL}};
var regexTable = [
{ regex: /^\/products\/(.*)$/, value: 'ProductPage' }, // Matches product pages
{ regex: /^\/category\/([^\/]+)$/, value: 'CategoryPage' } // Matches category pages
];
for (var i = 0; i < regexTable.length; i++) {
if (url.match(regexTable[i].regex)) {
return regexTable[i].value;
}
}
return 'OtherPage';
}

This will allow you to fire tags only on product and category pages, enhancing the accuracy and relevance of your tag management.

Example 2: Tracking Specific UTM Parameters

If you want to fire a tag based on specific UTM parameters in the URL, you can create a regex table to match various UTM conditions:

javascript
function() {
var url = {{Page URL}};
var regexTable = [
{ regex: /[?&]utm_source=google/, value: 'GoogleTraffic' },
{ regex: /[?&]utm_medium=cpc/, value: 'CPCTraffic' },
{ regex: /[?&]utm_campaign=spring_sale/, value: 'SpringSale' }
];
for (var i = 0; i < regexTable.length; i++) {
if (url.match(regexTable[i].regex)) {
return regexTable[i].value;
}
}
return 'OtherTraffic';
}

This will match different UTM parameters like utm_source, utm_medium, and utm_campaign, allowing you to fire specific tags based on the traffic source.

5. Testing and Debugging

Testing is crucial when using regex tables in GTM:

  1. Use GTM’s Preview Mode: Before publishing any changes, always preview the container in GTM’s preview mode to see if your tags are firing correctly based on the regex conditions.
  2. Use the GTM Debugger: You can use the Google Tag Manager Debug Console to inspect the variable values and see if the regex matches the expected patterns.
  3. Test Regex Patterns: Tools like Regex101 or RegExr allow you to test your regex patterns outside GTM to ensure they match the URLs correctly.

6. Conclusion

Using regex tables in Google Tag Manager is an excellent way to create dynamic and powerful tag management rules. By applying regex patterns to variables like URLs, referrers, and custom parameters, you can create highly targeted triggers that fire tags only under the right conditions. The flexibility of regex combined with GTM’s user-friendly interface provides an effective solution for managing complex tag firing requirements.