Insights

How To Use Regular Expressions In Google Analytics To Filter Data

What Are Regular Expressions In Google Analytics?

Regular expressions (regex for short) help speed up and create flexibility in the way we include/exclude data and information in our Google Analytics reporting.

Google Analytics supports regular expressions for:

  • View Filters
  • Goals
  • Segments
  • Audiences
  • Content Groups
  • Channel Groupings

This blog will focus on setting up View Filters in Google Analytics using regex for data collection as a way to keep your analytics clean and organized in one view. Think of regular expressions as a way to search for and match certain information and create specific definitions in Google Analytics. These regular expressions can also be used in any view to sort data.

If you've never used regular expressions before, don't worry, this blog will include a summarized Regex cheat sheet, so you don't need to memorize it.

Quickly Filter Pages In Google Analytics View With Regex

Here is one example of where a regular expression can be inputted to show a specific result in our Google Analytics View. For this particular example, we want to show only pages containing the following terms in the URL:

  • XP
  • XM
  • Pricing
  • Azure

To sort the data in any Google Analytic View, we will use the advanced search function under Behaviours > Site Content > All Pages in your Google Analytics property. This will include all of the pages included in our View to be sorted using a regex formula. The Primary Dimension we are using is "Page", and this will apply the formula to only Page URLs.

Click advanced next to the search bar above the data table and make sure "Page" is selected. From the next drop down, you must select "Matching RegExp," otherwise the formula will not yield any results. Click "Apply" when you are finished. The easiest way to input this formula into Google Analytics is using the "|" character for "or" in regular expressions. The formula itself looks like this: "xp|xm|pricing|azure" and will yield all of the pages containing any of those terms in the URL.

Where to use a regular expression in Google Analytics > Behaviours > Site Content > All Pages

The updated table will now only display the pages containing "XP|XM|Pricing|Azure" within our Google Analytics view.

Regex Metacharacter Cheatsheet For Google Analytics

Wildcards

Syntax Meaning Examples Matches
Dot (.) Any Single Character - Matches any single character (letter, number or symbol) 1.1
  • 111
  • 1A1
Question Mark (?) Starts With - Use the caret to match the following adjacent characters at the beginning of a string. 10?
  • 1
  • 10
Plus Sign (+) One or more of the previous item - Matches the preceding character 1 or more times. 10+
  • 10
  • 100
  • 1000
Asterisk (*) Everything - The asterisk or star matches the preceding character 0 or more times. 1*
  • 1
  • 10
  • 100
  • 1000
Pipe | Or - Use the pipe to create an OR condition in an expression. demo|example
  • demo
  • demos
  • example
  • examples

Anchors

Syntax Meaning Examples Matches
Caret (^) Starts With - Use the caret to match the following adjacent characters at the beginning of a string. ^St
  • Start here
  • Stand here
  • Stop here
Dollar sign ($) Ends With - Use the dollar sign to match the preceding, adjacent characters at the end of a string. end$
  • Temporarily suspend
  • We’re going off the deep end
  • Let's Pretend

Groups

Syntax Meaning Examples Matches
Parentheses ( ) Group - Matches the enclosed characters in exact order anywhere in a string. (10)
  • 10
  • 101
  • 1011
Square brackets [ ] List - Use square brackets to create a set of characters to match. t[aeo]p
  • tap
  • tep
  • top
Hyphen - Range - Use the hyphen along with the brackets to create a range of characters to match.
  • [a-z]
  • [A-Z]
  • [0-9]
  • All lower-case letters
  • All upper-case letters
  • All numbers

Escape

Syntax Meaning Examples Matches
 Backslash (\)  Escape a special character - It will interpret the next character literally. 0/.0/.0/.0/. 0.0.0.0

Setting Up Google Analytics Filters

In Google Analytics, we can limit or filter data in a view. Once your website data is processed, Google Analytics will apply the filters you have outlined in your Admin settings. This filtered data is available in your Google Analytics property through different views.

Remember: Once the data in Google Analytics is filtered, there is no way to retrieve the information excluded from that view. Google Analytics does not prorate data, and there is no way to recover it later. Ensure you have one view in your Google Analytics property dedicated to raw, unfiltered data, so you always have access to it.

Let's Set Up Regex Filters In Google Analytics

Step 1

In your Google Analytics property, ensure you are in a new view before creating filters for your data. Click "Filters" to begin.

Screenshot of admin screen in Google Analytics

Step 2

Click "New" to create a filter. The order you create all of the filters here will dictate which order Google Analytics will process it. Starting from the top going down the list.

Screenshot of adding a new filter into a Google Analytics view

Bonus!

One tip for cleaning up your filtered data is to apply a lowercase filter. This way lowercase filter ensures there are no duplicate URLs due to capitalization. It converts the field's contents into all lowercase letters but doesn't affect special characters or numbers. Not to mention it displays your data much cleaner!

Under new filter settings input the following specifications:

  • Name your Filter
  • Choose a custom Filter Type
  • Select Lowercase
  • Specify the Filter Field
  • Click Verify This Filter to test it
  • Click Save

Screenshot of setting up a lowercase filter in Google Analytics

Step 3

After we add the lowercase filter, let's get started with our Regex pattern. To use the regular expressions filter in your Google Anlaytics view, create a new filter. Under new filter settings input the following specifications:

  • Name your Filter
  • Choose a custom Filter Type
  • Select Include
  • Specify the Filter Field
  • Enter the Regex into Filter Pattern
  • Click Verify This Filter to test it
  • Click Save

Screenshot of Google Analytics applying the regex sitecore|partner pattern to the view

Rememeber to create your regex filter second, to ensure the lowercase filter is applied to the data first.

Screenshot of Google Analytics filter page with the lowercase and regex filters in order

The regex filter we used was "sitecore|partner" to display data from web pages including sitecore OR partner in the Request URI (uniform resource identifier).

Remember, it takes Google Analytics approximately 24 hours to begin populating data for new filters. Stay patient, and check back tomorrow. To see the filtered reports after the data has populated in your Google Analytics, check under Behavior > Site Content > All Pages.

Screenshot of adding a new filter into a Google Analytics view

Happy Filtering!

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting
 

Meet Theresa Gutierrez

Sitecore Strategist | Sitecore Strategy MVP

⛳ 🌮👩🏻‍💻

Theresa, aka 'T', is a high-energy marketing creative with 8+ years of experience across various industries. She's passionate about delivering strategy and design, with a focus on Sitecore SaaS technologies. T is an avid golfer and loving dog momma, appeased only by delicious food and a bevy of something bubbly. Cheers!

Connect with Theresa