Creating Google Analytics Goals With Regular Expressions (Regex)

29/04/2016

Google Analytics is an immensely powerful tool for analysing your website data. This article looks at creating a Google Analytics goal using a regular expression (regex) and how to create and test the regex.

So here's the scenario. You want to record any view of a particular page type as a goal. Easy enough you might think, but what if the pages are located randomly around the website folder structure? As an example, what if the page type is product pages and the product pages are at various depths in the page hierarchy? The shortest URL is of the form

domain/type1/type1a/product1
domain/type1/type1a/product2

but the product URLs can be as long as

domain/type1/type1a/type1ab/type1abc/product3
domain/type1/type1a/type1ab/type1abc/product4

Nothing within the product name indicates that it is a product - the product page names are the brand names.

Create the Goal

In this case, the only way to specify the goal is to create a regex expression that includes all of the distinct product URLs. This wouldn't be possible with hundreds of products but it is possible with a smaller number (for hundreds of products, you would need an easier way to identify a product page, possibly using Google Tag Manager and the Data Layer).

So how do you easily create a long regex expression to cover multiple pages? The key is the "OR" symbol in regex which is the vertical pipe character |

A regex expression of

abc|def|ghi

will match any URL which contains the strings "abc" OR "def" OR "ghi"

All good so far and you can go and create your Google Analytics goal - but how do you check that the Goal regex that you set is actually working correctly? You can click on Verify this Goal and this will give you the percentage of visits that triggered this goal over the past 7 days.

That certainly will give you confidence that the goal is not completely incorrect - but how can you sure that your regex is completely correct?

These are the steps that you need to take.

Test the Goal

1. Export the All Pages report from Google Analytics into a spreadsheet and identify every page URL that should be included in your Goal.

2. Using Notepad, build up the Regex using the vertical pipe for "OR". You only need enough of the URL to identify it uniquely. If this is not possible with the final part of the URL, then include more of the folder structure and prefix each forward slash with a back slash (in Regex, this means treat this forward slash as a forward slash and not a special character - this actually doesn't seem to be required in Google Analytics but it is good regex practice). Enter the expression into the All Pages report filter in this way

and check the pages listed with that filter.

3. You will expect to see the page list that you defined in Step 1. If you don't, then revisit your regex.

4. Once you're happy with the Regex expression, create your Goal - ensure that the drop down is set to Regular expression as shown. Verify the goal as the final step.

5. An important point to note is that if you create the goal as a regular expression then any funnel that you define will also expect regular expressions. 

If you have any comments, please add them below or contact me on Twitter

Written by Nick Baker

Previous
Previous

Google Tag Manager 101 Part 11 - Trigger Groups

Next
Next

A Leagile Strategy