[UA] Search-and-replace filters

Fine-tune the data in your Analytics views.

You can use search-and-replace filters to change the data in a reporting view as it's being processed. For example, you can consolidate your hostnames by removing the www. prefix. You can also make your data more human-readable by replacing codes or long pathnames with simpler, more intuitive versions.

Search-and-replace filters are not supported for either standard or custom product-level Ecommerce dimensions.
In this article:

How search-and-replace filters work

Search-and-replace filters use regular expressions to find a search string in a filter field and replace it with a replacement string.

Like all filters, search-and-replace filters only apply to hits collected after you've applied the filter to the view (filters cannot change historical data). And (like all filters), search-and-replace filters permanently change your data. Once a search string has been replaced, you can't undo that change. See the Verify your filter section below before applying any filters.

To create a search-and-replace filter:

  1. Follow the instructions to create a new filter for your view.
  2. Set the Filter Type to Custom.
  3. Click the Select filter type drop-down menu and select Search and Replace.
  4. Use the Filter Field drop down menu to select the field (dimension) you want search.
  5. Enter a regular expression in the Search String field.
  6. Enter the Replace String. To delete the search string entirely, leave this blank.
  7. Use the Case Sensitive checkbox if your regular expression is case sensitive. Otherwise, the search will be case insensitive.

Verify your filter

Search-and-replace filters are very powerful, so you want to be sure you've set them up correctly before changing your actual data. There are 2 ways to verify your filter:

1) If your view already has a large number of hits, you can use the Verify this filter link. This will show you what the filter would have done to a selected set of your historical data. You should ALWAYS do this, if possible. (Not all filter types can be verified in this way.)

2) You can also use Tag Assistant Recordings to record an actual flow through your site and see how the filter would affect those hits. You don't have to have matching hits already in the view, since Tag Assistant Recordings captures the hits as you move through the flow.

Search-and-replace examples

Simplify your page paths

Long URLs can make your reports hard to read. You can simplify them by removing directories. For example, here's how to turn www.example.com/directory/document1.html into www.example.com/document1.html:

  • Filter name: enter a name
  • Filter type: Custom
  • Select filter type: Search and Replace
  • Filter Field: Request URI
  • Search String: /directory/
  • Replace String: /

Filter example: remove "directory" from Request URI

Consolidate your domains

Although it’s common for www.example.com and example.com to serve the same content, Analytics treats them as separate URLs. If you're receiving hits from both domains, your traffic to this content will be split across two URLs, causing your traffic to appear lower than it actually is. You can prevent this by consolidating both domains into example.com:

  • Filter name: enter a name
  • Filter type: Custom
  • Select filter type: Search and Replace
  • Filter Field: Hostname
  • Search String: ^www\.
  • Replace String: leave blank

 Filter example: remove "www." from Hostname

Replace numbers with names

Suppose your hit data contains numeric codes, such as content categories, product IDs, or similar. Examples of the Request URI might look like this:


Using multiple search-and-replace filters, you could change mysterious numbers to intuitive names:


Here's how:

  • Filter name: enter a name
  • Filter type: Custom
  • Select filter type: Search and Replace
  • Filter Field: Request URI
  • Search String: id=100($|\&)
  • Replace String: id=books&

Verify this filter, then create a new filter following this pattern for each ID you want to replace.

Filter example, replace ID = 100 with ID = books

In the Search String field above, ($|\&) means "either the end of line OR the literal ampersand character." This means the pattern will match id=100 but not id=1001 and will match if the id comes in the middle of the Request URI or at the end.

As with all regular expression examples, you might need to adjust this to fit your actual data.

Data import offers additional ways of handling product and content data.


Was this helpful?

How can we improve it?
Choose your own learning path

Check out google.com/analytics/learn, a new resource to help you get the most out of Google Analytics 4. The new website includes videos, articles, and guided flows, and provides links to the Google Analytics Discord, Blog, YouTube channel, and GitHub repository.

Start learning today!

Clear search
Close search
Google apps
Main menu