Contact us

Analytics

Regular Expressions (RegEx) for Google Analytics4 min read

Learning to use Regular Expressions (RegExp or RegEx for short) in Google Analytics will allow you to refine and customise your searches and analysis to make quick work of reporting on [often needlessly] complicated data.

RegEx is a time saver, an elegant solution, which is oftentimes a necessity, as without it some searches and functions in Google Analytics (GA) are just not possible.

So what are Regular Expressions?

RegEx uses special characters to change a string of text into a custom search term, allowing you to specify multiple conditions for matching search terms in a single line.

There are characters used in RegEx that mean any, all, or, begins-with, ends-with, and one-or-more. Using these characters and combinations of brackets and commas for grouping, will allow you to specify between or min-max. There are even ways to specify character types such as any number, symbol and white space in your search.

This possibly doesn’t mean much written in just a paragraph, so we have created a handy “RegEx for GA” cheat sheet which details these special characters and their functions with examples on how to use them. This can be found below…

RegEx for GA Cheat Sheet

Hopefully with the examples shown on the cheat sheet above, you can see how you can generate very sophisticated search terms using RegEx.

Using RegEx in GA is particularly useful in the following two areas.

1. Search in tables

There is no ‘or’ condition when searching in standard reports in GA.

You can choose to include or exclude what you specify and you can add plenty of ‘and’ conditions; however, all must be true to return a search. It honestly feels like an oversight that you cannot use ‘or’ here but that is where RegEx comes in handy.

One of the most commonly used special characters in RegEx is the pipe | which specifies OR. So immediately you can see how using it here gets around the lack of OR functions in the advanced search.

Whilst handy to have this option in order to save time in general searches, sometimes it becomes an absolute necessity to aggregate data which is split into more than one line into a report.

A sad example

Imagine you run an email campaign to your subscriber list. You set your links across the email newsletter and diligently add your UTM parameters, source=newsletter medium=email campaign=jan2018 and you send your email.

Except you make one typo…. Instead of medium=email it says medium=emial…

EMIAL… there forever in all of your GA reports.

Its own line.

It doesn’t show up in any search for exact match = email, or even contains = email. It doesn’t pull into your custom segment for email traffic, it doesn’t even go into the default channel grouping for email.

So what are your choices? Pretend this activity didn’t exist? Manually add it back into all of your reports and recalculate all of the % metrics?

RegEx to the rescue

This expression email|emial typed into your search, added to your email segment or (ideally) applied to a custom channel grouping will group this data back together.

The other area where using RegEx in GA is more than just a time saver is:

2. Creating goals and goal funnels

All because in GA, the rule you apply to a goal is also applied to the funnel steps for that goal.

For destination based Goals, such as visits to a thank-you page for a newsletter sign up which can be reached from multiple different URLs before it, you need a RegEx that exactly matches the destination page but also an expression which covers all appropriate pages leading to it. That way you can correctly visualise the movement of people through these URLs to the end goal.

Think of this example: You run a blog with an email newsletter and the newsletter can be signed up to from any blog article. Your funnel for this goal would want to do the following:

Count people who visit the blog home page > people who visit ANY of the blog articles > people who visit the newsletter sign up page > people who visit the sign up thank you page (successful sign ups).

Using RegEx in your goal setup means that your “Goal details” would look something like this:

Without RegEx here, you cannot build this funnel into your GA goal because you’re using an exact match for the destination of this goal and therefore would leave you unable to specify multiple destination URLs for step 2, the blog article pages.

These are just a couple of examples of how and why you want to be making use of Regular Expressions in GA. As you can imagine, in practice the flexibility and power of using RegEx goes well beyond the scope of this blog post. One thing is for sure, if you want to get the most out of Google Analytics, RegEx is a tool you absolutely need in your arsenal.

To find out more, or to discuss how get started with RegEx, contact us.