March 18, 2020
OR Condition in Google Tag Manager Triggers: 3 options
In Google Tag Manager, you can manage your tags and define the conditions when the tags must fire. However, if you have multiple firing conditions and you want to fire the tag on ANY of them, there are some things to keep in mind.
In other words, I will show you how to configure triggers with OR condition.
Table of Contents
- Multiple conditions in a single trigger are connected with AND
- An Example
- Option #1: Multiple triggers added to the same tag
- Option #2: Single trigger + Matches RegEx
- Option #3: Single trigger with Regex (or Lookup) Table Variable
- Final words
If you prefer video content, take a look at this tutorial from my Youtube channel.
Multiple conditions in a single trigger are connected with AND
A common mistake among beginners is not knowing the relationship between all the conditions in a single tag. Here’s an example.
You want to fire a tag on any of three pages, /pricinc/page1/, /pricing/page2/, /pricing/page3/. If you’re just starting to learn how to use GTM, you might think that the following trigger is a good choice.
However, it’s NOT. As GTM’s interface instructs, ALL conditions in the same trigger must be met, therefore, most likely, such a trigger will never be activated because it is not possible for the URL of a website to contain all 3 options (page1, page2, page3).
So how can you instruct Google Tag Manager to fire if ANY of the conditions are met? Let’s take a look at various options.
First, let’s imagine a case that we’ll try to solve. You have 3 pages where you want to fire a tag (the type of tag does not matter right now). URLs of those pages are:
All of the options below will be tackling the same case example. If a visitor visits ANY of the aforementioned pages, we’ll fire a tag.
Option #1: Multiple triggers added to the same tag
Disclaimer: This option to implement OR condition is the least optimal and I do not recommend it. Options #2 and #3 are much better. However, option #1 still works, therefore, I decided to show it as well.
One of the possible options you have is to add multiple triggers to the same tag. You’ll need to create separate triggers for each page. As you can see, this solution scales really poorly and you can easily fill the container with unnecessary triggers (imagine a situation where you need to target 50 pages).
Once you create all the triggers, add them to the same tag. You can select multiple triggers at once if you hover your mouse on a trigger icon (then the checkbox will appear).
If any of those triggers meet the conditions, your tag will be fired.
If, on the other hand, you want to combine several different triggers and to fire a tag only when ALL trigger conditions are met, take a look at Trigger groups.
Option #2: Single trigger + Matches RegEx
A much more elegant solution is option #2. Instead of having multiple triggers for each condition, we can combine all of them in a single trigger. That is possible thanks to Regular Expressions.
In this article, I’ll not explain what it is in great detail. All you need to know right now is that RegEx is a pattern that is used to check the text under certain criteria. RegEx uses special symbols that have a certain meaning.
In this case, we’ll use a quite simple example, a pipe | (vertical line)
This symbol in RegEx means OR. So if you have several different pages that you want to match against, you can just create a regex page1|page2|page3 (obviously, this one was just an example).
The real regex for this case would look like this
We can optimize this a bit and introduce another symbol, parentheses (that are used to group elements). Then the correct regular expression could look like this:
There is no single correct answer. Every situation can be approached in different ways.
Once you create a regular expression, let’s include it in the trigger.
- Create a Pageview trigger and enter the following condition:
- Page Path
- Matches Regex (ignore case)
- /pages/product(1|2|3) (you should enter your actual Regular Expression here).
Obviously, you should choose a different trigger type and condition if you want to track different interactions on a page. This is just an example.
That’s it! Once you save this trigger and add to a tag, your tag will fire whenever that RegEx condition is met. In our case, that’s one of the three URLs.
Related useful resources:
- Regex Tutorial for Google Analytics and Google Tag Manage
- Regex101.com (a tool that helps you validate your regular expressions)
Option #3: Single trigger with Regex (or Lookup) Table Variable
Option #2 to implement the OR condition was much more optimal compared to option #1. However, it can still get pretty messy if you have too many pages (say, 50) that you want to match.
Sure, you can still use the method I’ve described above but it is not very convenient to manage/use in the GTM interface.
You might have a very long Regular Expression but all you can see in the GTM UI is this:
That’s where Lookup or Regex table variables might become really handy. In this blog post, I’ll focus on the Regex Table Variable.
The only difference between Regex and Lookup Table variables is that the Lookup Table is always looking for exact matches (while the Regex table and contain more complex and flexible rules).
So, here’s the plan:
- We’ll create a Regex Table Variable
- Every condition (for each page) will be included in a separate row
- If a certain condition is met, the variable will return “true”
- Then we will use the Regex Table Variable in the Pageview Trigger. Condition: If the variable returns “true”, then the trigger will be activated.
In Google Tag Manager interface, go to Variables > User-defined Variables > New > Regex Table.
- Select the input variable. In our example, I’m working with the Page Path, therefore, I had to choose it.
- IMPORTANT: Click Advanced Settings and then Uncheck:
- Ignore Case
- Full Matches Only
- Enable Capture Groups and Replace Functionality
- Then click Add Row and add the conditions. In the left column, enter /pages/product1 (reminder: this is just an example. You need to enter YOUR conditions here). In the right column — “true” (without quotation marks)
- Repeat the same process for each condition/page.
Here’s how the Regex table variable will work: it will start checking the conditions from the top row till the bottom. Once the condition is met, further rows will not be checked.
Now, it’s time to create the Pageview trigger (once again, this is just an example. If you want to apply this technique to a Click Trigger, go ahead and do that).
Add the condition: Regex – very important products (I made that name up) equals to true
Pretty much that’s the same as the RegEx Table variable, except that maybe the conditions could be much more complex in the Custom JS. However, right now, I cannot come up with a good example of this.
Yup. I’ve just wasted a two paragraphs and said nothing useful 🙂
Final Words Regarding OR Condition in GTM Triggers
Yet another blog post has come to an end. Out of 3 options on how to create OR condition in Google Tag Manager, my favorite ones are #2 and #3. I always advise GTM users from creating redundant triggers for each condition/page/button rather than combining them into a single trigger.
Option #2 (single trigger with “Matches Regex”) is useful for quick and small conditions.
Option #3 (single trigger with Regex Table variable) is useful for longer lists of conditions that need to be connected with “OR”.