Methods to Strip Queries from URLS in Google Analytics



Google Analytics has a user-friendly interface that makes editing your set-up relatively simple. However, when it comes to large accounts, performing view-level settings to be duplicated across tens or even hundreds of views within the same account isn’t as easy.

One example is when you want to exclude one or more URL parameters from your reports on every view. As mentioned in previous posts, this is important since Google Analytics will split pages into multiple rows that have different parameters. Most of the time you want to aggregate your data by page content, so this isn’t good when you go to analyze your content metrics.


In the above example, I want to see how ‘post-23’ is performing, but it is split into two rows. Now I can’t get an accurate picture of the bounce rate, hits, entrances or any other metrics since they aren’t combined. If you are not sure which, if any, URL queries you should keep, the general ‘rule’ is to keep only queries that involve different content. Also, it is best practice to have a view that strips out all queries along with a completely unaltered view.

The option exclude queries is under Admin > View Settings. The issue here is that you would have to copy and paste your parameters to every view that you want to exclude these on. When accounts have 100 or more views across multiple properties, this is a tedious task. Plus, maybe you missed a parameter and want to add to it… time to go back and copy-and-paste through all the views again.

Another limitation is that this field has a 256-character limit. If your site has a lot of URL queries, it would take a very long time to first identify what they all are and then list them one by one in this text box.


A more efficient method is to use a filter instead. Since most filters are account-level, they can be added to every view in your account at once, even across all of your different properties. You can try it now by going to Admin > Account > All Filters. After selecting or creating a filter, there is an option toward the bottom to shift-click-select and add it to as many views as you like. This works for most filters except those that are custom dimension-based – they cannot be batch applied with this method (…yet. I hope this will change!) since dimensions are property-level.


Remove One URL Query with Filters

Note: we will be using regular expressions for all of our filters, so if you need a little refresher there are posts here and here.

First, select the option to create a new filter. In the options, choose ‘Custom,’ ‘Search and Replace’ and choose the Request URI as the filter field. We are searching for the query in the URL, so this is where we place our regular expression that will match the query that we want to remove. In my example, I want to remove a ‘name’ query that is passed since it is PII, or Personally Identifiable Information. An example of the regex is below.


We’ll leave the replace field blank, as we want to take out everything in parenthesis and replace it with nothing.

It might look a little confusing, so let’s break it down:

The first part is the query’s name and the ‘=’. Keep in mind that the query may be one of many and between or before other queries that we want to remain. The next part of the expression, ‘[^&]’, is targeting any character that is not an ampersand so that it stops before the next query. The asterisk is there representing 0 or more of the preceding character, so it will cover us if it is the last query. If there’s an ampersand after the query parameter, we also want to remove it to prevent something like this from showing up “/blog/post-23?lang=en&&x1=key4929”.

Once this filter is applied, a second filter should also be created to strip out trailing ‘?’s or ‘&’s on page paths. Our first filter grabs the query word and value but not the character before it (‘?’ if it is the first query in the URL, ‘&’ if it is not.) We do this because we’re not sure if there will be one or more query parameters.

The second filter will also be a Search and Replace custom filter. In the first field, the RegEx to grab the “?” or “&” only if it is the last character is:


Again, we’ll leave the Replace field blank.

Remove Multiple URL Queries with Filters

If you want to exclude more than one query, you will have to create another filter for each query that you would like to be excluded. Remember that the order of filters is important, so make sure the remove query filters all go before the secondary filter for trailing “?”s and “&”s.

Remove All URL Queries

If you are sure none of the queries are relevant to your content, you can also remove all of them at once to have clean page paths. In some cases, it may make sense to have a separate view that strips off all query parameters, no matter what. This makes content reporting on say blog posts or news stories much easier!

Only one search and replace filter is needed in this case. The regular expression for the search field is the following:


This will capture anything following and including the question mark, and then we’ll replace it with nothing.

Remove Queries with Google Tag Manager

This method is more than just excluding the URL parameters, rather it prevents them from ever hitting the interface. This would be ideal in a situation where Personally Identifiable Information (PII) is appearing in the URL. Google Analytics’ Terms of Service states that this information can’t be stored on their servers, so it’s best to prevent it from ever reaching Google Analytics instead of filtering it out afterward.

An example is if someone’s name, phone number or email address becomes a URL query parameter briefly after filling out a form.

