如何用带例外的Regex配置GA URI搜索替换过滤器合并注册路径
Hey there, let's work through this Google Analytics filter setup to clean up your enrollment path tracking. I've got a tailored solution that'll merge all those messy variants into a single /enrollment path while excluding your thank-you pages—here's how to do it:
Your goal is to collapse 4000+ enrollment-related path variants (like /enrollment, /enrollment/, /enrollment/sem01, etc.) into a unified /enrollment value, while excluding any thank-you pages that fall under the /enrollment namespace. We'll use a custom search-and-replace filter with a regex that includes an exclusion rule to make this happen.
Here's the exact regex and replacement value you'll need:
Search String (Regex)
^\/enrollment(?!\/thank-you)(\/.*)?$
Replace String
/enrollment
Adjusting the Exclusion Rule
If your thank-you page uses a different path (e.g., /enrollment/thanks, /enrollment/confirmation), modify the negative lookahead part to match. For multiple excluded paths, use a pipe | to separate them:
^\/enrollment(?!\/thank-you|\/thanks|\/confirmation)(\/.*)?$
- Log into your Google Analytics account and navigate to the custom view you created for this tracking.
- Click the Admin gear icon in the bottom-left corner.
- In the View column, select Filters.
- Click Add Filter to create a new filter.
- Give your filter a descriptive name (e.g., "Merge Enrollment Paths & Exclude Thank You").
- Under Filter Type, select Custom, then choose Search and Replace.
- From the Field A -> Extract A dropdown, select Request URI (for GA4, select Page path + query string instead).
- Paste the regex from above into the Search String field.
- Paste
/enrollmentinto the Replace String field. - Use the Filter Test section at the bottom to verify:
- Input paths like
/enrollmentor/enrollment/sem01should show a replacement value of/enrollment. - Input your thank-you page path (e.g.,
/enrollment/thank-you) should not be matched or replaced.
- Input paths like
- Click Save to activate the filter.
Let's break down the regex so you understand how it works:
^\/enrollment: Matches any URI that starts with/enrollment(the^ensures we only match the start of the string, and\/escapes the forward slash for regex syntax).(?!\/thank-you): A negative lookahead that tells the regex to skip any path where/enrollmentis immediately followed by/thank-you—this excludes your thank-you pages.(\/.*)?: Matches any optional trailing content after/enrollment(including a trailing slash or SEM/UTM parameters). The?makes this section optional, so it covers both/enrollmentand/enrollment/cases.
内容的提问来源于stack exchange,提问作者Eric Anderson

