- ^ → 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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: Regular-Expressions.info.