Skip to content

Instantly share code, notes, and snippets.

@paulirish
Last active September 17, 2024 11:03
Show Gist options
  • Save paulirish/626834 to your computer and use it in GitHub Desktop.
Save paulirish/626834 to your computer and use it in GitHub Desktop.
userscript: Drop the UTM params from a URL when the page loads
// ==UserScript==
// @name UTM param stripper
// @author Paul Irish
// @namespace http://github.com/paulirish
// @version 1.2
// @description Drop the UTM params from a URL when the page loads.
// @extra Cuz you know they're all ugly n shit.
// @include http*://*
// ==/UserScript==
// Update:
// In chrome, it's better to just install the UTM stripper chrome extension:
// https://chrome.google.com/webstore/detail/google-analytics-paramete/jbgedkkfkohoehhkknnmlodlobbhafge
// It is great and open source: github.com/mihaip/utm-stripper
// You can also install this greasemonkey script if you really want.
// download this script. go to about:extensions. Turn on developer mode and drag and drop
// this file onto the window. it'll install it. hopefully.
// lastly, if your site / marketing funnel uses these tracking tokens. you can clean up your users URLs
// look at the comments below on correct installation to integrate with __gaq.push
if (/utm_/.test(location.search) && window.history.replaceState){
// thx @cowboy for the revised hash param magic.
var oldUrl = location.href;
var newUrl = oldUrl.replace(/\?([^#]*)/, function(_, search) {
search = search.split('&').map(function(v) {
return !/^utm_/.test(v) && v;
}).filter(Boolean).join('&'); // omg filter(Boolean) so dope.
return search ? '?' + search : '';
});
if ( newUrl != oldUrl ) {
window.history.replaceState({},'', newUrl);
}
}
@oyvindkinsey
Copy link

Sure that works?
I'm pretty sure a map(-> return bool).filter(..)) will return an array of .. well, booleans..

Stick with the basics I say,

var newSearch = location.search.replace(/utm_.*?(&|$)/g,""), newSearch = newSearch.length > 1 ? newSearch : "";
newSearch != location.search && window.history.replaceState({},'', location.href.replace(location.search, newSearch));

;)

@paulirish
Copy link
Author

yup that works.

you're right that it does return an array of bools...

and the Boolean constructor is treated as a function with, an undefined argument, returns the false boolean. so it's a quick way to shoot a return false callback into [].filter()

its all quite clever.

@oyvindkinsey
Copy link

Doh! It was the !/^utm_/.test(v) && v line that fooled me, this of course returns v when the first expression is true, not the composite boolean value of the entire expression...
But

search = search.split('&').filter(function(v) {
    return !/^utm_/.test(v);
}).join('&');

works just as well... no need for map ;)

@Charudatta
Copy link

Clever piece of code, but we cannot drop this into a webpage (or might also be a bad idea for the extension),

  1. This is not bound to an event, so are you sure GA will actually register the query string parameter to attribute this session to th campaign?
  2. Also, ideally it should not completely remove the params, but replace it with new values after the original params are registered by GA. This will be useful, so that if someone now shares this link or bookmarks it, we can correctly attribute the other visits.

Ideal way of doing this is to use, _gaq.push(function(){removeQueryString();}) //comes after the track page view method is called.

@kurtextrem
Copy link

Thank you so much, paul! I hate those utm things

@cowboy
Copy link

cowboy commented May 21, 2011

@oyvindkinsey lol I got so carried away with .map that I didn't even think, thanks!

@lachezar
Copy link

lachezar commented Feb 1, 2013

+1 on the statement that window.history.replaceState({},'', newUrl) must appear inside of _gaq.push otherwise the __utm.gif does not carry the correct parameters.

@raulaudelo
Copy link

script works great in Chrome + FireFox. Anyone have luck in IE 8 and above?

@elidickinson
Copy link

@raulaudelo - IE8 (and 9) do not support the session history API: http://caniuse.com/#search=history

@SamHasler
Copy link

// @include        /^http.*\?.*[?&]utm_.*$/

@timelf123
Copy link

ga('send', 'pageview', { 'hitCallback': removeUtms });

@vilav
Copy link

vilav commented Dec 12, 2017

@paulirish is this codeblock still valid with the new ga.js

@timelf123: is that line of code a replacement for the script or a add-on to the GA code to better work with this script?

Thanks!

@grand-lotus-iroh
Copy link

If you load example website https://mashable.com/ you will still see /article/bla-bla-bla/?utm_campaign=hp-r-2&utm_source=internal&utm_medium=onsite being loaded after each link when you hover article.

Perhaps we can strip / rewrite source code on page load? Any idears?

@pyxelr
Copy link

pyxelr commented May 10, 2020

If we have a userscript manager installed (such as Tampermonkey), it's quicker to add your script by clicking the Raw button.

@vivek1986
Copy link

vivek1986 commented Mar 2, 2022

As we all know how Google abuses privacy by Search url tracking, let's put an end to that once n for all:

// ==UserScript==
// @name         Cleanup Google Search URLs
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description Put an end to Google Search Tracking
// @author       VS
// @match        http*://*.google.com/search*
// @include      /^https?\:\/\/.*.google\..*\/.*$/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        none
// ==/UserScript==
(function(){
    'use strict';
    let url = window.location.origin;
    let currUrl = window.location.href;
    let cleanUrl = currUrl.split('&')[0].split('#')[0];
    // let cleanUrl = currUrl.replace(/\(pp_w\d+_h\d+\)/gi, '');
    window.history.replaceState({},'', cleanUrl);
    // window.history.pushState({page:cleanUrl}, cleanUrl, cleanUrl);
})();

@Korb
Copy link

Korb commented Feb 24, 2023

The script does not work with links pasted by the Augmented Steam browser extension. Links like https://steamdb.info/app/.../?utm_source=Steam&utm_medium=Steam&utm_campaign=SteamDB%20Extension and https://pcgamingwiki.com/api/appid.php?appid=...&utm_source=Steam&utm_medium=Steam&utm_campaign=SteamDB%20Extension remain unchanged. Mozilla Firefox 111.0b5 (64-bit), Tampermonkey 4.18.1 (January 17, 2023).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment