Useful Regex Codes for Google Analytics Reporting

Author's avatar Analytics UPDATED May 5, 2022 PUBLISHED Apr 7, 2022 12 minutes read

Table of contents

    Peter Caputa

    To see what Databox can do for you, including how it helps you track and visualize your performance data in real-time, check out our home page. Click here.

    Understanding regular expressions (regex) can be really challenging, especially if you are just getting started, but having this skill set is useful, especially for analysts and marketers. Regular expressions are essential for speeding up, setting up and auditing Google Analytics accounts and reports, and they help with advanced targeting, data organization, and deep insights delivery. 

    They also improve your ability to get accurate Google Analytics results, and as a newbie, you don’t need to have it all figured out initially. This article will discuss regex codes and provide all the information you need to use them in Google Analytics reporting.

    So, let’s get right to it.

    What is Regex, and Why Do We Need It?

    Regular expressions, also referred to as regex, are specific character sequences that broadly match search patterns in your analytics data set. They provide a syntax for finding, defining, and extracting data where basic filtering is not sufficient, and they can be used in multiple ways. You can use this tool in Google Analytics to develop flexible definitions for terms like goals, content groups, view filters, audiences, channel grouping, and segments. Besides, you can use regular expressions in place of plain text to simplify complex logic and test variables.

    You can use this tool to find new data that Google does not filter by default. A regex is not only valuable for analytics, as users can use it to check for patterns in a string and run complex targeting on their A/B tests.  Different tools and programming languages allow regular expressions, and they are usually made up of metacharacters with special meanings.

    Regex Uses in Google Analytics

    Regular expressions are handy in Google Analytics as they provide flexibility for report customization and configuration. Highlighted below are a few other uses of regex in Google Analytics.

    Table Filters Applications

    Standard and custom reports in Google Analytics are in a table format, and using regular expressions in table filters is an efficient way to work with specific data within the reports. You can control what is included or excluded from your report with ease by using multiple regex codes, and you have the flexibility to use the advanced filter. Besides, you should ensure you select the ‘Matching RedExp’ for the match type when using the Advanced option for the table filter.

    able Filters Applications in GA

    Setting Up Filters

    Building out filters is one of the most common uses of regular expressions in Google Analytics. You must set up a Google Analytics account and use a test view first to enable you to try a few things out first.

    Go to the Admin tab, click on Filters, and add filters to set up filters. 

    There are different filters used in Google Analytics, with the most common ones being excluding traffic from your IP address, organizing your subdomain tracking, and cleaning up query parameters.

    Setting up filters in Google Analytics

    Related: The 7 Most Important Google Analytics Filters for Marketers

    Setting Up Goals

    Regular expressions can be used to set up goals using events, destination, duration, and pages. You can create a goal that matches multiple pages. For example, If your users are redirected to different thank you pages after doing a transaction. when setting up a destination goal, you can use a regular expression to match different thank you pages

    Setting up goals in Google Analytics

    Custom Reports Filtering

    When creating a custom GA report, you can use regular expressions to filter specific information. For example, you can use a regex to include only the home and services sections of your website when creating a customized report. Also, you can use this expression in the filters you have in your reporting views.

    Custom reports filtering in Google Analytics

    Setting Up Custom Segments

    Google Analytics reports on All Sessions by default, and you can use segments if you want to dig deeper into a large dataset. If your segment has different conditions, you can use regular expressions to define the conditions in the segment builder. Besides, creating custom segments using regex is much easier than without. 

    Setting up custom segments in Google Analytics

    Channel Grouping

    Another critical use of regular expressions is to create channel grouping in Google Analytics. Channel grouping can be used to classify the various ways users find your websites, such as through social media and other platforms. For example, to create channel groups for social, you should select ‘Matches Regex’ and input (instagram| facebook| twitter| linkedin).* to match Instagram, instsgram.com, Facebook, facebook.com, LinkedIn, linkedin.com, Twitter, and twitter.com.

    Channel grouping in Google Analytics

    Defining Funnel Steps

    There are multiple pages in a typical sales funnel. For example, a user may go to different pages on your website when making a transaction. To set up a sales funnel to match the multiple pages, you need to use regular expressions, even though defining a funnel is optional. Plus, all URLs are treated as regex when you configure a funnel.

    Defining funnel steps  in Google Analytics

    Related: How to Set Up and Use Funnel Reports in Google Analytics: A Step-by-Step Guide

    Dashboard Widgets

    Regular expressions can be used to create a dashboard for a specific data set, and you can add filters to the dashboard widgets inside Google Analytics. 

    Dashboard widgets example

    Building Audiences

    You can use regular expressions to build audiences for remarketing and ad targeting in Google Analytics. Building an audience list is like custom segment creation, as you use a segment to create an audience.

    Audience building in Google Analytics

    What Are the Main Regex Metacharacters?

    Metacharacters are characters that take on a special meaning when used in regular expressions. These characters can be regarded as regex building blocks, and outlined below are the commonly used ones.

    1. Asterisk (*)
    2. Dot (.)
    3. Dot-Asterisk Combination (.*)
    4. Pipe (|)
    5. Backslash ()
    6. Caret (^)
    7. Dollar Sign ($)
    8. Question Mark (?)
    9. Plus Sign (+)
    10. Parentheses ()
    11. Square Bracket ([ ])
    12. Dashes (-)
    13. Curly Bracket ({ })
    14. Exclamation Mark

    1. Asterisk (*)

    The asterisk is used to match zero or more of the previous items. This definition can be quite tricky to understand, so here are some examples to clarify it. 

    For example:

    • Boo*ks would match boks, books, boooks, booooks, but not bokks.
    • Goo*gle would match gogle, google, gooogle, goooogle, but not goggle.

    It will neither match bokks nor goggle because there needs to be zero or more of the ‘O’ as defined. You should also note that using an asterisk in regex is more powerful in combination with other metacharacters. 

    2. Dot (.)

    The dot matches one single character, like numbers and letters, and it picks up whitespace as well.

    For example:

    • number . would match number 1, number 6, number 9, number A, but not number 10, number AB, or number.
    • .ool would match wool, tool, cool, pool, but not stool, drool, or school.
    • bo.k would match book, bokk, bouk, but not bok

    It only matches words with only one character, proving not very useful. However, Its true power lies when it is combined with other regular expressions like the asterisk. 

    3. Dot-Asterisk Combination (.*)

    This commonly used metacharacter combination matches zero or more random characters. This means that this combination matches everything, making it very powerful. Plus, you can use this to match anything in a string.

    For example:

    • my.* would match my book, my car, my house, but not car or book.
    • /products/.*/ clothes/ would match /products/ women/ clothes/, /products/ men/ clothes/, and /product/ kids/ clothes/.

    4. Pipe (|)

    This regular expression is the logical OR that should be used when you want to say OR. It is commonly used in Google Analytics to filter, set up, and track goals. 

    For example:

    • She| He would match either she or he anywhere in the data.
    • This|Those|That would match this, those or that.

    In filtering use cases, you can use the example of “ digital marketing| content writing” to get reports on articles under these two themes.

    5. Backslash (\)

    The backlash is an escaping character that you can use to turn a metacharacter into a regular character and vice versa. You can use this to clarify whether to read regular expressions that appear in plain text as regex or normal text. 

    For example, the forward-slash (/) signifies the beginning and end of a regex. If you want to treat this / character as a forward slash rather than a special character, you need to add the escaping backslash character like this -\/. 

    This way, search queries in regex would be search\/\?q= instead of /search/?q=

    You should add the backslash to treat the characters literally in the case of dollar signs and question marks. 

    This means USD\ $2 would match USD$2

    6. Caret (^)

    The caret is used to signify the beginning of a regular expression and placing it at the start of a regex connects the search to the beginning of the searched data. 

    For example:

    • ^book would match books, book bag, book reader, but not my book.
    • ^bag would match bag, bags, bags for school, but not school bag or church bags.
    • ^colour would match colour red, colour yellow, but not blue colour or red colour.

    Besides, It won’t be matched if there is anything before the word that is anchored by the caret.

    7. Dollar Sign ($)

    The dollar sign is used to signify the end of a regular expression – so it is the opposite of the caret. Placing a dollar sign at the end of the regex connects the search to the end of the data being searched. 

    For example:

    • book$ would match my book, red book, school book, but not school books or book bag.
    • colour$ would match red colour, blue colour, yellow colour, but not colours, colour book, or black colours.

    Without these anchors, the dollar sign and caret, the regex would match anywhere in the searched data. Besides, the caret and dollar sign are commonly used together to target exact matches.

    8. Question Mark (?)

    The question mark is used as a qualifier to check for zero or one occurrence of the preceding character. Using this means that the last character is optional, and it is useful if you want to target misspellings. 

    For example:

    • shoes? would match shoe or shoes, but not shoe1
    • blues?23 would match blue23 or blues23, but not blue123
    • Colou?rs? would match colour, colour, colors, and colours.

    9. Plus Sign (+)

    The plus sign is used as a qualifier to check for one or more occurrences of the previous characters.  It is very similar to the asterisk with minor distinctions.

    For example:

    • Abc+ would match abc, abcc, abccc, and abcccc.
    • bb+ook would match bbook, bbbook, bbbbook, but not book whereas bb*ook would match book, bbook, bbbook, and bbbbook. 

    10. Parentheses ()

    Parentheses in regular expressions work in the same way they do in mathematics – to create groups. This function is used to check for a string, and they capture the matched content as different elements.  You can use the parentheses to create and store variables when using them in an advanced filter. Plus, combining them with a pipe will enable you to create a list. 

    For example: ^/products/(romance|thriller|suspense)/books/$ would match /products/ romance/ books/, /products/ thriller/ books/, and romance/ suspense/ books/. 

    11. Square Bracket ([ ])

    The square bracket expression is used to make lists and match several iterations of alternative characters. You can also use this expression to exclude other iterations. 

    For example:

    • r[oi]pe would match rope and ripe
    • bag[123] would match bag1, bag2, and bag3

    Square brackets develop different marching conditions depending on the characters you place inside them. Besides, you can combine them with dashes to create strong lists. 

    12. Dashes (-)

    You can use the dashes to create linear lists to match, and it is best to combine them with square brackets. With this combination, you don’t have to list everything out.

    For example:

    • [1-20] matches any number from 1 to 20
    • [a-z] matches all lower-case letters
    • [0-10]% matches any number from 0 to 10 followed by %

    13. Curly Bracket ({ })

    The curly bracket is a qualifier that tells you how many times to repeat the preceding item.

    For example:

    • {1,3} means to repeat the preceding item at least 1 time and no more than 3 times
    • {5} means to repeat the last item 5 times
    • 2{2} means to check for 3 occurrences of the character 2 and the regex will match 22
    • pla{2-5}y would match plaay, plaaay, plaaaay, and plaaaaay, but not play.

    14. Exclamation Mark

    The exclamation mark is a logical NOT, and it is only used at the beginning.

    For example: [!A-Z] would match single characters that are not upper case letters.

    ga_acquisition_plus_dashboard_databox

    Optimize Google Analytics Reporting with Databox

    There is no need to log into your Google Analytics account every time you want to check how your website is performing. With Databox, you can get website performance insights at a glance, not only from your GA but also from all of the other tools you are using.

    It’s free and easy to get started.

    To visualize your most important metrics in one place with our custom dashboard software just sign up for a free account here. From there, you can build a custom dashboard yourself, get access to our library of 300+ pre-built templates, have our customer success team set up multiple dashboards for you, and much more.

    Author's avatar
    Article by
    Luca Tagliaferro

    Luca Tagliaferro graduated in Digital Marketing from a UK leading University and 2012 and in the last 10 years he became one the leading SEO consultants in the UK (according to Google). He specializes in international SEO, e-Commerce SEO, website auditing, and migrations.

    More from this author

    Get practical strategies that drive consistent growth

    Read some