Lookup tables and RegEx tables in GTM

GTM Lookup table and RegEx tables explained

What is the GTM Lookup table?

A lookup table in GTM is a custom variable type where you can output a specified value based on the input condition. If no values match provided conditions – a default value can be returned. For example, you could use that to categorize certain URLs or output proper tracking ID if that is different per Page or Domain.

GTM Lookup table diagram

Let’s say you want to use a different tracking ID on several pages, but you don’t want to create a separate tag per page. Using lookup tables you can create a variable that returns the correct tracking ID depending on the page.

  • To create such a variable go to the Variables section and create a new one.
  • From the Variable type select “Lookup Table”

As you will see we can select any built-in or custom variables as our Input parameter.

  • Since I’m interested in page location I will pick Page Path for this example
Select input variable in GTM
  • Then click “Add Row” to create a new condition. I will add here 5 page paths.
  • For each of those conditions, you can set an output that you would like to see. If we return the tracking ID then it could look similar to this:
Lookup table example in GTM
  • You can also set a default value which will be used if none of the conditions above are matched.

If you visit any of those pages in the preview mode, you will see that variable returns the value that we have provided in the right column.

If any other page is visited the default value is returned:

So this is a really easy way how to return specific values based on provided conditions. Then you can use it instead of static fields in tag setup:

Nevertheless, Lookup tables in GTM have a considerable limitation – only an exact match will return the expected result.
If you want to match a lot of dynamic values (e.g. All product pages) in a single condition, then you should use the RegEx table instead.

What is the GTM RegEx table?

RegEx table is a Lookup table with superpowers. If you need more advanced matching options (not only exact match) or if you want to extract some of the values dynamically and include those in your output – the GTM RegEx table is your option.

Instead of exact match RegEx tables use Regular expressions to match your input.

GTM RegEx table diagram

Let’s say you have hundreds or thousands of pages and you want to understand traffic distribution and performance of each page group (E.g. product category pages, blog posts, product detail pages, marketing landing pages, etc.).

Using RegEx tables we can match URLs that contain provided value and return the content group that we want to see in our reports ( Here you can find a detailed guide on how to group content in GA4 and GTM ).

Let’s take a look at how we would group these types of URLs using RegEx table:

  • Blog post – yourwebsite.com/blog/post/post-title
  • Product category – yourwebsite.com/category/t-shirts
  • Product details – yourwebsite.com/item/red-marvel-christmas-exclusive-tshirt

Open GTM and create a new user-defined variable using the RegEx table type.

RegEx table variable settings structure
  • Same as in the Lookup table we can pick “Input variable”. This should be the value WHERE we are searching for a pattern. In the case of URLs, I will use the “Page Path”.
  • In the RegEx table section, we need to provide the pattern that we are searching for. RegEx tables use Regular expressions (so as a variable name) to search for a value in a provided text string.

    Since in my example we will simply match any URLs that CONTAIN specific value we won’t dive deep into Regular expression syntax. If you want to learn more about RegEx syntax and all of the possibilities, I recommend starting with the MDN article, which is full of great examples.
  • I had 3 URL groups, so in the “Pattern” column I will add unique parts of those URLs.
  • In the Output column, I will provide a variable value that I want to have when any of those are matched. E.g. “Blog post” for Page Path that contains /post/

So if I have pages like:

  • /post/blog-post1
  • /post/blog-post2
  • /post/blog-post99

All of those will return “Blog post” as my output. You can clearly see how useful the RegEx table becomes in such scenarios.

  • Det default value if none of the conditions are met.

If I visit the page yourwebsite.com/about-us then this variable will return “Other”.

  • The most important part is to set correct Advanced settings, in case you want to match only part of the string (as we do in this example). Disable “Full Matches Only” and “Capture Groups and Replace” in order for partial matching to work.
  • You can save it now and give it a try in the preview mode.

If everything went as expected, each type of URL should return the output values that you have provided in the “Output” column.

Then you can pass this value to any tags as any other GTM variable.

Extract dynamic values using the RegEx table in GTM

It’s possible to return not only manually defined output but dynamic values as well. For example, you could extract product ID based on the DOM element or Page Path.

For example, if we have these URLs for our products with IDs:

  • yourwebsite.com/item/very-long-product-name1-12345
  • yourwebsite.com/item/very-long-product-name2-54321
  • yourwebsite.com/item/very-long-product-name3-67890

You could extract dynamic values by providing a regular expression in the pattern column.

In this case, we could use the following Regular expression:

.*item\/.*-(.*)

Regular expressions are a pretty advanced topic to grasp, so if you don’t have time to learn it in detail you could ask your developer to prepare them for you.

Most important points to remember to make this kind of extraction work:

  • Include a group capturing in your regular expression pattern. (In my case it’s “(.*)” )
  • Use dollar-sign replacement syntax to insert extracted dynamic value in your output.
    In my example, I have only 1 extracted group so I just use “$1“.
  • Full Match and Enable Capture Groups functionality should be enabled from Advanced settings.

When you put a regular expression then your variable will look similar to this:

Extracting dynamic values using GTM RegEx variable type

Then you can use it as a regular variable in any of the tags, and of course experiment with the Input values! Since we can extract dynamic values not only from URLs but from DOM element text, for example.

To validate whether your regular expression returns expected values I like to use RegExr.com . By providing a list of examples and your regex pattern, this tool will show you all outputs that would be captured from a given expression. It is useful and much quicker to test and edit regular expressions here before applying them to the GTM setup.

Common use cases for Lookup and RegEx tables

Let’s sum up some of the most common use cases where you would consider using Lookup or RegEx tables in GTM:

  • Determine Measurement/Tracking ID values based on Page URL/DOM element values/Other variables.
  • Site content grouping based on available values
  • Event name and parameter consolidation – For example, if clicked buttons don’t have readable text on them you could use their ID or class names to determine what the name or type of the button was clicked.
  • Extract dynamic data from other variables – You could use RegEx tables to capture and return dynamic values based on page URL/DOM element or other variables.

Summary

In this post, we have reviewed the Lookup table and RegEx table variables along with the example use cases. As you saw, the RegEx table option is much more powerful than the simple Lookup table, but it has a steeper learning curve since it relies on learning regular expressions. However, once you learn the basics, this variable type can become one of the most valuable elements in your GTM setups.

Leave a Reply

Your email address will not be published. Required fields are marked *