Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Last active June 7, 2018 15:42
Show Gist options
  • Save joshuabaker/98585541044a0fe4e8d657c0c257bc84 to your computer and use it in GitHub Desktop.
Save joshuabaker/98585541044a0fe4e8d657c0c257bc84 to your computer and use it in GitHub Desktop.
Snippet to remove UTM parameters from using the History API.
!function(){var e=history,t=location;if(e&&e.replaceState&&t.search){var n=t.search.slice(1).split("&"),l=n.filter(function(e){return"utm_"!==e.slice(0,4)});if(l.length<n.length){var a=l.length?"?"+l.join("&"):"",i=t.pathname+a+t.hash;setTimeout(function(){e.replaceState(null,null,i)},3e3)}}}();
(function() {
var hsty = history;
var loc = location;
function paramIsNotUtm(param) {
return param.slice(0, 4) !== 'utm_';
}
if (hsty && hsty.replaceState && loc.search) {
var params = loc.search.slice(1).split('&');
var newParams = params.filter(paramIsNotUtm);
if (newParams.length < params.length) {
var search = newParams.length ? '?' + newParams.join('&') : '';
var url = loc.pathname + search + loc.hash;
setTimeout(function() {
hsty.replaceState(null, null, url);
}, 3000);
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment