Regular expressions (RegEx) have some useful applications in Google Analytics. You can use them to create more accurate and versatile tracking scenarios than are available by default on the platform. Regular expressions are essentially special characters that instruct Analytics on which sets of data to capture and record. This post will highlight some scenarios where regular expressions are useful.
Regular Expressions in Google Analytics Goals
You can utilize a RegEx when setting up a goal in Analytics. Using this method for a goal is best when the URL is variable. Head match is another good option, but the regular expressions gives you a bit more control if implemented properly. The cool thing about regular expressions with goal URLs is that you can write your regular expression to match any part of the URL exactly and the rest of it to be variable. For instance, using the dot . matches any single character, the carat ^ tells analytics that your data must be at the beginning of the field and the * tells analytics to match zero or more of the preceding character. If you wanted to match a URL that always began with /regex but was always variable after that term, you could write ^/regex\./.*. Note that the . after the word regex has a back slash before it. This tells Analytics that the dot is actually a dot and not part of the expression. The period after the star is actually ending my sentence. The characters .* tells Analytics to match anything.
To use regular expressions in goals:
- Log into your Analytics account.
- Click on the Admin button in the top right corner.
- Click the profile you would like to create a goal for.
- Click the goals tab.
- Click +Goal.
- Select URL destination for the Goal Type.
- Make sure you select regular expression match from the Match Type drop down menu.
Excluding IP Addresses from Analytics Data
Another use for regular expressions is to exclude data from being collected based on user IP address. This is a common filter in Analytics that allows users to exclude things like their own machine or perhaps a contractor’s machine if there is work being performed to a website. Another reasonable use is the desire to exclude employee traffic from a profile so that more accurate data can be gleaned from a website used by a business. Businesses, especially larger ones, utilize dozens, hundreds and even thousands of computers. Entering in an IP address for every single machine is not practical, and also would not be effective. For these purposes, a range of IP addresses written as a regular expression works to exclude employee or other unwanted traffic from reports.
You can use all of the same regular expression rules that you would use for capturing specific URLs in your goal above; however, Google has a really great tool if you are excluding one or more IP addresses from data. It’s called the IP address range tool, and all you have to do is enter in at least one IP address and click the “generate RegEx” button. You then simply copy and paste the snippet of characters into your filter field.
To set up a filter excluding one or more IP addresses:
- Log into your Analytics account.
- Click the Admin tab in the top right hand corner of the page.
- Select the profile that you want to set your filter up in.
- Click the filters tab.
- Click the +New Filter button.
- Create new filter will be selected by default.
- Select the Custom Filter radio button.
- Select the Exclude radio button.
- In the Filter Field drop down menu select IP Address
- Paste your RegEx from the range tool into the Filter Pattern box.
- Make sure the “no” radio button is selected for Case Sensitive.
- Click save.
Be sure you are applying this filter to a copy of your default profile. You never want to apply filters to the default profile. Always apply filters to copies of the default profile. That way, if something is done wrong or, for some reason, you want to look at the data again, it will still be there.
Excluding Data in Reports
Another handy use for the RegEx is to exclude or include specific URL or other data from reports. This trick is cool because you aren’t actually changing any of your data — you’re just telling Analytics to do it for that moment in that specific report. After you leave that report or Analytics in general, your reports go back to normal. For instance, in your content reports, you just click on the advanced dimension filter and let Analytics know which pages you would like to see or not see in the report.
To use regular expressions to exclude URLs from content reports:
- Log in to Analytics.
- Select the content report that you want to manipulate.
- Click on the link that says Advanced at the top of the report.
- Choose include or exclude depending on your purposes.
- Note that you must use a Dimension in order to use a RegEx.
- Make sure “Matching RegExp” is selected.
- Write your regular expression in the box and then click Apply.
This use of the RegEx is particularly useful when sifting through many URLs that may be very similar. You can then only view the ones that have a particular parameter in them, for example. Note that you can add more dimensions and other regular expressions if you wish; however, you must remember how Analytics applies these. It will apply the filters in the order in which they are applied to the report (the first one first). Subsequent filters may not work if you have thrown the data out or told Analytics to keep it using the first filter in the series.
Writing regular expressions can be very confusing at first. It is almost like learning a new language, but is even more cumbersome because they use characters that already have alternative (and well-worn) meanings for us. There are tons of great resources on the web for learning about regular expressions. Remember to create duplicate profiles in your Analytics account so that if you make a mistake or want your data back when dealing with filters, it will still be around.
You can learn more about using regular expressions in Google Analytics here.
How do you use regular expressions in Analytics? Do you find them confusing or have you mastered them already? Let us know what you think by leaving a comment or dropping us a line.