Regex table variable in GTM: Everything you need to know

Note: Empower yourself and others by sharing this insightful article on LinkedIn or social media. Let’s build a well-informed community together. Need help? Reach out anytime. 

Introduction

 

Regex (Regular Expressions) are powerful tools for pattern matching and data extraction, essential in many programming and scripting languages. In Google Tag Manager (GTM), Regex enables advanced tracking setups by dynamically handling text-based data like URLs, page titles, and custom data layers, resulting in more flexible and accurate tracking.

Regex’s versatility is crucial for web analytics, allowing for complex rules that adapt to changing data inputs. This capability simplifies tasks such as categorizing pages, validating form inputs, and mapping event names, which would otherwise require extensive configurations.

In this blog, we’ll explore Regex table variables in GTM. From the basics of Regex syntax to practical examples, this guide will equip you with the knowledge to implement sophisticated tracking solutions.

What is a Regular Expression?

 

A Regular Expression, commonly known as Regex, is a sequence of characters that define a search pattern. This search pattern can be used for string matching within texts, such as finding specific patterns within strings, replacing substrings, or splitting text based on specific delimiters. Regex is widely used in various programming languages and tools due to its versatility and power in handling complex text processing tasks.

What is a Regex Table Variable?

A Regex Table Variable in Google Tag Manager (GTM) is a type of variable that allows you to map a set of input patterns to corresponding output values using regular expressions. This is particularly useful when you need to perform complex pattern matching and value substitution within your GTM setup. By leveraging Regex Table Variables, you can dynamically transform or categorize data based on predefined patterns, making your tracking more flexible and powerful.

Different Parts of the Variable Configuration

Configuring a Regex Table Variable in Google Tag Manager involves several key components. Understanding each part is essential for setting up a variable that accurately matches and maps input values to the desired outputs. Here’s a breakdown of the different parts of the variable configuration:

Input Variable

The Input Variable is the data source that the Regex Table Variable will evaluate. It can be any built-in or custom variable in GTM, such as a URL, page path, event name, or other text-based data.

Example Input Variables:
{{Page Path}}: The path portion of the URL.
{{Event Name}}: The name of the triggered event.
{{URL}}: The full URL of the page

Regex Table

The Regex Table consists of pairs of regex patterns and output values. Each row in the table defines a regex pattern to match the input variable and the corresponding value to return if the pattern matches.

Example Patterns and Outputs:

In the table above:

  • /blog/.* matches any URL starting with /blog/.
  • /products/.* matches any URL starting with /products/.
  • /support/.* matches any URL starting with /support/.
  • ^/home$ matches the exact URL path /home.
  • ^/about-us$ matches the exact URL path /about-us.
  • ^/contact$ matches the exact URL path /contact.
  • /category/(.*) captures and includes any text following /category/.
  • /product/(.*)/buy matches URLs for product purchase pages.

Set Default Value

The Default Value is the value that the variable will return if none of the regex patterns match the input variable. This ensures that the variable always provides a value, even when no patterns are matched.

Example Default Value: Other

Ignore Case

The Ignore Case option determines whether regex matching should be case-sensitive or not. Enabling this option allows the regex patterns to match input values regardless of case.

Example: If enabled, the pattern click_.* will match CLICK_EVENT and click_event.

Full Matches Only

The Full Matches Only option ensures that the entire input value must match the regex pattern for it to be considered a match. This is useful for more precise pattern matching.

Example Patterns:


In this table, the input value must exactly match the pattern for the corresponding output to be returned.

Enable Capture Groups and Replace Functionality

Capture Groups allow you to capture specific parts of the input value and use them in the output value. This is useful for dynamically constructing output values based on parts of the input.

Example Patterns with Capture Groups:

 

In this table:

/category/(.*) captures and includes any text following /category/.
/product/(.*)/buy captures the product ID or name in the URL and includes it in the output.

Some Use Cases Where the Regex Table Variable Can Be Used

URL Categorization: Categorize different sections of a website based on URL paths, such as blog posts, product pages, support pages, etc.

Campaign Parameter Normalization: Normalize campaign parameters to ensure consistent tracking across different marketing channels.

Dynamic Page Titles: Generate dynamic page titles based on URL patterns for enhanced SEO and user experience.

Form Field Validation: Validate and categorize form inputs based on regex patterns to ensure data quality.

Conclusion


Regex Table Variables in Google Tag Manager are an invaluable tool for managing complex pattern matching and data transformation tasks. By mastering the different parts of the configuration, you can build dynamic and flexible tracking setups that significantly improve your data collection and analysis capabilities.

