Using Regular Expressions in Google Analytics

Published April 29, 2015
The more time you spend managing a Google Analytics account, the more you’ll wish you could do… well, more. The default Google Analytics reports don’t give you the level of control you need to get the information you want – you need to look at the data on a more granular level. For example, you may find the standard search bar limiting in filtering data when trying to show a specific list of sources or keywords. Regular expressions are a powerful filtering tool and give you a higher level of control over the data you see in your Google Analytics reports.
A regular expression (sometimes shortened to regex) is a string of characters used to create a search pattern. It’s similar to a wildcard – helping you to be more purposeful in your filtering. But, rather than just having a single wildcard character, regex provides a variety of special characters that can be assembled into filters.
Here are some of the special characters used in regular expressions:

[table th="0"]
^,A caret signifies the start of a line. For example\, filtering search terms by ^megalytic would match any instances where megalytic was the first word in the search term.
$,A dollar sign signifies the end of the line. For example\, filtering search terms by megalytic$ would match any instances where megalytic was the last word in the search term.
|,A vertical pipe essentially means “or.” For example\, filtering sources by google|bing would match instances of either google or bing.
.,A dot matches any single character except for a line break. For example\, filtering by megal.tic would match instances of either megalytic or a misspelling like megalitic.
\ ,A backslash “escapes” the character following it. For example\, when entering a URL\, should you place a backslash in front of the period\, it would ensure it’s treated not as a wildcard but as part of the text string\, as in megalytic\.com.
*,A star matches zero or more of the previous character. For example\, megalyt*ic would match megalytic\, megalyttic\, or megalytttttttic.


Blog Image - Regular Expression


Implementing Regular Expressions in Google Analytics

So, where can you use regular expressions within the Google Analytics interface? A number of possible locations exist, from one-time searches to Segments to Filters to custom reports that can be saved for long-term use. We’ll break out some of our favorite uses below.

Advanced Search

To use regex for advanced searches, select “advanced” from the search bar that appears above most reports. From there, use the dropdown after the dimension selection to choose “Matching RegExp.” You can then complete setting up the filter to customize your search.


Google Analytics Advanced Search



You can also use regular expressions when creating a Segment. Segments allow you to define subsets of Sessions or Users based on custom criteria, with the ability to apply throughout reports within Google Analytics and save for future use.


Google Analytics Apply Segment


To create a Segment, start in the Reporting section of Analytics and select Add Segment in the area above the graph. In the box that appears, select the red New Segment button.


Google Analytics Create New Segment


Now, you can choose the criteria for your Segment. For instance, if you want to include only Sessions from Facebook or Twitter, you can select Traffic Sources and choose “matches regex” from the dropdown by Source. Then, enter facebook|twitter to match instances where the Source contains facebook or twitter.

View Filters

Google Analytics Filters allow you to control the data included in a particular View. You can utilize regular expressions to add a high level of control and efficiency when creating a Filter.

For instance, our article on filtering out fake referrals talked about the issue of removing erroneous data from the results shown in reports. A Filter allowed us to exclude the bogus traffic generated by spammers.


Google Analytics Filter Definition


Custom Report Filters

When creating a Custom Report, you can choose to include or exclude data based on regular expressions. In the example below, we wanted our report to show traffic from multiple states serviced by this particular client. Using a filter, we entered a list of states separated by the | symbol.


Google Analytics Custom Report Filter


Practical Uses for Regular Expressions

Now that we’ve covered areas you can enter regular expressions into Google Analytics, what are some practical uses? Regular expressions can help you be more efficient in filtering data, as well as segmenting on a more granular level than you could otherwise.

Viewing a List of Referrals

Regular expressions can be used to summarize data from a select list of sources. For example, this brand engaged in a public relations program involving placements on a number of sites. To break out which of these sites referred traffic back to the brand’s site, a Segment can include a series of these placements, escaping the periods in the URLs and separating sites with the | symbol.

