How to Use Regex in Google Analytics 4

Cody Schneider7 min read

Tired of applying dozens of filters just to see data for a specific group of pages in Google Analytics 4? Regular expressions, or regex, are your secret weapon for creating powerful, flexible filters with a single line of text. This article will walk you through the basics of regex and show you how to use it in GA4 with practical examples to level up your reporting.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What is Regex, Really?

Think of regex as a superpower for pattern matching. Instead of telling Google Analytics to find an exact match for a page, a campaign name, or a traffic source, you give it a "pattern" to look for. It’s like using a wildcard (*) in a file search on your computer, but infinitely more powerful.

For example, instead of filtering for /page-1, then adding another filter for /page-2, and another for /page-3, you can use a single regex pattern to match all pages that start with /page- in one go. This saves time and makes your analysis way more efficient, especially when dealing with hundreds or thousands of URLs, campaign names, or other dimensions.

The Essential Regex Characters to Know for GA4

Regex can look intimidating at first, but for 95% of what you'll do in Google Analytics, you only need to learn a handful of special characters, often called "metacharacters." Let's break down the most common ones.

  • The dot .: A wildcard that matches any single character. For example, /page-. would match /page-1, /page-2, and /page-A, but not /page-10.
  • The asterisk *: Matches the preceding character zero or more times. The pattern /blog/post-* would match /blog/post- (with zero characters after the hyphen), /blog/post-1, and /blog/post-amazing. It's a greedy character.
  • The plus sign +: Matches the preceding character one or more times. So, /page-+ would match /page-1 and /page-123, but it would not match /page-.
  • The question mark ?: Makes the preceding character optional, matching it zero or one time. For instance, colou?r would match both "color" and "colour".
  • The pipe |: This means "OR". It lets you combine multiple search terms into a single expression. For example, /about-us|/contact would match either /about-us or /contact.
  • The caret ^: Matches the beginning of a string. Using ^/blog ensures you only match URLs that start with "/blog", ignoring something like /category/blog.
  • The dollar sign $: Matches the end of a string. The pattern /checkout$ would match pages ending exactly with /checkout, ignoring any URLs with tracking parameters like /checkout?source=facebook.
  • Parentheses (): Groups expressions together. This is often used with the pipe character. For example, ^/products/(shirts|pants)$ would match only /products/shirts and /products/pants.
  • Square Brackets []: Matches a single character contained within the brackets. For instance, item-[AB12] would match item-A, item-B, item-1, and item-2.
  • **The backslash \**: An "escape" character. What if you actually want to search for a dot (like in a domain name) instead of using it as a wildcard? You "escape" it with a backslash. For example, to match example.com, you'd use the pattern example\.com.

Where to Apply Regex in Google Analytics 4

You can use regex in several key areas within the GA4 interface. Look for a dropdown menu next to a filter box and select "Matches regex" or "Does not match regex".

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

1. Standard Reports

This is the most common place you'll use regex. Let's say you're in the Reports > Engagement > Pages and screens report. You can filter the "Page path" dimension to group pages:

  1. Click the Add filter button at the top of the report.
  2. In the "Build filter" panel, select your dimension (e.g., "Page path and screen class").
  3. Choose the match type "Matches regex".
  4. Enter your regex pattern in the "Value" field and click Apply.

Boom! The report now only shows data matching your pattern.

2. Explorations

In the Explore section, you can use regex when filtering dimensions in any exploration type (Free form, Funnel, Path, etc.). Simply drag a dimension into the "Filters" section of the exploration, select "Matches regex," and enter your expression.

3. Segments and Audiences

Regex gets really powerful when creating highly specific user segments or audiences. For example, you could create an audience of users who have visited any of your /products/winter-collection/ pages by using a regex filter on the Page path dimension. This is far easier than adding dozens of individual pages.

4. Custom Channel Groups

