Regex, or Regular Expressions, can be a seriously powerful tool when creating custom segments, reports and filters. They allow you to go beyond the already powerful standard click and select functionality that comes as standard with Google Analytics.
There are plenty of resources available out there to help with this, but they can get quite technical. I hope to bring together the regex patterns that I find the most useful, and hopefully you will to.
If you have any use cases that you are having trouble with, please add them to the comments below and I will help you to find the best way to use regex to solve them.
I will be keeping this blog post up to date so please do bookmark and check back every once in a while.
Useful Regex
The Wildcard
If you want to see traffic to a specific section of your site using your url structure, then this regex is the one for you. This technique can be applied to anything where you want to match a word or phrase within a string (in this case a keyword in a url).
- In your filter ensure that you select Page for the URL, Regex for the condition and (.*)2013(.*)
In this example, the filter will return any page that has “2013” in the url. You could use this for anything where you are trying to match a specific work within a string of letters and numbers.
The regex (.*) is essentially a wildcard that, when placed before and/or after a keyword allows you to say to Google Analytics, please give me everything where this word appears.
The OR Regex
There is a regex condition that allows you to find multiple keywords in the page title. Let’s say that you wanted to find all content that relates to Christmas. You could create a filter on a Page Title report that only pulls in data based on a certain set of words.
You do this by using a PIPE:
- In your filter, ensure that you select Page Title, Regex as the condition and then your keywords – which should look like:
- christmas|santa|noel|mince pie|holly|misteltoe
The filter will return any pages where the page title contains the keywords listed above. Remember not to add a PIPE at the end of your keyword list as this will result in returning all pages.
The give me something specific Regex
Let’s say that you wanted to filter only traffic from Facebook and Twitter in your All Traffic Report in Google Analytics. To do this, you need to use Regex.
Go to the All Traffic report and select Source as your dimension rather than Source/Medium, then click on Advanced Filter. In the filter, you need to add the following to ensure that you only include traffic from Facebook and Twitter:
- facebook|twitter|^t.co$
By using the PIPE, you are ensuring that any of the keywords you added are included. The ^t.co$ ensures that you are only including traffic from Twitter’s redirecting url, t.co.
If you hadn’t encased the keyword with the ^ and $ symbols, you would have seen sources that contained t.co – for example pinterest.com and other domains.
- ^ means starting here
- $ means ending here