Last active
June 7, 2018 15:42
-
-
Save joshuabaker/98585541044a0fe4e8d657c0c257bc84 to your computer and use it in GitHub Desktop.
Snippet to remove UTM parameters from using the History API.
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(){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)}}}(); |
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() { | |
| 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