How to use regex for advanced URL matching in Google Tag Manager

Author:

Using regular expressions (regex) in Google Tag Manager (GTM) for advanced URL matching allows you to create more flexible and precise triggers and variables. Regex is particularly useful for matching patterns in URLs that are dynamic or follow a specific structure. By utilizing regex, you can create triggers that fire only on specific types of pages or interactions, even when the URL includes variables, query parameters, or dynamic elements.

In this guide, we’ll walk through how to use regex for advanced URL matching in GTM.

What is Regex?

Regular expressions (regex) are a sequence of characters that form a search pattern. In GTM, regex can be used to match strings (like URLs or page paths) that meet specific patterns or conditions. It is a powerful tool that can be used to match complex URLs that contain dynamic or variable elements, such as query parameters, subdomains, or page numbers.

Step 1: Basic Setup in Google Tag Manager

Before you use regex for URL matching, ensure you have GTM set up on your website.

  1. Sign up and create an account: If you haven’t already, sign up for GTM and create a container for your website.
  2. Install GTM: Once you create the container, Google Tag Manager will provide two snippets of code. These should be added to your website to enable tracking and use of GTM.

Step 2: Create or Edit a Trigger Using Regex

  1. Access Google Tag Manager: Log into your GTM account and open the container associated with your website.
  2. Create a New Trigger: On the left panel, click on Triggers and then click New to create a new trigger. Alternatively, you can edit an existing trigger that uses URL matching.
  3. Choose Trigger Type: Choose Page View (for tracking page loads) or any other trigger type that fits your needs, such as Click for tracking button clicks.
  4. Set up URL Matching with Regex:
    • Page URL Variable: In GTM, the variable used for URL matching is typically the Page URL variable, which is automatically available in GTM.
    • To use regex, you’ll need to choose Some Page Views under the trigger configuration. Then, choose the Page URL variable and set the condition to match using regex.

Example 1: Match Exact URLs

For URLs that match a specific pattern, you can use a simple regex like this:

  • Regex: ^/about-us$
  • Explanation: This matches exactly the /about-us path and will not match any other variations or additional query parameters.

Example 2: Match Multiple Pages with a Common Structure

If you want to match URLs that contain a certain structure but have different dynamic elements (e.g., product pages or categories), you can use regex like:

  • Regex: ^/products/[a-zA-Z0-9_-]+$
  • Explanation: This matches any URL that starts with /products/ followed by alphanumeric characters, underscores, or hyphens. For example, /products/shoes, /products/smartphone-123, etc.

Example 3: Match URLs with Query Parameters

If you want to match URLs that contain specific query parameters, regex is useful for capturing these patterns. For example, to match any URL that contains the parameter utm_campaign:

  • Regex: \?utm_campaign=.*
  • Explanation: This matches any URL that has ?utm_campaign= followed by any sequence of characters (i.e., any value for the utm_campaign parameter).

Example 4: Match URLs with Optional Query Parameters

You can also use regex to match URLs with optional query parameters. For instance, if you want to match all URLs that either have or don’t have a ref parameter:

  • Regex: ^/landing-page(\?ref=[a-zA-Z0-9_-]+)?$
  • Explanation: This matches the /landing-page path with or without the ref parameter. The (\?ref=[a-zA-Z0-9_-]+)? portion makes the ref parameter optional.

Step 3: Using Regex in GTM Triggers

To apply regex in your triggers, follow these steps:

  1. Open the Trigger Configuration: If you’re creating a new trigger, after selecting the trigger type (e.g., Page View or Click), click on the Choose a variable dropdown.
  2. Select “Page URL”: For page-related triggers, select Page URL as the variable. For click-related triggers, you can use Click URL or other click-related variables depending on your needs.
  3. Select “Matches Regex”: From the condition dropdown, select Matches Regex. This tells GTM that you are using a regular expression for pattern matching.
  4. Enter Your Regex Pattern: In the text field, enter your regular expression (for example, the patterns shown in the examples above).
  5. Save the Trigger: Once your regex is set up, click Save.

Step 4: Create a Tag to Fire Based on Your Trigger

After setting up the trigger, you can create a tag that fires when the trigger condition is met.

  1. Create a New Tag: In the left sidebar, click Tags and then New.
  2. Choose Tag Type: Select a tag type, such as Google Analytics or Custom HTML depending on your needs.
  3. Configure Tag: Set up your tag, specifying details like event category, action, or any other necessary information (for example, track page views in Google Analytics).
  4. Set Trigger for Tag: Under the Triggering section, select the trigger you created using regex.
  5. Save and Publish: Once you’re satisfied with the configuration, click Save and Publish to apply the changes.

Step 5: Testing Your Regex Setup

Before your tag is live, it’s important to test if everything works correctly.

  1. Preview Mode: Click Preview in GTM to enter debug mode. Enter the URL of your website and verify that the trigger fires as expected when visiting URLs that match your regex.
  2. Use GTM Debug Panel: Once in preview mode, you can interact with the website. In the GTM debug panel at the bottom of the screen, you’ll see a list of tags that have fired based on the triggers you’ve set up.
  3. Use Google Analytics Real-Time Reports: If you’re using Google Analytics, you can also monitor the Real-Time events to see if your tag is firing when expected.

Step 6: Advanced Regex Examples

Here are some advanced regex patterns you can use in Google Tag Manager:

Example 1: Match URL with Multiple Parameters

If you want to match URLs with multiple query parameters, such as utm_source and utm_medium:

  • Regex: \?utm_source=[a-zA-Z0-9_-]+&utm_medium=[a-zA-Z0-9_-]+
  • Explanation: This will match URLs like /page?utm_source=google&utm_medium=cpc.

Example 2: Match URLs with a Specific File Extension

To match URLs ending in .pdf or .jpg:

  • Regex: \.(pdf|jpg)$
  • Explanation: This will match any URL that ends with .pdf or .jpg.

Conclusion

Regex in Google Tag Manager provides powerful capabilities for tracking and managing events based on complex URL patterns. Whether you’re trying to match a specific page path, query parameters, or a combination of dynamic URL elements, regex can help you capture those interactions with precision. By understanding and applying regex correctly, you can create highly customized triggers that enhance your tracking and analytics setup in GTM.