Regex, aka Regular Expression, is a function you can use in Google Analytics to create advanced filters, goals, segments, audiences, content groups and channel groupings.
To better understand how Regex works, we’ve broken down some of the standard characters used in regular expressions, efficient ways to use the solution and how to use Regex to manage large portions of data.
. Matches any single character (letter, number or symbol)
? Matches the preceding character 0 or 1 times
+ Matches the preceding character 1 or more times
| Creates an OR match
$ Signified the end of a line
\ Escapes the character following it
* Matches zero or more of the previous character
Ways To Use Regex
Regex can be used in numerous scenarios in Google Analytics. One example would be creating a Referral Spam Filter. A few sources in your Referral Report may look suspicious, so you’ll want to remove them to make sure your data is as clean as possible. To remove it, you’ll need to use the pipe symbol (|) which means “or.” For example, if you see the following spam in your Referral Report, you’ll want to create a Filter Pattern to remove the spam using the pipe symbol.
Take Note: you can also use the pipe symbol when searching for something within Google Analytics.
Another standard situation is if you need to create a filter that excludes several internal IP addresses. You can create a Regex that captures all IP addresses without actually having to type out each IP address.
Dealing With Lots of Data
Finally, sometimes you may come across a ton of data from an external file (such as SKU data or a data dump from BigQuery). To parse the data into a Regex, you can use in Google Analytics for a Custom Report, use a delimited rule. To do this, follow these steps:
- Copy the data and paste it into Notepad to strip the table
- Copy the data from Notepad and paste it into a Word document
- In the Word document, Replace ^p with |
- Copy and Paste the separated data into a Custom Report
Take a look at Regexr.com to learn more and practice Regex building.