Last Updated: February 25, 2016
·
857
· 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.search.match(/utm_/) || location.hash.match(/utm_/))) {
  search = location.search.replace(/(\?|\&)?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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

The website is part of the attack surface, not only the API behind it. Every script, iframe, and tag on a public page is code that did not go through the same review as the backend.

Security insights from Cside