Last Updated: February 25, 2016
· gpakosz

Remove Google Analytics cruft from your URLs

This is a jQuery code snippet I use on my site to remove the cruft that Google Analytics when someone links to a page from a campaign:

if (window.history && history.replaceState && ( || location.hash.match(/utm_/))) {
  search =\?|\&)?utm_[a-z]+=[^\&]+/g, '');
  hash = location.hash.replace(/(#|\&)?utm_[a-z]+=[^\&]+/g, '');

  history.replaceState({}, '', location.pathname + search + hash);

This removes GA query strings and hashes after the request has been logged by your web server and before your readers bookmark the page.

Hope that helps.