Question: How do I remove URL params from getting pushed to GA4 through GTM? I have tried many solutions but none seem to be working.
Which key in "Fields to Set" do I need to use so GTM replaces the url query param from all dimensions like page\_path, page\_location, path\_referrer?

Answer: 
[This article](https://www.bounteous.com/insights/2020/05/15/excluding-url-query-parameters-google-analytics/) has been my life saver when dealing with URL params in GA4, but please use my experience and avoid the mistake of applying the script directly to `page_location`.

`page_location` is what I call a *technical dimension* that GA4 uses to sort referring websites according to its internal rules and do any other GA4 things. Remove URL params from `page_location` using GTM, and you'll stop seeing all channels, reliant on UTMs—so paid search, display, paid social, email etc (provided you use UTMs, of course). Don't forget: in this case, you remove the URL params in GTM *before* they get in GA, so if GTM strips params out, GA doesn't see them.

To illustrate my mistake, this is how my GA4 configuration tag in GTM looked like initially:

![page_location is set to receive a clean version of a URL with params stripped out, thanks to the script](https://i.stack.imgur.com/YFQKC.png)

Bad idea. Don't touch `page_location`.

The best approach is to just create your own dimension which you would use to store 'clean' URLs, say, `page_URI`. The reason: you stop relying on GA built-in dimensions that (potentially) are prone to change and you create something of your own that you will have control over and can add to any event as a dimension.

Below is my version of the script in GTM, deployed as a Custom Javascript Variable:

```
function() {
  var params = ['hash', 'email', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'fbclid', 'mc_cid', 'mc_eid', 'msclkid']; //Add URL params to be excluded from page URI
  var a = document.createElement('a');
  var param,
      qps,
      iop,
      ioe,
      i;
  a.href = {{Page URL}};
  if (a.search) { 
    qps = '&' + a.search.replace('?', '') + '&';
    for (i = 0; i < params.length; i++) { 
      param = params[i];
      iop = qps.indexOf('&' + param + '=');
      if(iop > -1) {
        ioe = qps.indexOf('&', iop + 1);
        qps = qps.slice(0, iop) + qps.slice(ioe, qps.length);
      } 
    }
    a.search = qps.slice(1, qps.length - 1);   
  } 
  return a.href; 
}

```

Two things to mention about the code:

1. List all params you want to strip out in the array ***params***;
2. `a.href = {{Page URL}}` - the code makes use of GTM's built-in variable `Page URL` (hence double curly brackets) that captures the full URL (without hostname, though). If you feel fancy, you can replace it with plain JS.

So the code above now populates the GTM field/GA4 dimension `page_URI` in the main configuration tag and any other tags, where I think having a clean URI is useful:

![you have your own personal URI dimension and don't touch anything built-in](https://i.stack.imgur.com/eIZBg.png)

I do realize that this approach uses up one GA4 dimension, but it's a price I'm willing to pay to have a clean URL in the absence of a better solution.
You can also use the following JavaScript in the custom JavaScript variable instead of the custom JavaScript mentioned above.

In this custom JavaScript instead of creating a new anchor element, we simply are taking the full page URL and then using the JavaScript's in-built `URL()` method to convert it to a proper URL that can be programmatically managed and then manage it according to the need.

I'm sharing my script below:

### Step 1

Create a custom JavaScript variable inside your GTM and add the following JavaScript code into it.

```js
function() {
    // Set the array with the list of query string you would like to remove being shown up in Google Analytics 4
    var excuded_query_params = [ 
        'add', 
        'the', 
        'query', 
        'strings', 
        'you', 
        'would', 
        'like', 
        'to', 
        'be', 
        'removed', 
        'from', 
        'GA4', 
        'analytics', 
        'report'
    ]

    // Get the full Page URL from GTM in-build variables
    var page_url_string = {{Page URL}}

    // Convert the received URL from string format to URL format
    var page_url = new URL( page_url_string )
    var page_url_copy = new URL( page_url_string )

    // Loop through the query parameters in the URL and if there is any query param which is in the excluded list,
    // remove that from the full URL
    page_url_copy.searchParams.forEach( function(param_value, param_name) {
        if( excuded_query_params.includes( param_name ) ) {
            page_url.searchParams.delete( param_name )
        }
    } )

    // Return the final URL
    return page_url.toString()
}

```

**Please Note:** as we are going to replace the value of `page_location` a default GA4 variable's data - it is highly recommended that you do not remove the `utm_` query parameters from the URL as GA4 reports use that data internally and that may lead to report breaking. So, it's best that you do not remove query parameters like `utm_souyrce`, `utm_campaign` etc.

### Step 2

Inside your GA4 Configuration Tag, click on **Fields to Set** and add a new field with the **Field Name** set as `page_location` and value set as this custom JavaScript variable.

[![GA4 Tag Setup with GTM - With Custom Query Params Removed](https://i.stack.imgur.com/6j4Qm.jpg)](https://i.stack.imgur.com/6j4Qm.jpg)

### Step 3

Now it's time to preview inside GTM and deeply.