Regular expressions in Google Analytics

Google Analytics provides a lot of data and reports. Most come in the form of tables that can contain over 100,000 rows. Sometimes a normal text search is not enough to find something in it. Regular expressions allow complex searches and can be used in many places in Analytics .

Regular expressions are not a special invention of Google Analytics, you can use them in many programs and services, e.g. Data Studio , MySQL or JavaScript. So it’s worth getting started!

What are regular expressions?

You can define complex search queries with a regular expression or regex for short. There are a number of meta characters as placeholders and to define groups or sets. Regular expressions thus give more possibilities than the usual matches or begins with . Regex also enable search-and-replace rules and are therefore a powerful tool when working with data.

A few common examples:

Regular expressiondescription
192 \ .168 \. [789] \.Matches all IP addresses from the network range 192.168.7., 192.168.8. and 192.168.9. each with the digits 1 to 255.
(www | blog) \. tirami \ .bizMatches the host names www.tirami.biz and blog.tirami.biz.
\ .pdf $Fits all files with the extension .pdf.
/service/.*\.pdf$Matches all files with the extension .pdf in the / service directory or a subdirectory of / service.
^ / .. / service /Matches all URLs whose first directory is two characters long, for example / de / service / or / en / service / or / it / service /, but not to / global / service /.
^ / [^ i] [^ t] / service /Matches all URLs with a service directory, except the Italian (it).
/ blog / \ d +All pages in the / blog / directory that begin with a number

How do I define regular expressions in Google Analytics?

With a regex you define placeholders or groups with meta characters. These are in a regex, not for the actual character. The point . means any character. That’s why you have to explicitly state in many places that you are using a regex and not a simple text string.

The most important meta characters for regular expressions:

characterdescriptionexample
.Placeholder for any charactert.rami matches the character strings tirami, tarami, torami and t6rami.
*Repeat previous character as often as you like. The symbol can also be completely absent.ti * rami matches tirami, tiiiiiirami and trami.
+Repeat previous character as often as you like. The character must appear at least once.ti + rami matches tirami and tiiiirami, but not trami.
?The previous character may or may not appear.ti? rami matches tirami and trami.
|Two characters can appear alternatively. Corresponds to a link with “or”.a | b matches a or b.
^The following characters must be at the beginning of the character string.^ / service matches the / service page, but not / customer service.
$Preceding characters must be at the end of the character string.products / $ matches / products /, but not / products / wines /.
()group several strings, for example for an OR link(Red | White) wine goes well with red wine and white wine.  
\Cancels the meta function for the following special character. This makes the entry a common character.tirami.biz fits on tirami.biz, but also on tirami8biz. tirami \ .biz on the other hand only matches tirami.biz (with a point between tirami and biz).

Meta characters for lists and character classes:

characterdescriptionexample
[]List of characters that can appear at this point in the character string. Can with *, + and? be used.t [iao] rami matches tirami, tarami and torami.t [iao] + rami matches tirami, tiiiirami, but also tiaoiaoirami.
Within lists, the minus indicates a range of connected characters.[AZ] corresponds to a list with all capital letters of the alphabet.
^The following character is considered negative within a list, that is, it must not be in this position.t [^ i] rami matches tarami and torami, but not tirami.
\ dany digit 0 to 912 \ d matches 123, 124 and 128. But not 12A
\ DSign that there is no digit12 \ D matches 12B but not 128
\ wLetter, number or underscore
\ WSign that there is no letter, number or underscorein URLs \ W matches # or?
\ swhitespace – space or tabRegular \ sexpression matches regular expression
\ Sany character that is not a space

Where can I use regular expressions in Google Analytics?

Google Analytics allows the use of regular expressions in some places:

Table filter

You probably know the search box that appears above most reports in GA:

Table filter in Google Analytics

Here you can enter a text and GA looks for all lines in which this text occurs. With a click on Advanced you can select further search options, including Corresponding RegExp .

Extended table filter in GA

Tip : The search field automatically detects when a regular expression is entered. So you don’t have to go to Advanced and change the menu. A regular expression without metacharacters is processed like a “contains”.

Segments

Several conditions can be used for a definition in segments , e.g. if you want to search for two cities:

Multiple conditions in GA segments

This segment is defined as users who come from Cologne or Munich . This OR can also be expressed with a regex:

Regular expression segment

The vertical line means something like either the text on my left or on my right must fit . Entering two entries doesn’t make it much easier, but you can list as many elements as you want in a row. With many entries, this saves some clicking.

Dashboard widgets

In the classic GA dashboards , a filter can be stored as a regex for each widget.

Regular expression in a dashboard widget

As with the segments, you can add multiple filters . However, these filters are always linked with AND. One line only Cologne and one line only Munich are mutually exclusive. You can create an OR list with a regex.

Custom reports

In custom reports you can define a regex as a filter that restricts the data displayed.

RegEx filter in custom report

Fun fact : Here it says RegEx in the menu , while it says regular expression for segments and RegExp in the table search field .

Goals

A regular expression can be specified when defining a goal. This is especially useful if you want to combine several pages / URLs in one destination.

Regular expression goals

The expression “/ jobs / [az]” describes all pages that are within the / jobs / folder – in this case the job offers.

By the way: If you choose regular expression for the target, you can also specify regex in the steps of the (optional) funnel.

Tip : Test your filter pattern for a target in the search field of the page report. There should come all sides in the result that you want to have as a goal.

filter

With filters you can control the incoming data of a data view. You can

  • only show selected data in the data view
  • exclude certain data
  • Change data

With the user- defined filter type , the filter patterns are defined with regular expressions. In order to use the search and replace and advanced filters you have to work with regex.

Extended filter with regular expression

Translated, this filter means:

  1. Take everything (. *) That is in field A (host name)
  2. Take everything (. *) That is in field B (URI)
  3. and write both $ A1 and $ B1 one after the other again the URI

Target groups

Target groups work almost identically to segments and allow regular expressions as a condition.

Regular Expression Audiences

Content groups

Content groupings allow you to summarize certain pages and view them together. You can define a group with multiple rules, or use a regex (or combine both).

Content grouping with regular expression

The regex “/ blog / \ d +” describes all pages that contain “/ blog /” and immediately followed by at least one number “\ d +”.

Channel groups

The channel groupings combine different sources in order to view the users together. You can also use regular expressions here.

Channel definition via RegEx rule

Note: Regexs work slightly differently for channel definitions than in the other cases. For channels, the specified pattern must match the entire source (medium, etc.). It is not enough that only one part fits. So google only matches the source google , but not google.com

Conclusion

With regular expressions you can define patterns more precisely than with a simple contains or starts with a filter. In many cases, using a regex is faster than combining multiple text filters. Some requirements can even be implemented only with regex, some search-and-replace filters. Not only does Google Analytics understand regular expressions, other Google tools and many other services can also use them. So it’s worth the effort to learn them 🙂

Leave a Reply