CampaignMorph Logo
Technical SEO

Regex Cheat Sheet for Digital Marketers (GA4 & GTM)

Ganesh Kanse
#Technical SEO #Data Analytics #Google Analytics #Automation
Regex Cheat Sheet for Digital Marketers (GA4 & GTM)

Why Marketers Need to Know Regex

Regular Expressions (Regex) look like a completely foreign language: ^/blog/.*$. However, they are arguably the most powerful filter mechanism available to a digital marketer.

While developers use Regex to validate emails in databases, marketers use Regex in Google Analytics 4 (GA4), Google Tag Manager (GTM), and Google Search Console to filter massive datasets.

Instead of creating 15 different "URL contains" rules in GTM to fire a conversion tag, you can write a single line of Regex.

The Core Regex Cheat Sheet

You don't need to learn everything about Regex. As a marketer, mastering these 5 symbols will solve 95% of your problems.

1. The Pipe | (OR)

The pipe simply means "OR". This is the most common Regex used in GA4.

  • Pattern: shoes|shirts|hats
  • Matches: Any URL containing "shoes", "shirts", or "hats".

2. The Caret ^ (Starts With)

The caret anchors your search to the absolute beginning of the string.

  • Pattern: ^/blog
  • Matches: /blog/seo-guide but NOT /category/blog/seo-guide.

3. The Dollar Sign $ (Ends With)

The dollar sign anchors your search to the absolute end of the string.

  • Pattern: thank-you$
  • Matches: /checkout/thank-you but NOT /checkout/thank-you-page.

4. The Dot . (Any Character)

A dot matches any single character (except a line break).

  • Pattern: b.t
  • Matches: "bat", "bet", "bit", "bot".

5. The Asterisk * (Zero or More)

The asterisk applies to the character immediately before it, meaning "repeat this zero or more times." When combined with the dot (.*), it means "literally anything."

  • Pattern: /category/.*/shoes
  • Matches: /category/mens/shoes, /category/womens/running/shoes.

Test Your Regex Before You Break Your Tags

The danger of Regex is that a single typo can cause your Google Tag Manager trigger to fire on every single page of your website, ruining your conversion tracking.

Always test your expressions before deploying them to production.

Use the free CampaignMorph Regex Tester.

How to test:

  1. Paste your proposed Regex pattern into the tool.
  2. Paste a list of URLs (both the ones you want to match, and the ones you want to exclude).
  3. The tool will highlight exactly which URLs are matched, ensuring your logic is flawless before you update GA4 or GTM.