How to Strip URL Parameters Using Google Tag Manager

This is the blog post to accompany my video tutorial on how to Strip URL Parameters from Google Analytics Page Views using Google Tag Manager:

The following code overrides the page location stripping myparam from the URL:

function() {
  var path = {{Page Path}},
    query = {{URL Query Parameters}},
    queryOverride = query.replace(/&myparam(\=[^&]*)?(?=&|$)|^myparam(\=[^&]*)?(&|$)/, ""),
    cleanedPath = path,
    url = {{Page URL}},
    protocol = url.split("//")[0];

  if(queryOverride.length > 0){
    cleanedPath = path + "?" + queryOverride;
  }
  return protocol + "//" + {{Page Hostname}} + cleanedPath;
}

This is what your Location Override variable should look like in Tag Manager:

Location Override Variable

 

This is what your page view tag should look like:

Page View Configuration

And this shows the configuration of the URL Query Parameters variable:

URL Query Parameters