With Regex Table Variables, you can efficiently categorize URLs, standardize event names, and normalize campaign parameters, among other applications. This level of sophistication in text processing within GTM allows you to streamline your workflows, reduce manual configuration efforts, and ensure consistent and accurate data across your digital properties.

Leveraging Regex Table Variables empowers you to adapt to evolving data patterns and maintain robust tracking setups that provide deeper insights into user behavior and campaign performance. Embrace this powerful feature to enhance your GTM implementations and take your analytics to the next level.

FAQs

Q.What are other places where regex can be used inside Google Tag Manager?
Regex can be used in various places within Google Tag Manager to enhance your tracking and data manipulation capabilities. Here are some key areas:
Trigger Conditions:

You can use regex in trigger conditions to specify complex matching rules for URLs, page paths, and other variables. For example, you can create a trigger that fires on all blog pages using a pattern like /blog/.*.

Tag Configuration:

In tag configuration, regex can help you dynamically set tag parameters based on URL patterns or other text variables. For instance, you can extract and use parts of a URL as tag parameters.

Variable Definitions:

Besides Regex Table Variables, regex can be used in Custom JavaScript variables to perform advanced string manipulations and validations before returning a value.

Lookup Tables:

Similar to Regex Table Variables, Lookup Table Variables can use regex to match input values against specific patterns and return corresponding outputs.

Filters in Built-in Variables:

When configuring built-in variables like Page URL or Page Path, you can apply regex filters to include or exclude certain values based on patterns.

Q.What are some commonly used regex symbols?
Regular expressions consist of various symbols that allow you to create powerful search patterns. Here are some commonly used regex symbols:

1. Basic Symbols:

  • .: Matches any single character except newline.
  • ^: Matches the beginning of a string.
  • $: Matches the end of a string.
  • *: Matches zero or more occurrences of the preceding element.
  • +: Matches one or more occurrences of the preceding element.
  • ?: Matches zero or one occurrence of the preceding element.
  • |: Acts as a logical OR between patterns.
  • \: Escapes a special character to treat it as a literal.

2. Character Classes:

  • [abc]: Matches any one of the characters a, b, or c.
  • [^abc]: Matches any character except a, b, or c.
  • [a-z]: Matches any lowercase letter.
  • [A-Z]: Matches any uppercase letter.
  • [0-9]: Matches any digit.
  • \d: Matches any digit (equivalent to [0-9]).
  • \w: Matches any word character (alphanumeric plus underscore).
  • \s: Matches any whitespace character.

3. Quantifiers:

  • {n}: Matches exactly n occurrences of the preceding element.
  • {n,}: Matches n or more occurrences of the preceding element.
  • {n,m}: Matches between n and m occurrences of the preceding element.

4. Groups and Lookarounds:

  • (…): Groups multiple tokens together and remembers the matched text.
  • (?:…): Groups multiple tokens together without remembering the matched text (non-capturing group).
  • (?=…): Positive lookahead assertion, matches if the pattern inside parentheses can be matched to the right of the current position.
  • (?!…): Negative lookahead assertion, matches if the pattern inside parentheses cannot be matched to the right of the current position.

By understanding and utilizing these symbols, you can construct complex and precise regex patterns for various uses within Google Tag Manager, enhancing your tracking and data manipulation capabilities.

About Author

Rahul is a Digital Analytics Developer with 2+ years of experience crafting web and app tracking solutions. I leverage the Google Stack (GA4, Firebase, GTM, Looker Studio) to collect, analyze, and visualize data. My expertise extends to Customer Data Platforms (Rudderstack, Segments) and Mobile Measurement Platforms (AppsFlyer, Singular), along with similar third-party tools, ensuring adaptability to project requirements. I am skilled in implementing server-side tracking for comprehensive data capture and utilize BigQuery for efficient data warehousing and advanced analytics. My focus aligns with maximizing digital business performance by extracting actionable insights to optimize user journeys and conversions.

Share This Article:
Facebook
Twitter
LinkedIn
Email
Table of content

Related Stories

Frustrated by 'Not Set' appearing in your Google Analytics reports? Discover the common causes behind this...
Unlock the full potential of your Calendly integration with enhanced conversion tracking using Google Tag Manager....
Unlock the full potential of your website with our step-by-step guide on adding Facebook Pixel using...
Learn how to efficiently store data in local storage using Google Tag Manager with our comprehensive...