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.
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:
- Follow the instructions to create a new filter for your view.
- Set the Filter Type to
Custom.
- Click the Select filter type drop-down menu and select
Search and Replace.
- Use the Filter Field drop down menu to select the field (dimension) you want search.
- Enter a regular expression in the Search String field.
- Enter the Replace String. To delete the search string entirely, leave this blank.
- 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
Long URLs can make your reports hard to read. You can simplify them by removing directories. For example, here's how to turn example.com/directory/document1.html into 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:
/
Although it’s common for 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
Suppose your hit data contains numeric codes, such as content categories, product IDs, or similar. Examples of the Request URI might look like this:
/docs/document.cgi?id=100
/docs/document.cgi?id=200
...
Using multiple search-and-replace filters, you could change mysterious numbers to intuitive names:
/docs/document.cgi?id=books
/docs/document.cgi?id=magazines
...
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.
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.