Cookie Settings
Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

Other cookies are those that are being identified and have not been classified into any category as yet.

No cookies to display.

Blog

+ Free advice and other musings

How to Write Regular Expressions for Google Analytics Goals

by | May 11, 2019 | Google Analytics

What are regular expressions in Google Analytics?

In Google Analytics, you can use regular expressions to create filters, goals, and funnel steps.

Regular expressions (RegEx) is a way to check for patterns in a string. A string is a sentence, word, URL, etc. It’s a way to find matches. For example:

  • Filter: “show me all the inbound traffic that matches facebook, twitter, instagram, or youtube”
  • Goal: “track every time someone landings on thank-you.html or thanks.html”

Why is it worth knowing regular expressions?

RegEx is a powerful way to create advanced queries that combine multiple things. If I’m interested in creating a conversion goal for people who click any of the “Follow Us” icons then I do not want to create separate goals for Facebook, Twitter, Instagram, I want to create a single goal for Follow that tracks clicks to any of those. 

To do that, you must use Regular expression, not Equal to or Begins with.
 

How do you write regular expressions?

RegEx uses special characters to find matches in the string. These include backslash, pipe, question mark, parentheses, square brackets, dash, dot, plus, star, caret, dollar sign.

The Pipe in RegEx means “either”: Notice the screenshot below uses | between the words twitter|facebook|instagram. That says, “hey Google Analytics, track as a goal conversion when the event is Outbound Link Click and the match is either twitter|facebook|instagram

(Note: I will write the target strings for RegEx in bold vs. putting quotes around the string so there’s no confusion about what to write or include.)

Now, sometimes things get more complicated and the string includes a question mark, dot or slash. These special characters have another use in Regular Expressions. We use backslash to denote their plain use. 

The Backslash in RegEx means “escape”. In the screenshot above is com\/your-account. In the client example above, the match is to:

twitter.com/your-account
facebook.com/your-account
instagram.com/your-account

RegEx finds a match in the string, which means we don’t have to write the whole thing, just the unique part. The slash is a special RegEx character so backslash has to appear before the slash to make it a plain slash. 

com\/your-acount

 

Here’s a combination of Pipe and Backslash. Perhaps you want to track as a goal when people click to share on Twitter, Facebook, or pin on Pinterest.

In the Goal details > Label, we would write the Regular expression:

\/share|\/pin

i.e., match url strings that include either

https://twitter.com/share
https://www.facebook.com/sharer.php
http://pinterest.com/pin

 

 

Want to learn more about Regular Expressions? Lunametrics has a great guide:

https://www.lunametrics.com/regex-book/Regular-Expressions-Google-Analytics.pdf

Or Analytics Mania has a great post on setting up click tracking. 
https://www.analyticsmania.com/post/google-tag-manager-click-tracking/

 

Your turn

There are lots of different ways to write RegEx. The above example works but there are other ways to write the regular expressions. 

You may also like …

Big Changes Are Coming to Search

Big Changes Are Coming to Search

How AI Overviews and the Rise of Conversational Search Are Redefining SEO Google’s May 2024 announcement of AI Mode in Search and the expansion of AI Overviews marked a pivotal shift in how search works — and how SEO professionals need to think. If you’re a marketer...

read more