Regex or Regular Expressions in Google Analytics Custom Reports

Regular expressions, or regex for short, is a string of text used to describe a search pattern. In simple terms, it is find and replace on steroids. In this specific example, we are not replacing anything. There are a ton of applications ranging from parsing data on a web server to manipulating high volumes of text files to what makes SEO permalinks possible on Linux servers, which is done using Apache’s mod_rewrite by editing the .htaccess file. In Google Analytics, you can create Custom Reports using regular expressions to build customized reporting views that allow you to get to the data that you want in the format that you want it presented. I will show you how.

To keep things simple, I will use the example that I want to build a Custom Report to see the traffic coming from Facebook. Traffic from Facebook generally comes in from facebook.com, m.facebook.com, l.facebook.com and lm.facebook.com. The l.* and lm.* are part of their Link Shim release in 2008, which helps protect Facebook users from malicious URLs.

Custom Reports Basics
To create a Custom Report, click on the Customization tab on the top and then click the “New Custom Report” button.

I gave mine the title of “Facebook Traffic” and left the report Name the default. Under Metric Groups, I added “Users”, “New Users”, “Sessions”, “% New Sessions”, “Pageviews” and “Avg. Session Duration”. Under Dimension Drilldowns, I added “Source / Medium”, “Full Referrer” and “Landing Page”. You can change the metrics and dimensions to the ones that you want to see but for this purpose, you can use the ones that I have used to make it easier on your end. If you click on Save, you will see all traffic—I will show you below several methods to filter out just traffic from Facebook.


Google Analytics Custom Reports

Go back to Customization, and under the Actions dropdown, click on Edit. Under “Filters – optional”, click on “add filter” and select Source (under Acquisitions).

Method 1 – Easy
You can type in facebook.com as the Source and save but you would need to create a separate report for “m.facebook.com” and the other subdomains. This method is easy but inefficient in this case scenario.


Google Analytics Custom Reports

Method 2 – Intermediate
Under the dropdown that says Exact, change that to Regex and in the text field, type in the following: ^facebook\.com|l\.facebook\.com|m\.facebook\.com|lm\.facebook\.com$

The carrot ^ indicates the start of the regular expression and the dollar sign $ indicates the end of the regular expression. The pipes | indicate an OR operator, and the \ in front of the periods in the domain names are escape characters because a period in regular expressions is a special character. Note that Google is smart enough to detect without the escape characters but to be safe, I would throw it in there.


Google Analytics Custom Reports Regex

This method is sufficient for this case scenario but let’s say we want to cover our bases in the event Facebook sends traffic through newly created subdomain or simply ones that we are not yet aware of. We would use Method 3.

Method 3 – Advanced
Make sure the dropdown still says Regex and now in the text field, type in the following: ^.*\.facebook\.com$

The carrot ^ indicates the start of the regular expression and the dollar sign $ indicates the end of the regular expression. The period followed by asterisk .* signifies any string of text, and the back slashes \ are escape characters for the periods in the domain name.


Google Analytics Custom Reports Regex

Below is a screenshot of the report using Method 3—we have captured Facebook referral traffic from sources that we did not anticipate. Well, I did but I leave a surprise for you as my reader 😉


Google Analytics Custom Reports Regex