When defining rules for Custom Channel Groups, regex can help you lump various campaign names or sources together. You could group all paid search campaigns from Google and Bing with a single regex rule on the "Session source / medium" dimension, like ^(google|bing) / cpc$.

Practical Regex Examples for GA4

Let's move from theory to practice. Here are everyday scenarios where regex can simplify your analysis.

Example 1: Grouping All Your Blog Posts

Your blog post URLs follow a consistent structure. You want to see traffic data for all blog articles combined.

  • URL Structure: /blog/how-to-do-seo, /blog/marketing-tips-2024, /blog/another-great-post
  • Goal: See traffic for all pages under the /blog/ directory.
  • Dimension to filter: Page path and screen class
  • Regex:
^/blog/.*  

Breakdown: The ^/blog/ part ensures the path starts with /blog/. The .* acts as a wildcard, matching any characters that come after it.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Example 2: Combining Multiple Traffic Sources

You want to analyze performance from all organic search engines together, not just Google.

  • Goal: See sessions from Google, Bing, and DuckDuckGo in one view.
  • Dimension to filter: Session source
  • Regex:
^(google|bing|duckduckgo)$

Breakdown: The ^ and $ combination ensures it's an exact match for one of the sources listed. The parentheses () group the | (OR) conditions.

Example 3: Filtering IP Addresses (for internal traffic)

You need to create a filter to exclude traffic from your office IPs to clean up your data.

  • Your IPs: 192.168.1.5 and 192.168.1.23
  • Goal: Define internal traffic based on IP patterns.
  • Location in GA4: Admin > Data Streams > Configure tag settings > Define internal traffic
  • Regex:
^192\.168\.1\.\d+$

Breakdown: This matches any IP starting with 192.168.1.. Notice the \. to escape the dots so they are treated as literal periods. The \d+ matches one or more digits at the end of the string.

Example 4: Isolating Subdomain Traffic

Your company uses different subdomains (e.g., shop.yoursite.com, blog.yoursite.com). You want to analyze data from just one of them.

  • Goal: Report on traffic only to the blog subdomain.
  • Dimension to filter: Hostname
  • Regex:
^blog\.yoursite\.com$

Breakdown: Using the ^ and $ ensures it only matches the exact hostname blog.yoursite.com. Escaping the dots with \ tells regex to look for actual periods, not wildcards.

Example 5: Cleaning Up URLs with Query Parameters

Your product pages get traffic with and without tracking parameters, splitting your data in the reports for the same logical page.

  • Example URLs: /product/blue-widget, /product/blue-widget?utm_source=facebook, /product/blue-widget?ref=123
  • Goal: Group data for /product/blue-widget, regardless of what comes after it.
  • Dimension to filter: Page path and screen class
  • Regex:
^/product/blue-widget

Breakdown: By only matching the start of the string with ^, we effectively ignore everything from the ? onward, combining all variations of the URL into a single row.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Pro Tip: Test Your Regex Before You Use It

Regex errors can be frustrating. A misplaced character can either match nothing or match everything. Before applying a pattern in Google Analytics, always test it first.

Websites like regex101.com and regexr.com are fantastic for this. You can paste a list of your actual page paths, campaign names, or hostnames into the "test string" box, then build your regex pattern and see what it matches in real-time. This saves a huge amount of guesswork and ensures your filters work as intended once you're back in GA4.

Final Thoughts

Mastering regular expressions moves you from a casual Google Analytics user to a power user. By learning just a few key patterns, you can stop clicking and start analyzing, building deeper, more flexible reports that would be impossible with standard filters. It’s a skill that pays off by saving you time and revealing insights you might have otherwise missed.

Of course, while learning regex is incredibly useful, not everyone has the time to fine-tune patterns when a complex question comes up. For times like that, we built Graphed to be your AI data analyst. You can simply ask a question like, "Show me traffic and conversions for all blog posts in a table" in plain English, and Graphed creates the report without you having to write any regex. It's the perfect way to get fast answers while you handle the bigger-picture strategy.

Related Articles