Blog

What is RegEx in Google Analytics?

Regular expressions, or RegEx, are unique text-matching characters that can be utilized for various data extraction purposes inside Google Analytics (GA). It’s functionalities can be applied to view filters, goal tracking/setting, custom segments, and table filters, as well as many environments independent of Google Analytics (RegEx is a tool frequently used in programming languages like JavaScript, Python, R, and C++). 

While mastering RegEx is a skill of its own, one that can be quite complex to master, Google Analytics requires only a foundational understanding of its functionalities. 

To put it short, RegEx in Google Analytics allows you to match specific patterns in your data.

 

How to Use RegEx Effectively in Google Analytics 

In order to use RegEx effectively in GA, you will need to understand the tool’s Metacharacters & sequences, and their individual functionalities.

RegEx Metacharacter or Sequence 

Interpretation

^

Matches any string that comes after the ^ character. Any text preceding ^ will not be matched.

|

The pipe symbol, which means or is one of the most frequently used regex metacharacters in GA. It allows you to match multiple items (X|Y|Z).

$

Matches the end of a string

.

Matches any single character

[ ]

Brackets match any one of the enclosed characters. Example: [abcd] would match a, b, c, d, abcd

\

The escape character marks the following character as a literal instead of a metacharacter.

?

The ? quantifier makes the preceding character optional. 


Example: colou?r will match both colour and color.

*

Matches 0 or more of the preceding character. 


Example: zo+ will match either z or zoo

 

RegEx Google Goal Example

Let’s say you have three different thank you pages with unique URLs (/thank-you/, /thank-you-sales/, /product-inquiry-received/), but you would like to track all of them under a single goal completion. In order to do this you will need to set the ‘Destination’ (under Goal Details) to ‘Regular Expression.’

In the empty field you will need use the Pipe symbol to separate each URL:  thank-you|thank-you-sales|product-inquiry-received

RegEx PageView Filter Example

Let’s say you operate a Health and Wellness website and you have been writing lots of COVID-19 related blogs. You want to analyze traffic on all the blog pages that begin with /covid-19. This can be done very easily by using the ^ metacharacter and adding ^/covid-19 in the advance section of your All Pages breakdown.

 

Another RegEx PageView Filter Example


You currently run an eCommerce shop that offers both a wide list of products and services. You’re looking to compare PageViews and Bounce Rate data on two specific sections of your site, /products/ and /services/. This can be done by using both the ^ and the |. You can create the following regex to pull the data you need, ^/(products|services)/. Now you will be able to analyze the metrics you want for all the pages containing /products/ or /services/ (example: /products/black-gloves, /products/shoes/runners,  /services/online-styling-course/ )

 

RegEx Custom Segments Example


Another area of Google Analytics that you can apply regular expressions to is within your custom segment settings. Let’s say that you wanted to filter traffic based on two Countries, Canada and Australia. This can be done by either using the built in OR options or by using regex.

 


Summary

 

RegEx can be a very powerful tool in Google Analytics. Allowing you to speed up your data analysis process, segment your data, and track specific goal interactions. 

If you need help or have questions regarding regex use cases in Google Analytics or Google Tag Manager, feel free to email us at info@pylotdigital.com



Leave a comment

Your email address will not be published.