This article has been imported from and is not necessarily up to date!

Remove UTM params using hitCallback in Google Tag Manager

Last updated: 

I love clean URIs and I enjoy analytics, even though I just started using them a little more conscious.
Sometimes it's usefull to include those handy UTM_ parameters in your links.
They can add important information, make the address bar ugly and even be kind of counter usefull once people start bookmarking those links.

So why not use a handy GTM Variable?!
I called mine cJS - replace history to remove query params.
Stuff the following snippet into the Custom JavaScript field:

function() {
  return function() {
    if (!!(window.history && window.history.pushState)) {
      if (/(\?|&)(utm_.*=|ref=)/.test( {
        window.history.replaceState({}, document.title, document.location.pathname +'&').filter(function(x) {return !/^(utm_.*=|ref=)/.test(x)}).join('&').replace(/^(.*)/, '?$1') + document.location.hash)
      if (/\?$/.test(document.location.href)) {
        window.history.replaceState({}, document.title, document.location.pathname)

And add it to a Universal Analytics Tag as follows:

  1. Enable overriding settings in this tag
  2. Open More Settings and Fields to Set
  3. Hit + Add Field
  4. Enter hitCallback as name
  5. Select your created cJS - replace history to remove query params variable
  6. Remember to test and submit your changes

As GitHub Gist

More on hitCallback

Please let me know if anything is unclear or doesn't work as expected. The code should just work and as it checks for available browser APIs shouldn't break anything, yet you'll never know 😉
Just click the Twitter button to message me.

Can Rau
Can Rau

Doing web-development since around 2000, building my digital garden with a mix of back-to-the-roots-use-the-platform and modern edge-rendered-client-side-magic tech 📻🚀

Living and working in Cusco, Perú 🦙