If you’re a digital marketer, I’m sure you’ve come across Regular Expression in the tools and software you use frequently. Whether it’s to define your Google Analytics goals, filters or segments.
I highly recommend that you master Regular Expression because it will be very useful in digital marketing.
I wouldn’t call myself an expert but I do know some basic regular expressions and I hope that these will be helpful to you~!
Disclosure: Some of the links below are affiliate links. I will earn a small commission, at no extra cost to you, if you purchase through these links. Your support encourages me to continue blogging and help with the costs of hosting this site. Thank you!
What Is Regular Expression? Why Is It Important?
Regular Expression is like a digital language where you specify the data you’d like to include or exclude.
Regular Expression makes your data more specific. For example, you could narrow the data to only show information related to a specific web page.
Although you could always use ‘equals to’, ‘begins with’ or ‘contains’, Regular Expression is actually more precise. It helps you set the rules so only data that fits into the logic will appear.
Regular Expression Characters
There are characters that have special meanings in Regular Expression. Here’s a list of them.
Bear in mind that if you want these characters to be interpreted literally, place a backslash (\) before the character. For instance, if you want the dot to be interpreted as a decimal in an IP address, write it with a backslash (\.) so that the dot isn’t regarded as a wildcard. Don’t worry, you’ll understand this better when we explain about Escape.
Use the following wildcards if you want to replace or represent one or more characters.
Anchors are used for the start and end of a string.
If you want to show a group of data, do use the group characters.
As I’ve mentioned earlier, you can make regular expression characters be interpreted literally by placing a backslash (\) before the character. Meaning that you use the backslash (\) to escape regex characters.
Always Test Your Regular Expression
If you’re unfamiliar with using Regular Expression or if you’re unsure whether your string of regular expression is correct, do use a Regex tester.
There are many free testing tools online like https://regex101.com/. Just bookmark one of those so you can easily test your Regular Expression.
Or you can also just input your Regular Expression and see whether it works. For example, if you set a Contact Us goal in Google Analytics using Regular Expression, you can do a test form submission and see whether Google Analytics captured your goal completion successfully.
In addition to checking your Regular Expression as soon as you create it, you also need to ensure that you update the Regular Expression if there are any changes (especially for important things like Google Analytics goals when you change the thank you URL).
Now that we’ve learned some basic Regular Expression, let’s move on to some use cases where we see some practical examples of how Regular Expression is used in our daily digital marketing tasks.
We’ll start with Google Analytics.
I assume that almost everyone uses Google Analytics since it’s a free web analytics tool. Hence, these use cases might be more relevant to you.
Define Specific Goals
I use regular expression for all my Google Analytics goals due to its precision.
Let’s say your thank you page URL is /thank-you.html
You can use a RegEx like ^/thank-you\.html to accurately define the URL. This way, a goal completion is captured only if visitors land on a page that begins with /thank-you.html. I’ve also included a backslash so the dot is interpreted literally.
Do check out my step-by-step guide on how to set up google analytics goals~
Filter Your Report Data With Advanced Filters
Take for instance that I launched 5 Google Search campaigns to drive traffic to my site:
But I only want to see the data for 3 campaigns. You can use the advanced filters in Google Analytics for this. Just set the filter as Include Campaign/Campaign ID with matching regular expression of Brand|Industry|Generic. Google Analytics will then only show data of campaigns that matches Brand, Industry or Generic.
Create Precise Segments To Isolate & Examine Subsets Of Data
Similar to the advanced filter above, segments help you to isolate and examine subsets of data. However, unlike advanced filters where you have to manually create each time, you can create a segment then save it for future use. This saves you time where you only create once.
I recommend that you create segments that you use often. If you find yourself frequently using a certain filter, consider making this a segment instead.
Simply create a new segment and define your conditions with regex.
Create Custom Reports Based On Your Needs
If you need a customized report, you can create a custom report in Google Analytics.
Again, you can apply regular expression in the filters.
For example, if you want a custom report that only shows data from Samsung Galaxy. You could create a filter like Include Mobile Device Info Regex
- Galaxy S[0-9] which will result in S5, S6 Edge, S8, S8+, S9, S9+
- Galaxy S. which will result in S III, S IV, S5, S6 Edge, S8, S8+, S9, S9+
If you include both filters like what I did below, only results that match both filters will appear. In other words, we’ll only see sessions from S5, S6 Edge, S8, S8+, S9, S9+.
Google Tag Manager
Google Tag Manager is another tool where you can use regular expression.
I highly recommend that you use Google Tag Manager to house all your codes.
Specify The Conditions For Your Triggers So Tags & Codes Only Fire At Certain Pages
You can use RegEx for the triggers, where you specify the conditions for the tags and codes to fire.
.* is a powerful combination where it matches zero or more random characters. Simply put, it matches everything. You can create a trigger that fires on all pages with URL matches RegEx .*
Bear in mind, this is just an example. It’s easier to just use Google Tag Manager’s default All Pages trigger.
Google Data Studio
Google Data Studio visualizes your data into beautiful dashboards and reports. Again, you can use regular expression here.
Use Filters To Fine-Tune Your Data
Let’s say you’re running 2 campaigns and you want to include only data of these 2 campaigns
Instead of using the given OR clause, you could create a filter with RegExp match Competitors?
Pull all your metrics and data into one place with Supermetrics. Everything from PPC, SEO, social and web analytics, including Google AdWords to Instagram Insights.
Refine Your Data With Filters
For example, I could create a filter with campaign matches RegEx Jun2019$ so I only pull data from campaigns ending with ‘Jun2019’.
Meaning that I only pull data from GSN_Jun2019, FB_Jun2019 and LI_Jun2019 but not GDN_Jun2018 or Jun2019_YT.
Now It’s Your Turn
I hope you found Regular Expression useful. Just like how I use it frequently, do consider using Regex in your marketing tasks.
If you encounter any errors, feel free to ask me and I’ll try my best to answer you~