DEV Community

Cover image for Why Your GA4 Regex Keeps Failing — 2 Rules That Fix 99% of It
Count Of Tuscany
Count Of Tuscany

Posted on

Why Your GA4 Regex Keeps Failing — 2 Rules That Fix 99% of It

If you ever pasted a regex into Google Analytics 4 and got zero rows back
while the exact same pattern works fine in your editor — you didn't write a
"wrong" regex. You wrote one that GA4 doesn't run the same way.

Two differences explain almost every failure.

1. GA4 matches the whole string

GA4's regex condition is a full match, not a "contains".

/blog/
Enter fullscreen mode Exit fullscreen mode
  • In most regex engines, this finds /blog inside /blog/ga4-guide.
  • In GA4, it matches only the exact string /blog. /blog/ga4-guide → zero rows.

Fix — wrap it:

/blog/.*      → everything under /blog/
.blog. → any path containing "blog"
Enter fullscreen mode Exit fullscreen mode



  1. GA4 uses RE2 — no lookahead, no backreferences

GA4 runs Google's RE2 engine: fast and safe, but it does not support:

  • lookahead (?=…) / (?!…)
  • lookbehind (?<=…) / (?<!…)
  • backreferences \1

A pattern like ga4(?=-tips) fails silently. And because RE2 can't assert,
"match X but not Y" logic is better expressed as two filters in GA4 itself
(one include, one exclude) instead of one clever regex.

Patterns that actually work in GA4

You want Regex
Pages under /blog/ /blog/.*
Product detail /products/<slug> /products/[^/]+
Event ending in _click .*_click
Source is google or bing `(google
URLs with a query string {% raw %}.*\?.* (use page_location, not page_path)

Test before you paste

Every pattern above can be checked live against sample data in a free,
browser-only tester — no login, and no data ever leaves your device:

Both run entirely client-side, so what matches in the tester matches in GA4.

Top comments (0)