RegEx for Google Analytics: A Beginner's Guide on How to Use Regular Expression


Have you wondered how you can get specific and customised data that suits you instead of using the GA’s default version? Well you can do that with “Regular Expressions”, which is also known as “RegEx”.

By Ahmed khalifa
Blog header image for Regex for Google Analytics

For all the overwhelming amount of data that Google Analytics can provide for you, you could argue that you don't need anything more than what you have already got.

But I'm afraid there is more to GA than just looking at sessions, landing pages and sources of traffic.

That's OK though, because it's time to unlock another way to extract even more useful data about your website.

Have you wondered how you can get specific and customised data that suits you instead of using the GA's default version?

Well you can do that with "Regular Expressions", which is also known as "RegEx".

At first, it seems really daunting (and I had that feeling once). But once you get used to them, they can be incredibly powerful.

So assuming that you are a beginner, this post will provide a clear and simple guide on how to use regular expressions in Google Analytics to get the data that you (didn't know) you needed.


What Are Regular Expressions or RegEx?

When I first started using Google Analytics, I remember how my goals and filters were not working the way I wanted them to work.

I had a specific request in mind and none of the filtering options provided gave me what I was looking for.

Then I started hearing about this thing called Regular Expressions, which is where you can use special characters to "match up" and fine-tune your GA data.

At first, it seemed so overwhelming, scary and I couldn't get my head around it, but once I got the basics out of the way and then started making complex RegEx, it opened up a whole new level of data on GA.

Why Use RegEx?

There are a number of reasons why you should use RegEx:

  • create filters that work, as many of them require some form of Regular Expressions
  • create a goal that can match many goals instead of creating multiple goals. So if you have multiple "thank you" pages which you want to consolidate into one, you can use Regular Expressions to round them all up
  • create bespoke data that you wouldn't have been able to get otherwise
  • to make Google Analytics data more custom to your specific needs

Overview of RegEx to Use in Google Analytics

There are 13 different symbols that are used in RegEx (remove quotations marks when using the symbols):

  1. The Pipe "|"
  2. The Dot "."
  3. The Asterisk "*"
  4. The Dot/Asterisk Combo ".*"
  5. The Backslash "\
  6. The Question Mark ""?""
  7. The Square Brackets ""[ ]""
  8. The Square Brackets/Dash Como ""[-]""
  9. The Parentheses ""( )""
  10. The Curly Brackets/Braces ""{ }""
  11. The Caret ""^""
  12. The Dollar Sign ""$""
  13. The Plus Sign ""+""

1. The Pipe “|”

  • Definition: or
  • Examples: /blog/|/podcast/ to find /blog/ or /podcast/ or adidas|nike to find adidas or nike
How to use Pipe ('Or') as Regex on Google Analytics
RegEx used above is|youtube

2. The Dot “.”

  • Definition: match any single character
  • Examples: .ite could match site, kite, bite, #ite, %ite, but not ite.
How to use Dot as Regex on Google Analytics
The above example shows how you can use the dot symbol to find anything that has characters before the word Facebook (but not “facebook” by itself)

3. The Asterisk/Star “*”

  • Definition: match zero or more of the characters before it
  • Examples: if some shouted heeey, you don’t know how long it could be, e.g. hey, heey, heeey, etc. So a star can cover all grounds hee*y
  • There isn’t an image of how you can best use it, as the most common way to use the asterisks is to combine it with the dot, as shown below, which is a more powerful combination

4. The Dot/Asterisks Combo “.*”

  • Definition: matches zero or more random characters before it (i.e. anything and everything)
  • Examples: if you have the following URL structure /product/.*/shoes, you can make sure that you capture everything in the middle, such as:
  • /product/men/shoes/
  • /product/women/shoes/
  • /product/kids/shoes/
  • /product/baby/shoes/
How to use Dot and Asterisks as Regex on Google Analytics
RegEx used above is google\+redesign/.*/quickview

And you may have noticed within the filter section where you write the RegEx that there is a backslash “\” included before the plus sign.

This is because the plus sign has its own meaning (which is explained below), so it needed to be ignored or “escaped” as shown below.

5. The Backslash “\”

  • Definition: “escape” or ignore a character
  • Examples: if the next character is a character used in RegEx, you can “escape” it. E.g. in /thankyou?id=123, the question mark has a purpose in RegEx (we’ll come to that soon). So to ignore it and turn it into a normal question mark, you write is as /thankyou\?id=123 and add a backslash before the question mark
How to use Backslash as Regex on Google Analytics
RegEx used above is /myaccount.html\?mode

6. The Question Mark “?”

  • Definition: the last character is optional
  • Examples: imagine if you have a website that is created by a community and spelling mistakes occur in the URL, you can find the misspelled words as well as the correctly spelled words. For example, to target both Johnny and Jonny, you can just type Joh?nny to capture both variations of the name

7. The Square Brackets “[ ]”

  • Definition: a way to create a “list” of characters by focusing on specific letters
  • Examples: you can also focus only on
  • lower cases [abc]
  • capital letters [ABC]
  • numbers [0-9]
  • lower case letters, upper case letters, and numbers [a-zA-Z0-9]
  • Much like with the asterisk mentioned earlier, you will benefit from the square brackets more if you combine it with the dash below and create an even more powerful list

8. The Square Brackets & Dash Combo “[-]”

  • Definition: include a sequence of characters or letters
  • Examples: if you have a blog with the year included in dates and you want to choose a specific year, combining dash and square brackets can help, e.g. blog\/201[1-5]/. Or to find a sequence of products e.g. \/shopping\/product\-[a-c] to find product a, product b and product c
  • Below is an image example for this site if I want to look at podcast episodes between 50 and 59 to analyse a specific sequence of podcasts:
How to use Square Brackets and Dashas Regex on Google Analytics
RegEx used above is /podcast/episode\-5[1-9]\-this\-week\-online\-today

9. The Parentheses “( )”

  • Definition: a way to group numbers or words together, like in mathematics
  • 2 x 5 + 10 = 20
  • 2 x (5 +10) = 30
  • Example 1: if you have 2 confirmation pages called /example1/confirmation/ and /example2/confirmation/, you can group them together with RegEx using /example(1|2)/confirmation/
  • Example 2: earlier I’ve used these directories as examples:
  • /product/men/shoes/
  • /product/women/shoes/
  • /product/kids/shoes/
  • /product/baby/shoes/
  • Instead of using dot asterisk “.*” combo, you can use /product/(men|women|kids|baby|)/shoes
How to use Parentheses as Regex on Google Analytics
RegEx used above is: /google\+redesign/shop\+by\+brand/(youtube|google|android)/quickview

10. The Curly Brackets/Braces “{ }”

  • Definition: repeat the last “item” or “piece” of information a specific number of times
  • Example 1: {1.2} means to repeat that last piece at least once but no more than 2 times
  • Example 2: {2, 4} means to repeat that last piece at least once but no more than 4 times
  • Example 3: {5} means to repeat that last piece 5 times
  • Example 4: if you are in an office with a range of IP addresses and you want to exclude them the range from to (notice the last 2 digits) on Google Analytics, you can filter using 83\.239\.139\.[0-9]{1,2}
How to use Curly Brackets as Regex on Google Analytics

11. The Caret “^”

  • Definition: start exactly like this
  • Examples: imagine you have a set structure in your URL that begins with /blog/dog-food/ followed the by post name, you can ensure that they are tracked by choosing the caret at the beginning so that all URLs that contain that structure are chosen, e.g. ^/blog/dog-food/.
How to use Caret as Regex on Google Analytics
RegEx used above is ^/google\+redesign/apparel/mens/

12. The Dollar Sign “$”

  • Definition: something ends with… (and replace the ellipsis with your choice of character/s)
  • Examples: if you want to filter anything that ends with a specific character, you add the dollar sign, e.g. /$ means the page has to end with a slash (which is normally the homepage), shoe$ means it has to end with shoe, black shoe, men’s shoe (but not shoes, black shoes, men’s shoes)

13. The Plus Sign “+”

  • Definition: match one or more of the previous characters (not to be confused with asterisk, which is to match zero or more of the characters before)
  • Examples: rarely used but still useful. aargh! can be written in many ways. So making it aa+rgh will ensure that you capture aaargh, aaaaargh, aaaaargh (you get the idea)


Where Can I Use RegEx in Google Analytics

Now that you are armed with this info, what do you do next? Where can you apply your newly found skills in RegEx in Google Analytics?

Well there are 4 main places you can consider:

a) Applying Filters on Reports

