How To Use RegEx in Google Analytics 4 - Go Fish Digital
Request Proposal Toggle Menu

How To Use RegEx in Google Analytics 4

How To Use RegEx in Google Analytics 4 featured cover image

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 CharacterPurposeExample FilterExample Return
|Used as an OR expression (e.g. cat or dog)cat|dogcat, dog
.Used as a placeholder for any character 12.41214, 1224, 1234, 1244
?Matches the character before it 0 or 1 timetrees?tree, trees
*Matches the character before it 0 or more times10*1, 10, 100, 1000
+Matches the character before it 1 or more times10+10, 100, 1000
[]Matches one character that is contained in the bracketPart[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 metacharacterHow\?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.

MORE TO EXPLORE

Related Insights

More advice and inspiration from our blog

View All

How To Export Google Analytics Data To Excel

Google Analytics data organization and filter options can help you avoid...

Abby Reed| September 14, 2024

Best Training Courses for Google Analytics 4 (all different levels)

If you are looking to get started using GA4 or ready...

Abby Reed| August 20, 2024

The Best Conversion Rate Optimization Tools In 2024

If you are on the hunt to find the best Conversion...

Abby Reed| July 14, 2024