Skip to content

Instantly share code, notes, and snippets.

@moorscode
Last active April 4, 2016 06:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moorscode/8e67808c3ecb283c9fabb492ab30b8a6 to your computer and use it in GitHub Desktop.
Save moorscode/8e67808c3ecb283c9fabb492ab30b8a6 to your computer and use it in GitHub Desktop.
Remove analytics campaign tags "utm_source" from the URL without redirecting to a new page, so visitors can bookmark or share the link without affecting the campaign count.
(function (l, h) {
if ( typeof h.replaceState === 'undefined' ) {
return;
}
if (!/[?&]utm_/ig.test(l.href)) {
return;
}
var elems = ['source', 'medium', 'term', 'content', 'campaign'];
var re = new RegExp('(^|&)utm_(' + elems.join('|') + ')=[^&]+', 'ig');
var parts = l.href.split('?');
parts[1] = parts[1].replace(re, '').replace(/^&/, '');
if (!parts[1].length) {
parts.pop();
}
h.replaceState(null, null, parts.join('?'));
})(location, history);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment