Blog Home



21 Feb

Google Analytics: What Would You Do with Regular Expressions?



During my interaction with Google Analytics, I have made a conclusion that without specific manipulations you can use only 50-60% of this fantastic web analytics tool power.

By saying “specific manipulations” I mean a wide circle of custom settings: reports, segments, filters, dashboards and so on. However, I want to dedicate this article to unconditional King of the transformations and custom implementations – the regular expressions.

If you ask me to define what are regular expressions (by the way, let’s call them just RegEx), I will tell you that RegEx is a specific language which will help you to speak with Google Analytics. RegEx allows to group general data.  For more detailed basic information with every RegEx symbol descriptions you can visit (Google Analytics Support Center). Here I don’t want to be a teacher who forces you to learn the definitions by heart. Instead, I will provide you with several practical cases with regular expressions.

Case #1. Show me some more!

For example, we need to filter out all brand name keywords using segment. Here is a standard method to complete this task:


Looks pretty simple. But imagine that you can have up to 20-30 different variations of your brand name in keywords… Would you be happy clicking on “OR” statement for 20-30 times and getting a huge unreadable combination?

I advise you to write it down just in one line. Take a look:


The RegEx “|” allows us to do an “or” match, so you can write all possible combinations of brand names in one line just separating them by “|”. And, of course, don’t forget to switch “Matching RegExp” match type.

This method will perfectly work when, for example, brand names should be excluded. Using the standard method you can exclude only 2 versions of brand name, but RegEx will expand the list.

Another great example for this case.

Imagine that you need to create a segment containing (or excluding, whatever) the list of the pages:

  • /great-article/
  • /content/1
  • /content/2
  • /content/3
  • /content/45

You understand that the standard method is simply impossible in this case because we have to combine 46 different pages, but regular expressions will bring us to the happiness. Look at the simple line:


Explaining this: the segment will grab the data from /great-article/ page and /content/1 … /content/45 pages. Brackets [] create a list of items to match all numbers from 1 to 45, the symbol “$” requires no numbers/letters/symbols after the written before.

Case #2. Give me exactly what I want

For example, you have 2 pages on your website:

  • /thank-you/
  • /orders/thank-you/

And you want to create 1 goal for these 2 pages.

Note that if you create a goal like this,


you will lose the page /orders/thank-you/ (since we have Exact Match). Head Match will not help because it influences parameters only.

The only thing we have is to use RegEx in the following way:


(.*) is my favorite regular expression. It means “everything”.  So, here we say “mark every page ending with /thank-you/ as a goal.”

Case#3. Mixed, Advanced

There can be every complex task which requires thinking and analyzing. Without introductions, begin immediately with practical problems.

One of the most popular and common way of RegEx usage is Advanced filters. For example, we need to see not only domain name in Referrals report, but the whole URL. It can be very useful when you have visits from large website or forum and you want to know from which category/topic your visitors came from. To achieve this target, the following filter should be created:


Explaining this: Google Analytics reads the full URL (domain name and URI) – the RegEx (.*) means that every symbol (every URL) is read. Then Google Analytics writes it in the Standard report Referrals instead of just URI.

So what we have?

A few points for you to remember:

  1. Before writing a RegEx, think it over. Model the situation, try to figure out all aspects of it.
  2. Test, test, test. It is almost natural to make small mistakes in RegEx at the very beginning.
  3. Test again.

As you can see, regular expressions are the food for the mind of those who like to analyze, compare, reflect and play with data. You can use them not only in complicated filters, but also in small everyday tasks. Regular expressions make a web analyst’s life a little bit simpler and more interesting.

Leave a Reply

Your email address will not be published. Required fields are marked *