GTM Regex Table Variable Explained

This article is in conjunction with the article Understanding Lookup Table Variable in Google Tag Manager where I explained the concept of lookup table variables in great detail.

The Regex table variable in GTM is very much similar to the lookup table variable. Make sure you understand, how a lookup table variable works before you decide to use the more advanced regex table variable.

Introduction to GTM Regex Table Variable

A Regex table variable is a user-defined variable in Google Tag ManagerIt is used to determine whether or not there is a match with an input pattern and if there is a match then return the corresponding output value. If there is no match then return ‘undefined’ (unless the default value has been set).

By using Regex table variables you can greatly reduce the number of tags required to send hit data (esp. ‘event’ data) to Google Analytics and you also do not need to create very big lookup tables.

The word ‘Regex’ in the Regex table variable denotes Regular Expression.

Unlike the lookup table variable, you need a good working knowledge of regular expressions in order to effectively use a regex table variable. It is the use of regular expressions which makes a regex table variable so powerful.

If you are brand new to regex then read this article: Google Analytics and Google Tag Manager Regex (Regular Expressions) Guide

I have written this article under the assumption that you are already familiar with regex.

A regex table variable is made up of the Input variable and Regex table:

GTM Regex Table Variable

The Regex table is made up of one or more rows and two columns.

You can add as many rows as you like to a Regex table. However, the number of columns in the Regex table will always remain two.

The first column of the Regex table is labelled ‘Input’ and is used to specify ‘Input Patterns’. You can use any string, GTM variable or regular expression as an input pattern.

The second column of the lookup table is labelled ‘Output’ and is used to specify ‘Output values’ for the corresponding input pattern.

You can reference/call any GTM variable (including another lookup/regex table variable) from within the output value field. You can also reference the regex table variable in custom HTML tags and custom JavaScript variables.

The corresponding output value of the first matched pattern is the value returned by a regex table variable.

It is important to remember that by default this match needs to be a full match and case insensitive for regex table variable to return a value other than ‘undefined’.

Let us use the regex table variable for content grouping. This will give you a good idea of its usage and power.

Traditionally when we set up content grouping via Google Tag Manager, we use data layers to collect content group data and then send that data to GTM.

This method usually requires the help of a web developer who sends content group data to your data layers. But by using a Regex table variable, you can set up content groups without using data layers.

Setting up content groups via Regex and lookup table variables

Following is the 10000-foot view of the whole process:

Step-1: Decide the names and number of content categories which you will use as ‘content group’.

Step-2: Identify all of the web pages which will be part of each ‘content group’.

Step-3: Setup Content groups in your main GA view and note down their index numbers.

Step-4: Create a Regex Table variable that pulls data from your website for each content group.

Step-5: Test the Regex Table variable.

Step-6: Create a Lookup Table variable that dynamically assigns an index number to each content group.

Step-7: Test the Lookup Table variable.

Step-8: Edit the tag you use to send pageview data to GA for sending content group data.

Step-9: Test the tag you used to send content group data to Google Analytics.

Step-10: Publish your container and view results.

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

Step-1: Decide the names and number of content categories which you will use as ‘content group’

Let us suppose we are going to set up the following five content groups for a blog:

  1. Attribution Modelling – it groups all of the articles related to attribution modelling.
  2. Google Tag Manager – it groups all of the articles related to GTM.
  3. Google Analytics – it groups all of the articles related to GA.
  4. Facebook – It groups all of the articles related to Facebook.
  5. Other – it groups all of the articles which are not related to ‘Attribution Modelling’, ‘Google Tag Manager’, ‘Google Analytics’ and ‘Facebook’.

Note: If you are brand new to the concept of ‘content groups’ then read this article: Introduction to Content Grouping in Google Analytics

Step-2: Identify all of the web pages which will be part of each ‘content group’

For example, I want all of the web pages whose URL contain any of the following words to be a part of the ‘Attribution Modelling’ content group:

  • attribution
  • model
  • online
  • offline
  • nonline

I want all of the web pages whose URL contains the string ‘google-tag-manager’ to be a part of the ‘Google Tag Manager’ content group.

I want all of the web pages whose URL contains the string ‘google-analytics’ to be a part of the ‘Google Analytics’ content group.

Similarly, I want all of the web pages whose URL contains the string ‘facebook’ to be a part of the ‘Facebook’ content group.

Step-3: Setup content groups in your main GA view and note down their index numbers

Now since I already know the name and number of content groups I need, I set them up in my Google Analytics main reporting view like the one below:

content grouping google analytics

Note: I created the five content groups in Google Analytics by using the ‘Group by tracking code method’.

To learn more about this method, read this article: Creating Content Group in Google Analytics via tracking code

Step-4: Create a Regex table variable that pulls data from your website for each content group

#1 Log into your GTM account and then click on the ‘Variables’ link in the left-hand side menu:

variables gtm menu

#2 Scroll down to ‘User-Defined Variables’ section and then click on the ‘New’ button:

new user defined variable

#3 Select ‘Regex Table’ from the ‘Choose Variable Type’ menu:

choose variable type menu

#4 Enter a name for your Regex table variable. You will later reference the variable by this name. I named the regex table variable as ‘Content Group’:

content group

#5 Select the Input variable from the drop-down menu. I selected ‘{{Page Path}}’ as input variable:

page path input variable

Note: If your input variable does not already exist then create a new one. You need an input variable in order to use a regex table variable.

#6 Enter input patterns and their corresponding output values in the regex table, row by row, from top to bottom like the one below:

enter input patterns 1

From the screenshot above we can conclude the following:

# If ‘page path’ contains any of the following words then make the corresponding web page, part of the ‘Attribution Modelling’ content group:

  • attribution
  • model
  • online
  • offline
  • nonline

Note: The pipe symbol (|) separating the two words corresponds to the Logical OR operation.

#  If ‘page path’ contains ‘google-tag-manager’ then make the corresponding web page, part of the ‘Google Tag Manager’ content group.

#  If ‘page path’ contains ‘google-analytics’ then make the corresponding web page, part of the ‘Google Analytics’ content group.

#  If ‘page path’ contains ‘facebook’ then make the corresponding web page, part of the ‘Facebook’ content group.

#7 Specify the default value for your regex table variable by clicking on the ‘Set Default Value’ checkbox and then entering the default value:

set default value 1

If the default value has not been set and no match is found then the regex table variable returns ‘undefined’.

Here what I am doing is that if the ‘page path’ does not match any of the specified input patterns then make the corresponding web page part of the ‘Other’ content group.

Later I will set up a lookup table variable to complete this task.

#8 Under ‘Advanced Settings’ uncheck the following two checkboxes:

  • Full Matches Only
  • Enable Capture Groups and Replace Functionality
advanced settings regex table variable

By default, the regex table variable uses full match which won’t allow us to do a partial match. So I unchecked the ‘Full Matches Only‘ checkbox.

GTM recommend not to use ‘Enable Capture Groups and Replace Functionality‘ if the ‘Full Matches Only’ checkbox is disabled/unchecked. For this reason, I unchecked the ‘Enable Capture Groups and Replace Functionality‘ checkbox.

By default, the regex table variable does case insensitive matchIf you want the regex table variable to take the case into account then uncheck the ‘Ignore Case’ checkbox.

#9 Click on the ‘Save’ button to save the new regex table variable.

Step-5: Test the Regex table variable

#1 Click on the ‘Preview’ button on the top right-hand side of your GTM account to put your container in the preview mode.

#2 Navigate to an article page on your website and then scroll down to see the GTM preview and debug window.

#3 Select the event ‘DOM Ready’ (under the ‘summary’ section) and then click on the ‘Variables’ tab:

gtm debug window1

#4 Scroll down until you find your new regex table variable:

regex table gtm debug window

Make sure that your regex table variable returns a value other than ‘undefined’ and the value it returns is relevant to the page you are on.

For example, if you want all of the web pages whose URL contain the string ‘google-analytics’ to be a part of the ‘Google Analytics’ content group and you are on the page whose URL does contain the string ‘google-analytics’ then the value returned by your regex table variable should be ‘Google Analytics’ (see the screenshot above).

Likewise, navigate to different web pages on your site and ensure that the regex table variable returns a value other than ‘undefined’ and the value it returns is relevant to the page you are on.

Step-6: Create a lookup table variable that dynamically assigns an index number to each content group

#1 Create a new lookup table variable.

We are going to use this variable to dynamically assign an index number to each content group.

In the context of ‘Content Grouping’, an index number can be any number from 1 to 5.

Google Analytics internally identify a ‘content grouping’ through its index number and not by its name.

Since in GA, you can create only five ‘content groupings’ per reporting view, the value of index number can only be one of the numbers from 1 to 5.

To learn more about index numbers, read this article: Creating Content Group in Google Analytics via tracking code

#2 Select the regex variable we created earlier as ‘input variable’.

I selected the ‘content group’ regex table variable as the input variable from the drop-down menu:

content group input variable

#3  Enter input patterns and their corresponding output values in the lookup table, row by row, from top to bottom like the one below:

enter input patterns2 1

From the screenshot above we can conclude the following:

  • 3.1 If the ‘content group’ input variable returns ‘Attribution Modelling’ then set the index number to 1.
  • 3.2 If the input variable returns ‘Google Tag Manager’ then sets the index number to 2.
  • 3.3 If the input variable returns ‘Google Analytics’ then set the index number to 3.
  • 3.4 If the input variable returns ‘Facebook’ then set the index number to 4.
  • 3.5 If the input variable returns ‘Other’ then set the index number to 5.

#4 Click on the ‘Save’ button to save the new lookup table variable.

Step-7: Test the lookup table variable

#1 Click on the ‘Refresh’ button next to the ‘Leave Preview Mode’ link in your GTM account:

refresh preview mode

#2 Navigate to an article page on your website and then scroll down to see the GTM preview and debug window.

#3 Select the event ‘DOM Ready’ (under the ‘summary’ section) and then click on the ‘Variables’ tab:

gtm debug window1

#4  Scroll down until you find your new regex and lookup table variables:

regex lookup table variables

Make sure that your lookup table variable returns a value other than ‘undefined’ and the value it returns is relevant to the corresponding content group.

For example from the screenshot above, it is evident that for the ‘Google Analytics’ content group the index number is 3 (which is how it should be).

Likewise, navigate to different web pages on your site and ensure that your lookup table variable returns a value other than ‘undefined’ and the value it returns is relevant to the corresponding content group.

Step-8: Edit the tag you use to send pageview data to GA for sending content group data

#1 In your GTM account, navigate to the tag you use to send pageview data to your Google Analytics property. This is the same tag you use to deploy Google Analytics tracking on your website.

#2 Edit this tag to set up ‘content groups’ like the one below:

content groups ga tag

# I am calling the {{Index Content Group}} lookup table variable in the ‘Index’ field. When this variable is called, it returns an index number.

# I am calling the {{Content Group}} regex table variable in the ‘Content Group’ field. When this variable is called, it returns the name of a ‘content group’.

Step-9: Test the tag you used to send content group data to Google Analytics

#1 Click on the ‘Refresh’ button next to the ‘Leave Preview Mode’ link one more time.

#2  Navigate to an article page on your website and then scroll down to see the GTM preview and debug window.

#3  Click on the tag you just edited for sending content group data to Google Analytics:

click on tag

#4 Check the value of the content groups field:

content groups field value

From the screenshot above, it is evident that the tag is sending correct content group data to Google Analytics.

Step-10: Publish your container and view results

#1 Click on the ‘Leave Preview Mode’ link in your GTM account and then publish your container.

#2 Wait for 10-20 minutes and then navigate to ‘All Pages’ report (under ‘Behavior’ > ‘Site Content’) in your Google Analytics view:

all pages report

#3 Select your ‘content grouping’ from the content grouping drop-down menu:

content grouping menu

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade