Google Analytics grew up out of Urchin, a log-processing tool designed for tech-savvy webmasters who were used to using shell scripts and perl to automate common tasks in Unix. When Google bought Urchin, they turned in into a software as a service, dramatically improved the usability and functionality. However, a lot of the features within GA share some legacy code with Urchin. Asking a webmaster or sysadmin to write complicated regular expressions to create a Google Analytics filter wasn’t too far out of the norm. With the acquisition, the tool became commonplace for marketers, however, there’s a gap in knowledge specifically around regular expressions (regex). This post aims to outline some simple regular expressions to add flexibility to your Google Analytics implementation.
Regular expressions are basically a way to add ‘wildcards’ to a search term. Excel accepts * in find and replace, which matches anything. If you’re familiar with old DOS commands, *.exe meant all executable files and *.* meant all files. Regex is a more complicated (some would argue flexible) version of the same thing. As an aside, this post will cover Google Analytics Regular expressions, which may or may not work for other applications.
First off, I’ll outline the most-used regular expressions in Google Analytics, then give several applications of regex.
Regular expressions definitions
^ – loosely, the carat means ‘starts with’. The regular expression ^cat will match both cat and cathode, but not scat.
$ – the dollar sign is the means ‘ends with’. The regular expression word$ will match word and keyword, but not wordsmithing.
. – the period is a wildcard. It means match the one instance of any character.
* – the asterisk means ‘repeat the character before this zero or more times’
+ – the plus means ‘repeat the character before this one or more times’
| – the pipes mean ‘or’
Applications of regular expressions in Google Analytics
Regular Expressions in the content reports
Here’s my favorite. For a quick and easy way to test whether a regular expression will match against the desired pages, go into the Top Content Report and enter the regular expression into the search box below the report. For example, try ^/$|^/about/$ This should return both the results for the home page as well as your /about/ page.
Regex in Google Analytics filters
Historically, regex was most useful when creating a filter. There are some awesome tutorials on this; I won’t attempt to recreate those.
Regular Expressions in Goal Definitions
If you have several pages that you’d like to trigger a goal, you can add them using either the asterisk or the pipe. For example, if you’d like both /products/subscription/thanks page and the /products/transaction/thanks page to trigger goal 1, we could define goal 1 as either /products/.*/thanks or /products/subscription/thanks|/products/registration/thanks. This tactic also works when defining the steps in the funnel report.
Regular Expressions in Google Analytics Advanced Segments
The newest use of regex in GA has to do with defining advanced segments. Within the Advanced Segment definition, we can use regex to match against several pages, keyword or campaigns to make your segment definitions more flexible. In the example below, I simply used the regular expression pipe to simplify an “OR” logical statement, the result of which will show visits coming from keywords related to excel. (Read more about how to categorize keywords)
How do you use regular expressions in Google Analytics?
Related posts:


{ 1 comment… read it below or add one }
Jared,
cool tip for viewing home and about pages. I think I’ll make a segment for that.
I just read some cool tips somewhere else, though I can’t remember the post – but I do have the regex they used.
This filters keywords by one, two and three word phrases – so it’s kind of like your excel long-tail formula, except you can view it within GA. If I find the link, I’ll post a comment.
1 word:
^\b\w+\b$
2 word:
^\b\w+ \b\b\w+\b$
3 word:
^\b\w+ \b\w+ \b\b\w+\b$
Thanks