You’ll need to make sure you have your Page URL variable enabled in the list of Built-In Variables in Google Tag Manager V2. (If you’re still in V1, make sure you have a url path macro defined!)

Next, create a Custom JavaScript variable and name it Updated Document Location. Paste in the following JavaScript:

This script has an array of query parameters that will be removed from your page path. You can add or remove query parameters by editing the array on the second line.

Next, in your pageview tag, go to More Settings > Fields to Set. Add a new field called “location” and set that value to your new Variable, {{Updated Document Location}}, as shown below.

All that’s left is to Preview and Debug and then Publish!

Wrapping Up

There you have it – multiple ways to remove query parameters from your URLs! Remember, you can always use the built-in Remove Query Parameters option at the View level for the most basic query parameter issues. Using a Filter or script in Google Tag Manager will help with larger websites that have more complicated setups!

Samantha is a Manager for the Analytics & Insight department at LunaMetrics. She has a passion for exploring data and loves the excitement of finding solutions and explanations behind metrics. Her background includes advertising, SEO, and analytics as well as involvement in the arts. When she isn't logged into Google Analytics, she can be found traveling, talking about food or playing games.

  • Cem Simsek

  • Johannes Scharlach

    You can also just use one regex: ((&|?)MID=[^&]*)

    • J.P. VanderLinden

      Thanks Johannes! I was able to modify this regex to apply to all URL query parameters by replacing the “name” attribute with “(.*)” for any.


  • Dmitry Klymenko

    Nice article, Samantha thanks !

    Just a couple of notes – you could still, it would make sens to make regex case insensitive and the option exclude queries in View settings is case sensitive and works before filters

  • Parham

    How would you replace multiple/all occurrences of a query? For example I’d like to remove all query parameters starting with _ (underscore). Is this possible with only one search and replace filter?

  • This was very helpful, thanks!

  • Joao Carpio

    Is it possible using only GMT to split this page path: /job-listings/seo-assistant–microsoft–los-angeles

    and put the following info on event action and label:

    event action: seo-assistant
    event label: microsoft–los-angeles

  • I am trying to remove all query parameters. When I use this filter ^[^?]+(?.*), it removes the whole URI and leaves the active page just empty.

    • J.P. VanderLinden

      Steven –

      I had the same issue, but by modifying the comment by Johannes below, I found success. Try using ((&|?)(.*)=[^&]*) instead, and replacing with nothing. It worked for me.

  • JohnSmithTheAnon

    Is it possible to do this as a segment rather than a filter?

    • Jimmy

      No it is not unfortunately. As the article mentions, I’d highly suggest creating a view with the filter applied and one without any filters!

  • Jochen Setzer

    Hey there, great article. Thanks Samantha!

    I have one question regarding the “removal of all url queries”. Will I then loose all the AdWords information in my ga account since the “gclid”-parameter is removed as well?


    • J.P. VanderLinden

      Jochen –
      If you use this as a filter, it’s only blocking what you see, not what Google Analytics processes. If you’re using auto-tagging in AdWords, you probably aren’t even seeing the gclid in your content reports anyway, right? That’s because Google already knows to remove it.

      • Jochen Setzer

        J. P. – Thanks for your helpful answer. Does this also apply to the utm parameter that I ad to track email campaigns? Will they also be processes by ga if I filter out all URL queries? Cheers, Jochen

        • J.P. VanderLinden

          Yes. Filters applied in Analytics occur after the data is process by Google. This differs from applying rules in the htaccess file or on the web server, which happens before the Analytics tracking code fires.

          A good way to see this is always keep a raw, untouched GA view with no filters. This raw view will have every URL parameter inside, so you can double-check to confirm the data you are seeing in your filtered view isn’t tainted or dropping visits.

          • Jochen Setzer

            Thanks a lot for the explanation J.P.! I found it just impossible identify every possible query parameter in order to clear up reports like least viewed pages etc.. I’ll now do as you suggest and keep and unfiltered view but get rid of all query parameters in (most) other views! Thanks again!

  • manish

    Hello Guys

    Anybody can help me to how can i set and basic use of
    “Site search Tracking” field of google analytical and how it connected
    with my website of

  • DuncanS

    Great post!

    Just a friendly warning: the provided GTM JavaScript in this article does not perform exact matches on the query parameter names i.e. “m=…” will end up inadvertently changing “cam=…” into “ca”.

    For a few JavaScript alternatives see this Stack Overflow post:

    I ended up combining the LunaMetrics JavaScript code with some code from that post.

    • Samantha Barnes

      Thanks, Duncan! That’s a really great point and especially useful if there are a lot of parameters.

  • Yoyo

    I have two pieces of information that might help folks:

    1. If you want to match multiple parameters with one filter in Google Analytics, use this regex:


    Then, replace the “paramName” with the parameters you want matched. The “i” at the end makes it case insensitive so you don’t have to worry about multiple versions of the same parameter. I tested this out in Google and in a Regex building tool and it worked for all the cases I tested for. The word boundaries “b” makes the code match a whole word. This came in handy when I tested against short or one letter parameters like a=, b=, c=, and so on.

    2. If you have tons of web pages with tons of developers adding URL parameters left and right that you have no idea what they mean, you can reference the JavaScript here…

    …to remove ALL parameters EXCEPT the ones you want. This code will be very beneficial in preventing unwanted data from being passed into Google Analytics. Since Google Analytics doesn’t allow the lookback feature in regex to be used you cannot implement this as a filter on the Google Analytics platform. To make it work, you have to implement it through your tag management system or place it directly into the GA code on the page. Basically, you will have to alter the document path (dp) and maybe the document location (dl) properties before they are sent off to Google in the collect tag. So, when the cleaned up URL is processed by Google it already has parameters removed.

    I hope someone else finds this beneficial.

  • a

    the javascript for GTM code prompts a parsing error when you try to save it. Why post broken code??

  • hi, i use autotrack, from custom html to send pageview, how can i update the path page in custom html?

    i think some like “ga(‘set’,’page’,'{{Update Page Path}}’);” buth not change page path.


  • Stephen M. Harris

    Thanks for the handy post!

    One fix: here is an improved regex for “Remove One URL Query with Filters”:

    – Added [&?] in front, so it’d only match param by full name (current regex would strip partial param name matches)
    – Added # to characters not to match for param value (in case tracking is customized to include url hash/fragment)
    – Made entire =ParamValue section optional, so regex works if param is present with no value
    – Replaced [&?] in front with (^[^#?]*?([^#]*&)?) to ensure only actual query params are stripped, not parts of the url path or hash/fragment

    (Note with this regex, you’d need to use “1” In the Replace field.)

    • Jay


      I tried your method, but I couldn’t get it to work. Can you verify my setup? Also some of my end with footnotes, periods, brackets, pounds signs, and other miscellaneous characters.


      • Stephen M. Harris

        Hi Jay, If you have a pound sign in a query parameter, GA will treat everything after the pound sign as a fragment (i.e. not the querystring).

        I did a quick test and discovered that this is the case even if the pound sign is url encoded! This appears to be a design flaw, as it means there is no way to distinguish between a valid url-encoded pound sign and the pond sign that marks the start of the URL fragment.

        If you haven’t modified tracking to track fragments to GA (or know that your fragments won’t have a querysting-like format) then you can remove the second and third pound signs from the regex to make this filter work for you:

  • Tam

    I can’t seem to get any of the “remove all” solutions to work (the one in the original article and the two in the comments). I even tried the original solution of excluding the parameters in the View settings and that doesn’t work either. Anyone got any suggestions please?

  • Ed Carbery

    Thanks for this info…saved us a lot of coding/testing!

  • ezstreet

    I’m not getting this to work. Can someone PLEASE paste the entire code that will STRIP ALL after the ? I just want clean URLS and this code is broken, gets parse errors in GTM V2. Also I tried to modify it so it will remove all and that isn’t working either.


  • Kenny Yang

    Shouldn’t we be returning instead of a.href since the analytics page view field ‘page’ is a path?

  • Helga Sondermann

    Some useful filters including param-stripping can be found here:

  • velocify

    Is this retroactive? I tested the filter and it works but when I view the actual report, I still see the querystrings.

    • Kyle W.

      No, filters are applied to the raw data before it is processed, therefore it only works going forward (since the historical data has already been processed), and cannot be undone. That’s why it’s good to a) test your filters, and b) keep an unfiltered view.

      • Eric

        So how do we clean data already processed? I have URL with PII in my report and would like to remove this parameter.

        • Kyle W.

          I think PII is trickier, as Google technically doesn’t allow PII to hit Google’s servers at all, which means filters don’t help. Filters would only prevent the PII data from showing up in your reports.

          You can use Tag Manager as a backup to avoid sending any hits with PII to GA…but that doesn’t help much with historical PII data.

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187