utm parse and add to links
This file contains 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 getRefQueryParam(name) { | |
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); | |
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); | |
var results = regex.exec(location.search); | |
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); | |
}; | |
let utmParamQueryString = ''; | |
let utmParamQueryStringTrimmed = ''; | |
(function () { | |
let utm_source = getRefQueryParam('utm_source'); | |
let utm_medium = getRefQueryParam('utm_medium'); | |
let utm_content = getRefQueryParam('utm_content'); | |
let utm_campaign = getRefQueryParam('utm_campaign'); | |
let utm_term = getRefQueryParam('utm_term'); | |
let roistat = getRefQueryParam('roistat'); | |
let roistat_referrer = getRefQueryParam('roistat_referrer'); | |
let roistat_pos = getRefQueryParam('roistat_pos'); | |
if (utm_source) { | |
utmParamQueryString += '&utm_source=' + utm_source; | |
} | |
if (utm_medium) { | |
utmParamQueryString += '&utm_medium=' + utm_medium; | |
} | |
if (utm_content) { | |
utmParamQueryString += '&utm_content=' + utm_content; | |
} | |
if (utm_campaign) { | |
utmParamQueryString += '&utm_campaign=' + utm_campaign; | |
} | |
if (utm_term) { | |
utmParamQueryString += '&utm_term=' + utm_term; | |
} | |
if (roistat) { | |
utmParamQueryString += '&roistat=' + roistat; | |
} | |
if (roistat_referrer) { | |
utmParamQueryString += '&roistat_referrer=' + roistat_referrer; | |
} | |
if (roistat_pos) { | |
utmParamQueryString += '&roistat_pos=' + roistat_pos; | |
} | |
if (utmParamQueryString.length > 0) { | |
utmParamQueryString = utmParamQueryString.substring(1); | |
utmParamQueryStringTrimmed = utmParamQueryString; | |
utmParamQueryString = '?' + utmParamQueryString; | |
} | |
})(); | |
var navLinks = document.querySelectorAll('body a'); | |
navLinks.forEach(function (item) { | |
item.href += utmParamQueryString; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment