Feature request - Remove parameters from URL

To avoid including the parameters one by one in the ‘Remove parameters from the URL’ list it would be useful to have a checkbox to remove all the parameters at once.

2 Likes

Hi @Javier_Guirao_Carras, welcome to our Community!

Thank you for your suggestion, we’ll review the data and consider this improvement.
Tymek

Even better: checkbox to remove all except whitelisted parameters.

I have built a whitelist mechanism that removes all parameters except the ones that are whitelisted. If you are interested, I’m happy to share it.

2 Likes

:+1: in support for the request of this feature

Hi @rmeekers, i would greatly appreciate if you would share your whitelist mechanism :slight_smile:

Here ya go

Variables

CustomJS: PageURL clean

function () {
  
  function parseUrl() {
    var url =  window.location;
    return {
        protocol: url.protocol,
        host: url.hostname,
        port: url.port,
        query: url.search,
        hash: url.hash,
        path: url.pathname.toLowerCase().replace(/^([^/])/,'/$1').replace(/\/$/, '')
    };
  }

  var parsedUrl = parseUrl();

  function cleanParams() {
    var result = '';
    if(!parsedUrl.query) {
      //result = '';
      return result;
    }


    var urlSearchParams = new URLSearchParams(parsedUrl.query);
    var q = Object.fromEntries(urlSearchParams.entries());

    
    for (var key in q) {
      //Check for and retain whitelisted parameters
      if ({{ query parameters to be preserved }}.indexOf(key) == -1) {
        delete q[key]
      }
    }
    
    var keys = Object.keys(q).sort();
    
    // If there are no query params, just return q
    if (keys.length == 0) {
      //result = '';
      return result;
    } else {
    
      result = '?';

      for (var i = 0; i < keys.length; i++) {
        if (i > 0) {
          result = result.concat("&")
        }
        result = result.concat(keys[i] + "=" + q[keys[i]])
      }
      return encodeURI(result);
    }
  }


  var cleanedParams = cleanParams();
  var hostname = {{ Page Hostname }};

  return parsedUrl.protocol + '//' + hostname + parsedUrl.path + cleanedParams + parsedUrl.hash;
}

Constant: query parameters to be preserved
This is a comma separated list of all parameters that you want to keep
Example:

utm_medium,utm_term,utm_source,utm_campaign,utm_content,gclid,q,bot_detected

Tags

Custom HTML: Set additional tracker settings
(This tag is prioritised in the settings.)

<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['setCustomUrl', {{ PageURL clean }}]);
</script>

Hope this helps :slight_smile:

1 Like

Is using an extraction for Page URL sufficient?

Each of those options removes all the parameters.