How to use Regular Expressions (Regex) in Google Analytics 4

Like Universal Analytics, Google Analytics 4 uses regular expressions as well. Therefore, knowing how to use them and where to apply them can make your reports more flexible and allow you to use the “or” operator in GA4 explorations filters which these reports lack.

As a result of that and the recent GA4 update, which allows you to set up events and conversions using regular expressions determined to explain this topic in depth.

What are regular expressions in Google Analytics 4?

So, we must first clarify what regular expressions term stands for. In general, it describes the sequence of symbols or patterns used to check if the string follows this pattern or is used to extract the relevant part of the string or to extract and replace it with another one. 

For instance, suppose that you have three strings, “Hello, Ana!”, “Hello, Bob!” and “Hello, Spence!” and you want to extract the name from each of these strings to create a list of people mentioned in these strings. For this, you can use “Hello, (.*)\!” regular expression, and apply it to each of them. As a result, you will get “Ana”, “Bob”, and “Spence”. 

As you can see above, regular expressions can be used mainly to find the necessary information in the text or to get strings that match the specific criteria specified in the regular expression. Imagine that you have a list of phone numbers with some incorrect information and you want to check which one is a real phone number and which one is not. So you can, for instance, use this regular expression “/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/“ to check if the string follows the pattern or not. If it follows, it’s the accurate phone number, if not, you should miss this info.

Where can you use regular expressions in Google Analytics 4?

Although I mentioned above one of the most frequent ways of using regular expressions in our world, Google Analytics doesn’t allow you to extract the part of the string (and/or replace it) but use regular expressions strictly for checking if the variable follows the same pattern, the example with the phone number.

Build Events and Conversions with Regex in GA4

And the first way you can use regular expressions (regex) in Google Analytics 4 is to build new events based on the data you sent. After that, you can use these events for conversions.

Suppose I want to create a custom event whenever someone visits my articles. So I can use the GA4 pageview event and define page_location to follow the rule “matches a regular expression (ignore case) “https://vakulski\-group\.com\/blog\/post\/.*“. This way, GA4 will generate a new event whenever someone visits my blog posts. 

Create GA4 Event with RegEx
Create GA4 Event with RegEx

Use regex in segments and audiences

The second place you can use regex in Google Analytics 4 is segments and audiences. For example, instead of using “or” operator I used “desktop|mobile” to get only mobile and desktop traffic (excluding smartTV and table devices). 

Use RegEx in GA4 Segments and Audiences
Use Regex in GA4 Segments and Audiences

Use regex in GA4 Detail Reports

Since August 4, 2023 it’s also possible to use regular expressions in GA4 standard detail reports filters. To do that, click on “Add filter +” icon, select the dimension you want to use for the filter, select “matches regex” or any other operator with regex and write your regex string.

User RegEx in GA4 Standard Detail Report Filters
User RegEx in GA4 Detail Report Filters

Use regex in GA4 Explorations

Although you can create audiences and segments in GA4 explore reports, sometimes it’s just much easier to apply a filter instead of creating a segment. Moreover, it’s faster. And I didn’t mention that filters in GA4 follow “and” operator. There is no option to apply multiple filters with “or”. Therefore, regular expressions can significantly help you to find new insights and speed up your analysis. 

Use RegEx in GA4 Explore filters
Use RegEx in GA4 Explore filters

Use regex in Referral Exclusion (Unwanted Referrals)

The fifth place you can apply your regex knowledge in Google Analytics 4 is when you exclude unwanted referrals from your GA4 properties. For instance, you can exclude such referrals as PayPal, Gmail, and Microsoft sign-up and login authorisation redirect and etc. 

If your website is multi-lingual, you can find yourself in even more need to use regular expressions since the same 3rd party provider can use different domains. For instance, you can exclude Paypal from your GA4 property using regex.

Exclude PayPal using regex in GA4 property
Exclude PayPal and other unwanted referrals using regex in GA4 property

Apply internal and developer traffic filters with Regex 

Whenever you set up a new GA4 property, you also use GA4 filters to exclude internal and developer traffic.  It’s an extra place to use regex in Google Analytics 4. 

Exclude internal traffic in GA4 using regex
Exclude internal traffic in Google Analytics 4 using regex

Custom channel grouping with Regular Expressions 

And the last thing I found, but not the least, is custom channel grouping. You can create your own custom channels using regular expressions. Using them to aggregate data about the specific source you are interested in can be beneficial. 

Use regex in GA4 Custom Channel Grouping
Use regex in Google Analytics 4 Custom Channel Grouping

Essential metacharacters in regular expressions you can use in Google Analytics 4

Although regular expressions need time and practice to learn them to the advanced level, I would like to mention here below a few critical special symbols that will help you on your way to growing your business with GA4. Besides this list, I recommend you test every new regular expression before applying it to avoid data leaks and all that stuff.

Wildcards

.Matches any single character (letter, number or symbol)1. matches 10, 1A
1.1 matches 111, 1A1
?Matches the preceding character 0 or 1 times10? matches 1, 10
+Matches the preceding character 1 or more times10+ matches 10, 100
*Matches the preceding character 0 or more times1* matches 1, 10
|Creates an OR match
Do not use at the end of an expression
1|10 matches 1, 10

Anchors

^Matches the adjacent characters at the beginning of a string^10 matches 10, 100, 10x
^10 does not match 110, 110x
$Matches the adjacent characters at the end of a string10$ matches 110, 1010
10$ does not match 100, 10x

Groups

( )Matches the enclosed characters in exact order anywhere in a string

Also used to group other expressions
(10) matches 10, 101, 1011
([0-9]|[a-z]) matches any number or lower-case letter
[ ]Matches the enclosed characters in any order anywhere in a string[10] matches 012, 120, 210
Creates a range of characters within brackets to match anywhere in a string[0-9] matches any number 0 through 9

Escape

\Indicates that the adjacent character should be interpreted literally rather than as a regex metacharacter\. indicates that the adjacent dot should be interpreted as a period or decimal rather than as a wildcard.
216\.239\.32\.34 matches 216.239.32.34

Conclusions

With the recent Google Analytics 4 update, it’s apparent that Google will implement regular expressions across the entire GA4 property and allow to use them on a larger scale. Though we still can’t use regex in default GA4 reports, there are many other places where we can use regular expressions to enhance our analytics and make it simple and beautiful. 


Frequently Asked Questions

Can I use regex in GA4?

You can use regular expressions in GA4 in the following places: segments and audiences, custom channel grouping, creating new events and conversions, building internal and developer traffic filters, using them in explore reports and even more.

Where can I use regex in Google Analytics 4?

Google Analytics 4 allows using regular expressions in segments and audiences, custom channel groupings, creating new events and conversions, building internal and developer traffic filters, using them in explore reports and even more.

Leave a comment

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