Last active
April 4, 2016 06:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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