GA4 RegEx | Ultimate Guide

Like its predecessor, Universal Analytics, GA4 also uses regular expressions (Regex). By understanding GA4 Regex, you can effectively search, extract, and manipulate data according to specific patterns or rules. So, let’s embark on this journey of harnessing Regex to enhance your analytics workflow.

What are regular expressions in Google Analytics 4?

Regular expressions, also known as regex, allow you to create patterns to match and filter data. They are used to match and filter information based on specified criteria.

For example, let’s say you have a website and want to track how many people visit different pages related to “shirt.” You can use regular expressions to create a pattern that matches all the URLs containing the word “shirt.” Applying the regular expression pattern in Google Analytics 4 will filter the data and show you only the URLs with the word “shirt” in them. This way, you can quickly see how many people visited pages related explicitly to shirts on your website.

Regular expressions in Google Analytics 4 (GA4) are patterns used to match and manipulate data. They are widely used in GA4 for several reasons, some of them include:

  • Advanced Data Filtering: Regular expressions enable you to apply complex filters to your data. You can define specific patterns to include or exclude data based on your requirements. This helps you focus on the most relevant information and eliminate noise from your reports.
  • Data Input Validation: Regular expressions can be used to validate data inputs. They can ensure data matches a specific pattern or format, such as validating email addresses, phone numbers, or other structured data. This helps maintain data accuracy and integrity.
  • Data Manipulation: Regular expressions enable you to manipulate text by replacing, removing, or rearranging specific patterns. With this, you can find and replace specific text. This is useful for cleaning data, standardising formats, or transforming data to fit specific requirements.

You can use Regular expressions in various ways in Google Analytics 4. You can create custom dimensions to capture specific data, set up filters to include or exclude certain information from your reports or build audiences based on specific patterns.

In summary, Regular expressions allow us to find and organise information based on specific patterns, making it easier to gain insights and make informed decisions.

What’s the difference between Partial Match and Full Match RegEx in GA4?

In Google Analytics 4 (GA4), there are two ways to match patterns using regular expressions: Partial Match and Full Match.

Partial Match means that the pattern can be found anywhere within the data. It looks for pattern occurrences within the data, regardless of its position. For example, if the pattern is “shirt,” it will match data like “themed shirt” or “shirt for sale.”

A full match, on the other hand, requires all the data to match the pattern exactly. It looks for an exact match of the pattern against the entire data. For example, if the pattern is “shirt,” it will only match data that contains the word “shirt” exactly, like “shirt” but not “themed shirt” or “shirt for sale.”

So, Partial Match finds the pattern anywhere in the data, while Full Match requires an exact match of the entire data to the pattern. The choice between the two depends on whether you want to capture any occurrence of the pattern or only exact matches.

Criteria

Partial Match Regex

Full Match Regex

Matching Behaviour

Matches any part of the string

Matches the entire string

Use Case

Useful for capturing variations of a keyword or phrase

Useful for capturing specific, exact matches

Impact on Data

May result in higher number of matches

May result in lower number of matches

Considerations

Can lead to potential data overlap

May miss out on relevant data if not used correctly

Suitability

Broad analysis and capturing multiple variations

Precise analysis and capturing specific matches

What is a Metacharacter in RegEx?

Metacharacters in regular expressions (RegEx) are special characters with specific meanings and functions within a pattern. They play a crucial role in defining how the pattern operates. For example, the dot (.) matches any character, the asterisk (*) repeats the preceding character or group, and the pipe (|) allows for selecting between different patterns. 

Other metacharacters like square brackets ([]), caret (^), dollar sign ($), and backslash () have their unique roles, such as defining character classes or matching the beginning or end of a line or string. 

Understanding metacharacters is essential as they enable the creation of powerful and flexible patterns in regular expressions, specifying the pattern’s behaviour and the type of data it should match. So, let’s explore the common metacharacters to enhance our understanding of their functionality further.

Common RegEx characters

There are several RegEx characters, but we will focus on the common ones in this section. These characters have been grouped based on their functionalities.

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
GA4 RegEx Infographic
Infographic: How to use regular expressions

Where can you use regular expression filters in Google Analytics 4?

In Google Analytics 4 (GA4), regular expression filters can be used in various areas to filter and manipulate data. Here are some common features where you can apply regular expressions.

How to use RegEx in GA4 Explorations

Using Regular expressions (RegEx) in GA4 explorations allows for advanced data analysis and filtering based on specific patterns in your GA4 reports. Simply create an explore report, add all the dimensions and metrics you require, and then use your Regex pattern in the filter segment of the report pane. In the image below, we used the Regex pattern “desktop|mobile” in the filter to get only the device categories that are either desktop or mobile.

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

How to use RegEx in Standard GA4 Reports

Since August 4, 2023, GA4 users can utilise regular expressions in GA4 standard detail report filters. To do that, simply go to any of the standard reports of your choice in your GA4 property, click on the “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

How to use RegEx in segments and audiences

You can use Regex in Google Analytics 4 segments and audiences, you have to set the Regex as a condition. For example, instead of using the “or” operator, I used the “desktop|mobile” Regex pattern to get only mobile and desktop traffic (excluding smart TV and tablet devices) for the segment in the image below.

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

How to build custom events and conversions with RegEx in GA4

You can use regular expressions (regex) in Google Analytics 4 to build new events. 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 page view 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
Creating GA4 custom event with RegEx

How to use RegEx in Referral Exclusion (Unwanted Referrals)

You can use Regex to exclude unwanted referrals from your GA4 properties. For instance, you can exclude referrals such as PayPal, Gmail, and Microsoft sign-up and login authorization redirect. 

For instance, If your website is multilingual, which means you have different domains for different languages, you can use regular expressions (RegEx) to exclude specific domains from your Google Analytics 4 (GA4) property. In the example below, in the Google Tag configuration, we used Regex to exclude specific domains and PayPal from being counted as referrals in the GA4 property by simply applying the Regex pattern in the “List unwanted referrals.”

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

In Google Analytics 4 (GA4), you can use regular expressions (RegEx) to create filters that exclude internal and developer traffic from your analytics data. These filters help ensure that visits from your team or development environments do not skew your reports.

For example, go to your Data Stream’s “Configure Tag Settings” to exclude all traffic from a specific IP address range. You can use a RegEx pattern like “(206.123.140|71|76|87)”. This pattern matches IP addresses starting with “206.123.140” and followed by either “71”, “76” or “87”.

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

Create new custom channel groupings with RegEx

In Google Analytics 4 (GA4), you can create custom channel groupings using regular expressions (RegEx) to categorise your traffic into custom channels. Custom channel groupings allow you to group your traffic based on specific criteria not covered by the default channel definitions. To create a custom channel grouping, simply go to your “Data Settings” in your GA4 admin, click on Create new channel group, add a new channel, name it, and use Regex to add the conditions for your channel grouping, as seen in the image below. With this, you will be able to analyse your traffic based on these custom channels to gain insights into specific segments of your audience.

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

How to use RegEx to set up content grouping in GA4 via GTM

To set up content grouping in GA4 via GTM you must use the Regex Table variable in GTM. Let’s see how to achieve this below. Create a variable in GTM, select RegEx table variable as the type, choose Path Page as Input Variable, and set default value as others; and provide the Regex you would like to use in the pattern- output. For example, suppose I want to create a content group to track every blog post as a post group, every testimonial as a testimonial group, and every project as a project group. In that case, I will simply input the following Regex patterns: “.*\/post\/.*”, “.*\/testimonial\/.*”, “.*\/project\/.*”.

“.*\/post\/.*”  means every page path that includes /post/—the same with testimonials and project pages.

RegEx Table GTM Variable to send content groups data into GA4
Using regular expressions to set up GA4 content grouping

Do you need help with GA4?

GA4 offers a range of features and capabilities that enable businesses to gain valuable insights into their website or app performance and user behaviour. By utilising GA4, you can effectively track and analyse data, make data-driven decisions, and optimise your online presence. At Vakulski-Group, we specialise in providing comprehensive assistance with GA4, ensuring you can fully utilise this powerful analytics tool. Our services include GA4 implementation, customization, and data analysis. By partnering with us, you can leverage the full potential of GA4 and make informed decisions for your business. Contact us today to get started on maximising the benefits of GA4!

Conclusions

In conclusion, regular expressions (regex) in Google Analytics 4 (GA4) are valuable for creating powerful, flexible patterns to match and filter data. By understanding and utilising regex, businesses can gain deeper insights into their website or app performance, user behaviour, and marketing efforts. Defining specific patterns using metacharacters allows for precise data analysis and customization. 

Whether it’s creating custom dimensions, filters, or audiences, regex empowers businesses to tailor their analytics reports to their specific needs. With the proper knowledge and application, GA4 regex can be a game-changer in unlocking valuable insights and optimising business strategies.


Frequently Asked Questions

Can I use regex in GA4?

You can use regular expressions (RegEx) in GA4 (Google Analytics 4). RegEx can be employed to search, extract, and manipulate data based on specified patterns within GA4. 

What is the character limit for GA4 RegEx?

There is no defined character limit for regular expressions (RegEx) in GA4 (Google Analytics 4). You can use RegEx patterns of different lengths and complexities to match and manipulate data within GA4. Use concise patterns for optimal performance.

Where can I use regex in Google Analytics 4?

In Google Analytics 4 (GA4), you can use regular expressions (RegEx) in various areas such as segments and audiences, custom channel groupings, for creating new events and conversions, building internal and developer traffic filters, and in explore reports.

Leave a comment

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