How To Write and Test Regex Filters for Google Analytics (With Examples)

As with many of my articles here, I do some research for a client and then write about it here. To be honest, there are a couple of reasons why… first is that I have a terrible memory and often research my own website for information. Second is to help others who may also be searching for information.

What is a Regular Expression (Regex)?

Regex is a development method to search and identify a pattern of characters within the text to either match or replace the text. All modern programming languages support Regular Expressions.

I love regular expressions (regex) but they can be a little bit frustrating or infuriating to learn and test. Google analytics has some amazing capabilities… where you can create views with regular expressions or filter your data within regular expressions.

For example, if I wanted to see just the traffic on my tag pages, I could filter for /tag/ in my permalink structure by using:

/tag\/

The syntax is critical there. If I just used “tag”, I would get all pages with the term tag in them. If I used “/tag” then any URL that starts with tag would be included, like /tag-management because Google Analytics default to including any character after the regular expression. So, I need to ensure that I have the following slash included… but it has to have an escape character on it.

Regex Syntax Basics

SyntaxDescription
^Begins with
$Ends with
.A wildcard for any character
*Zero or more of the previous item
.*Matches any characters in
?Zero or one time of the previous item
+One or more times of the previous item
|The OR operator
[abc]A or b or c (can be any number of characters)
[a-z]Range of a to z (can be any number of characters)
[A-Z]Range of A to Z (capitalized)
[0-9]Range of 0 to 9 (can be any number)
[a-zA-Z]Range of a to Z or A to Z
[a-zA-Z0-9]All alphanumeric characters
{1}Exactly 1 instance (can be any number)
{1-4}Range of 1 to 4 instances (can be any number)
{1,}1 or more instances (can be any number)
()Group your rules
\Escape special characters
\dDigit character
\DNon-digit character
\sWhite space
\SNon-white space
\wWord
\WNon-word (punctuation)

Regex Examples For Google Analytics

So let’s put some examples out there for some Custom Filters. One of my colleagues asked me for assistance to identify an internal page with the path of /index in addition to all blog posts that were written with the year in the permalink:

My custom filter pattern for the filter field Request Url:

^/(index|[0-9]{4}\/)

That basically states to look for /index OR any 4-digit numeric path ending with a trailing slash. I created a view in Analytics and added this as the filter:

Here are a few more examples:

^/[0-9]{4}\/
(.*)certificat(.*)
(direct\smail|paid\ssearch)
^/mens/shirt/(.*)
^/page/[1-9]*/$
123\.456\.789\.[0-9]
thankyou\.html\?success=true

How to Test your Regex Expressions

Rather than trial and error within Google Analytics, I often just jump over to regex101, a fantastic tool for testing your regular expressions. It even breaks down your syntax for you and provides the details of your regular expression:

Build, Test, and Debug Regex

Exit mobile version