How to use Regex Tables for Advanced Tag Management in Google Tag Manager

Author:

Using regex tables in Google Tag Manager (GTM) can significantly enhance your ability to manage tags with advanced conditions and mappings. Regex (regular expressions) allows you to create flexible and powerful patterns for matching data. Here’s a detailed guide on how to use regex tables for advanced tag management in GTM:

 1. Access Google Tag Manager

  1. Sign in to Google Tag Manager:
  2. Select Your Container:
    • Choose the container where you want to implement the regex table or create a new one if you don’t have a container set up yet.

 2. Enable Built-In Variables

To work effectively with regex tables, you might need certain built-in variables to be enabled.

  1. Navigate to Variables:
    • In the GTM workspace, click on Variables in the left-hand menu.
  2. Enable Click Variables:
    • Click Configure under Built-In Variables.
    • Ensure variables such as Click URL, Click Text, Page URL, and other relevant variables are checked. These will be used as input values in your regex table.

 3. Create a Regex Table Variable

  1. Add a New User-Defined Variable:
    • Click on New under User-Defined Variables.
  2. Choose Variable Type:
    • Click Variable Configuration.
    • Select Lookup Table.
  3. Configure the Lookup Table:
    • Name your variable (e.g., “Advanced Regex Lookup Table”).
  4. Define Input Variable:
    • Select the input variable (e.g., Page URL or Click URL).
  5. Add Lookup Table Rows:
    • Add rows for each regex pattern you want to use. In the Input column, enter your regex patterns. In the Output column, enter the values you want to map to.
  6. Enable Regex Matching:
    • Check the box that says Enable regex matching.
  7. Save the Variable:
    • Click Save to save your regex table variable.

Example Regex Table Configuration:

Input (Regex Pattern) Output
^/products/.* Products Page
^/services/.* Services Page
facebook.com Social Media
twitter.com Social Media
instagram.com Social Media
^(?!.yourdomain.com).$ External Link

 4. Use the Regex Table Variable in a Tag

  1. Create or Edit a Tag:
    • Navigate to Tags in the left-hand menu.
    • Click on New to create a new tag or select an existing tag to edit.
  2. Set Up Tag Configuration:
    • Click on Tag Configuration and select the type of tag you want to configure (e.g., Google Analytics: GA4 Event or Universal Analytics).
  3. Use the Regex Table Variable:
    • In the tag configuration fields, use the regex table variable. For example, you can set the event category, action, or label to use the regex table variable by selecting it from the variable list.

Example for Google Analytics Tag (GA4):

  • Event Name: page_category
  • Event Parameters:
    • Parameter Name: category_name, Value: {{Advanced Regex Lookup Table}}

 5. Test Your Setup

  1. Enter Preview Mode:
    • Click Preview in the GTM workspace to enter preview mode.
    • Enter your website’s URL in the prompt to open your site with GTM’s debug pane enabled.
  2. Test the Tags:
    • Navigate through your website and perform actions that should trigger your tags.
    • In the GTM debug pane, check that the regex table variable resolves correctly and that the tag fires with the expected values.
  3. Verify in Google Analytics:
    • Go to Google Analytics to verify that the events are being tracked correctly.
    • For GA4, check the Events section under Realtime.
    • For Universal Analytics, go to Real-Time > Events.

 6. Publish Your Changes

  1. Submit Your Changes:
    • Once you’re satisfied with the setup and have thoroughly tested it, click Submit in GTM.
  2. Publish Your Container:
    • Add a version name and description, then click Publish to make your changes live.

Example Use Cases for Regex Tables:

Use Case 1: Tracking Specific Page Categories

You want to track different sections of your site, such as product and service pages.

  1. Regex Patterns:
    • /products/.* to match any URL under the products section.
    • /services/.* to match any URL under the services section.
  2. Outputs:
    • Products Page
    • Services Page
  3. Implementation:
    • Use these patterns in your regex table and map them to meaningful names.
    • In your Google Analytics tag, set the event parameter page_category to use the regex table variable.

Use Case 2: Custom Event Tracking for Outbound Links

You want to differentiate between clicks on social media links and other external links.

  1. Regex Patterns:
    • facebook\.com
    • twitter\.com
    • instagram\.com
    • ^(?!.*yourdomain\.com).*$ to match any external link not containing your domain.
  2. Outputs:
    • Social Media
    • External Link
  3. Implementation:
    • Use these patterns in your regex table.
    • In your event tracking tag, set a custom event parameter (e.g., link_type) to use the regex table variable.

Tips for Using Regex Tables:

  • Testing Patterns:
    • Use tools like regex101.com to test and validate your regex patterns before implementing them in GTM. This can help ensure that your patterns are correct and will match the expected input values.
  • Complex Mappings:
    • If you have complex requirements, consider breaking down your regex patterns into simpler ones and using multiple regex tables. This approach can make your setup easier to manage and debug.
  • Performance Considerations:
    • Ensure your regex patterns are efficient. Poorly optimized patterns can slow down your tracking setup, especially if they are applied to high-traffic variables like Page URL.
  • Documentation:
    • Keep good documentation of your regex patterns and what they are intended to match. This will help you and others understand the purpose of each pattern and make maintenance easier.

By following these detailed steps and tips, you can effectively use regex tables in Google Tag Manager to handle advanced tag management scenarios. This will enable more precise and flexible data collection, allowing you to gain deeper insights into user behavior on your website.