Without regular expressions, you’d have to search manually for each of these sites within the referral list or export data to Excel to filter there. Below, see where a list of placements was entered when creating a Segment.


Google Analytics PR Segment


Applying this Segment to the Referrals report will now show data strictly from those sites. You can now see summarized results from all of these particular placements, as well as data for each all within one report.


Google Analytics PR Referrals


Filtering by County

Regular expressions can allow you to organize data by dimensions not available by default within Google Analytics. For instance, an agency client segmented its marketing by county and wanted to see data broken out that way. However, Google Analytics does not offer an option to see geography by county.

Instead, we were able to obtain a list of cities for each county the client wished to target, and we used regular expressions as a way to filter. After creating the first Segment for Albany County, we entered the cities in that county via regular expression into the Location box. Now, the client was able to see data for Albany County with this Segment applied.


Google Analytics Segment by Country


Filtering out Multiple IPs

Generally, you should filter out traffic from within your office and from anyone on your team who will be accessing your site, so your own visits don’t skew analytics data. However, if you have people working from several locations, adding filters for each can be time consuming to manage. Regular expressions allow you to create a single filter to exclude traffic from multiple IPs.

To create this filter, go to the Admin section of Analytics, and then select Filters under the View of your choice. Add a new Filter and choose Custom for Filter Type. Choose Exclude, and then select IP Address for Filter Field. You can then enter a series of IP addresses in the text box.


Google Analytics Exclude IPs


Now, after applying this filter, visits from these IP addresses will be excluded from this Analytics View.


Regular expressions are an important tool to be mastered by anyone who needs to do significant work in Google Analytics. You’ll save time and be able to exercise a greater level of control over data. Spend some time learning about the basic setup of regular expressions and brainstorm ways to use regex in displaying data relevant to the site(s) you work on. Search for ways others have used regular expressions for examples of how you can implement.

For a detailed tutorial in regular expressions, see:



When the client first came to you, you talked up the value of Google Analytics. You emphasized the importance of seeing where your traffic was coming from. You went on and on about how Google Analytics can show traffic sources to pinpoint whether people came from search, social media or a specific site referral, and how valuable this data was. You sold them on it, so much so that your client looked forward to receiving that first report, the magical day when they would finally understand where visitors were coming from.
But then the report came, and it looked like this:



It showed that 10% of your client’s traffic came from “(direct)/(none)”. What does this label mean? How do you explain Direct traffic to your client? Better yet, how do you explain “none”?
Let’s take a closer look at understanding Direct traffic in Google Analytics and how we can address it with clients.
Remember how your mom told you not to stand too close to the television because it might hurt your eyes?

The same rules can apply to data. If you’re too close, you may miss the patterns and trends that are crucial to understanding your website’s performance. You can’t judge a site’s performance looking at data in the bubble of a single day, you must consider any day’s traffic compared to the days before and after.

Google Analytics makes it fairly easy to analyze trends over long periods of time. But it also allows you to stand right in front of that TV, to look at more granular levels of time, right down to the hour.
There’s a better way to get that close to the data, without burning your retinas. We’ll cover how to analyze traffic effectively in today’s post.
Digital marketers spend a lot of time focused on PPC and SEO campaigns in order to drive desirable traffic to a website. The phrases we’re ranking for and bidding on get meticulous attention, so much so that we often forget about some of the other ways that visitors find us.

We put a tremendous amount of the effort we put into reviewing organic search data and PPC campaign performance in analytics. But how closely do we monitor referral reports?

If that’s not a channel you review regularly, you may be missing out on seeing traffic that is coming directly from links you’ve obtained around the web, local business listings, news mentions, and more. Many times, links are only considered as a means to an end, a metric that Google uses in determining how to rank sites in the SERPs (search engine results pages). But the fact is, many of a site’s links may be directly contributing to its traffic.

In this article, we’ll review how to look at referral reports in Google Analytics, and some of the many ways to use that data to better inform your web marketing decisions.