Just as I have demonstrated many times above, you can use the advanced filter section within the table.

But you don’t have to click on ‘advanced’ to use it, as you can type it in directly within the field:

Filterring within table of Google Analytics

b) Creating New Filters in Admin

Creating custom filters can help to better define the quality of your data, and regular expressions can help you with that.

An example mentioned earlier would to be to use filter to exclude any traffic within your office’s IP range:

How to use Curly Brackets as Regex on Google Analytics

But if you are going to use filters, it’s always a good idea to use ‘Test’ view first, which is a good practice to use when setting up Google Analytics, so that you don’t skew your data permanently.

To create your own filter, you can follow Google’s guidelines.

Related Article: How to Set Up Google Analytics Correctly Step-by-Step?

c) Setting Up Goals

Whether you have just started or have an existing account, every Google Analytics must have goals as part of their set-up.

It’s the only way you can have conversions and also to understand better what improved or worsened your conversion rate.

If you haven’t done that, Google has basic guidelines on how you can do that.

d) Setting Up Segments

Segments are those sections at the top where you can segment (obviously) your data.

For example, you can segment your data to focus on specific types of specifications such as browser types, locations, device types, etc.

Examples of the default Google Analytics segments
Examples of the segments available on Google Analytics by default

If you want to learn more about segmentations, click on the link below:

Related Article: How to Effectively Carry Out Google Analytics Segmentation

How to Test RegEx?

If you are not sure whether your RegEx will give you the results you want on Google Analytics, it’s always a good idea to test it.

The easiest way you can do that is to use the Advanced Filter section within the table to quickly check whether it works or not.

Just remember to click on ‘Apply’.

Even though you can use other filter options such as “Begins With” and “Containing”, this option is not available in areas like goals and filters. This is why it’s worth knowing at least some of RegEx as it will unlock various ways to filter what you’re looking for.

At the same time, RegEx will give you a lot more flexibility than using the other filter options available when you consider that you can combine any of the RegEx to create your own custom filter.

And that’s something you can’t do using the default option provided by GA.

If you want a more detailed yet well-designed and laid out explanation of all the regular expressions mentioned in this post, by far the best one out there is by LunaMetrics.

However, the best way to learn how to use RegEx in Google Analytics is to go to Google Analytics and start practicing.

Eventually, it will become a natural thing for you.

Have you ever heard of regular expressions on Google Analytics? If you have, how are you finding it? If not, do you think you will benefit from it?

Let me know in the comment section below.