How To Use RegEx in Google Analytics 4

Posted in: Analytics

Join thousands of marketers to get the best search news in under 5 minutes. Get resources, tips and more with The Splash newsletter:

In Google Analytics 4, Regular Expressions (RegEx) act as powerful search tools that help you find and filter specific patterns in your data. By using RegEx, you can make sense of large amounts of information and discover important insights. With RegEx, navigating through complex data becomes easier, helping you make better decisions based on analytics.

What is RegEx?

RegEx is a way to filter data based on matching phrases and patterns. It works by matching strings or characters based on specific criteria, allowing you to customize your expression to create the exact kind of filter you need. RegEx can be beneficial for more complex filtering needs than traditional filtering allows. 

How can I use RegEx?

RegEx filters use a combination of multiple expressions and special characters to filter data based on patterns. Using RegEx allows you to filter values matching part of a phrase, rather than only matching exact values, returning multiple values rather than just one. Understanding how to use RegEx can be instrumental in filtering values to match the needs of your business. 

What are RegEx Metacharacters?

RegEx metacharacters are special characters that can be used when creating RegEx filters to define how phrases should be filtered. While there are many RegEx metacharacters, the following chart lists some of the most popular RegEx metacharacters, their purpose, and an example of how they can be used. 

RegEx Character Purpose Example Filter Example Return
| Used as an OR expression (e.g. cat or dog) cat|dog cat, dog
. Used as a placeholder for any character  12.4 1214, 1224, 1234, 1244
? Matches the character before it 0 or 1 time trees? tree, trees
* Matches the character before it 0 or more times 10* 1, 10, 100, 1000
+ Matches the character before it 1 or more times 10+ 10, 100, 1000
[] Matches one character that is contained in the bracket Part[123] Part1, Part2, Part3
Used in brackets to match a single character in a range of numbers or letters [0-9] 1, 2, 3, 4, 5, 6, 7, 8, 9
() Matches enclosed characters in order somewhere in a string (danc) dance, dancing
\ Indicates that the following character should be treated as a regular character rather than a RegEx metacharacter How\? How?

 

RegEx in Google Analytics 4

RegEx in explorations

RegEx can be used to create custom filters in the explorations section of GA4. RegEx expressions in Google Analytics 4 match exactly, so using metacharacters to broaden your results is key. 

To add a RegEx filter to a Google Analytics 4 exploration, first navigate to or create your exploration (located in the Explore tab). Under variables, select “Segments” and choose the type of segment you want. Next, click “add new condition” and select the variable you want to filter. After selecting your variable, click “add filter.” Select “matches RegEx” from the available dropdown menu and write your RegEx statement below. Click apply when you’re done. 

For example, let’s say you wanted to look at the number of users who had visited a page with the word “shop” somewhere in the title. You could create a user segment and write a RegEx filter with the word “shop.” However, if you just write the word “shop” the RegEx filter will only look for a page that has the exact name “shop.” If you want the filter to include pages that have shop somewhere in the title, you can use the expression *shop.* This expression uses metacharacters that say that there can be an unlimited number of any characters before and after the word shop, and to include any result where the page title includes the word “shop.” 

Adding a RegEx filter in GA4

RegEx in Looker Studio

RegEx can also be used in Looker Studio. While Looker Studio has other available filter options, using RegEx filters allows you to filter more complex data to your needs. One example of this would be the limitation of Looker Studio OR clauses, which only allow you to include 11 OR clauses. If you need more OR clauses than this, you can use a RegEx filter and the | metacharacter. 

Adding a RegEx filter in Looker Studio

To add a RegEx filter in Looker Studio, click on the chart you want to filter and select “add a filter.” Select “create filter,” and choose either “RegExp match” or “RegExp contains.” Then, write your RegEx filter. 

Conclusion

Although RegEx is slightly more complex than other filtering options, its advanced abilities allow you to write more complex statements to filter for the exact information you need more efficiently than typical filters. When using RegEx filters in GA4 or Looker Studio, always make sure to test that your filter is working properly and modify your expressions as needed. Doing so will allow you to create nuanced and custom filters for your website’s needs.

Search News Straight To Your Inbox

This field is for validation purposes and should be left unchanged.

*Required

Join thousands of marketers to get the best search news in under 5 minutes. Get resources, tips and more with The Splash newsletter: