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 …

How to Track Marketing Campaigns in GA4

How to Track Marketing Campaigns in GA4

Holiday campaign tracking provides insights into what's working or not working. But you need to set it up. It's 5 weeks and 3 days until Black Friday, and even if your business is not located in America, this retail event kicks off the holiday shopping season around...

read more