A Bookmarklet To Remove UTM Tracking In URLs

December, 2022

I make a lot of reference links for my posts. I end up spending a bunch of time ripping the UTM tracking query parameters out of them. I knew I could set up something to scrub them but hadn't given it much though until this post from Chris Biscardi on Mastodon nerd sniped me.

The result is this code for a bookmarklet that removes the UTM params from the current URL and reloads the page without them. It also copies the scrubbed URL to the clipboard.

This is very much a "works on my machine" solution (which, in this case is a mac running chrome). It's not doing anything wild so I expect it can be made to work most places if it doesn't fire out of the box.

To use it, make a new bookmark and then edit it to put in this code. It has to all be on one line for it to work.

javascript: (() => { const url = new URL(document.location); const params = url.searchParams; const newParams = [];  for (const [key, value] of params) { if (key.indexOf('utm') === -1) { newParams.push(`${key}=${value}`); } };  let newParamsString = newParams.length ? `?${newParams.join("&")}` : ''; const newURL = new URL(`${url.origin}${url.pathname}${newParamsString}`); navigator.clipboard.writeText(newURL.href).then(() => {document.location = newURL.href;},() => {console.log("could not copy text")}); })();

Enjoy,

-a