Understanding Regex Table Variable in Google Tag Manager

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.

So make sure you understand, how a lookup table variable works before you decide to use the more advanced regex table variable.

A Regex table variable is a user defined variable in Google Tag Manager.

It is used to determine whether or not there is a match with an input pattern and if there is 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 Regex table variable denotes Regular Expression.

Unlike 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: Regular Expressions Guide for SEO, Google Analytics & Google Tag Manager

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

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

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 corresponding input pattern.

Get the E-Book (62 Pages)

You can reference/call any GTM variable (including another lookup / regex table variable) from within the output value field.

You can also reference 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 an full match and case insensitive for regex table variable to return a value other than ‘undefined’.

 

Let us use 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 require the help of a web developer who send content group data to your data layers.

But by using a Regex table variable, you can setup 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 which pull data from your website for each content group.

Step-5: Test the Regex Table variable.

Step-6: Create a Lookup Table variable which dynamically assign 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.

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 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 contain 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 contain the string ‘google-analytics’ to be a part of the ‘Google Analytics’ content group.

Similarly, I want all of the web pages whose URL contain 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:

Note: I created the 5 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 which pull data from your website for each content group

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

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

#3 Select ‘Regex Table’ from the ‘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’:

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

Note: If your input variable does not already exist then create 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:

From the screenshot above we can conclude 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 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:

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

By default regex table variable uses full match which won’t allow us to do partial match.

So I unchecked the ‘Full Matches Only‘ checkbox.

GTM recommend not to use ‘Enable Capture Groups and Replace Functionality‘ if ‘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 match.

If you want the regex table variable to take 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 in 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:

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

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 which dynamically assign index number to each content group

#1 Create a new lookup table variable.

We are going to use this variable to dynamically assign 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 ‘content group’ regex table variable as the input variable from the drop down menu:

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

From the screenshot above we can conclude 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 set 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 ‘Leave Preview Mode’ link in your GTM account:

#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:

#4  Scroll down until you find your new regex and 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 ‘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:

Here,

# 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 an ‘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 ‘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:

#4 Check the value of content groups field:

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

Step-10: Publish your container & 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:

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

Announcement about my books

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 Beyond
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.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books:

error: Alert: Content is protected !!