Skip to content

Instantly share code, notes, and snippets.

@Michaelcgn
Created February 20, 2018 03:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Michaelcgn/196a1f8b5ada3d37eadb0bdee14fb4ad to your computer and use it in GitHub Desktop.
Save Michaelcgn/196a1f8b5ada3d37eadb0bdee14fb4ad to your computer and use it in GitHub Desktop.
UTM Parameter aus URL entfernen
function() {
// Callback für Google Analytics zum Entfernen der utm/ Kampagnenparameter
return function() {
if (!window.history.replaceState) { return; };
var cleanSearch = window.location.search
.replace(/utm_[^&]+&?/g, '') // utm Parameter werden aus der URL entfernt
.replace(/&$/, '') // entfernt ein überflüssiges & am Ende
.replace(/^\?$/, '') // entfernt ein überflüssiges Fragezeichen am Ende
;
// für den Fall, dass Fragments als utm Parameter eingesetzt werden
var cleanHash = window.location.hash
.replace(/utm_[^&]+&?/g, '') // entfernt die utm-Fragmente
.replace(/&$/, '') // entfernt ein überflüssiges & am Ende
.replace(/^\#$/, '') // entfernt ein überflüssiges # am Ende
;
window.history.replaceState({}, '', window.location.pathname + cleanSearch + cleanHash);
}
}
@NormanHoehne
Copy link

Hey Michael,

thank you for sharing this code adaption from wistia's fresh url script with the world. I used it in the past two years but the last days made a deep analysis of single customer journeys necessary. And with what I found I really must admit that this callback function leads to non-persistent sessions in Google Analytics and Facebook Pixel.

⚠️ It should not be used on a production site ⚠️

I cannot determine where the problem exactly lies but here is what I found out:

A great amount of client journeys start as intended with all the information from the UTM parameters in their first Google Analytics hit. Then after a few! seconds later they click to the next page sending a new hit to Google Analytics. But this second hit is sent within a new session without any session information from the first one. This leaded to a lot of Sessions and Conversions with '(not set)' in the campaign, source, medium, term & content dimensions.

Google Ads is not affected by this because they store cookies in order to make sessions persistent. There are even tutorials out there to leverage this behaviour with all campaigns coming in with decorated URLs: https://www.simoahava.com/analytics/persist-campaign-data-landing-page/

But i really can't recommend this function anymore.

Best,
Norman

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