Skip to content

Instantly share code, notes, and snippets.

@khanhicetea
Last active November 7, 2019 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanhicetea/3e4026d98fcc32e6c5e3913bc9f7b26a to your computer and use it in GitHub Desktop.
Save khanhicetea/3e4026d98fcc32e6c5e3913bc9f7b26a to your computer and use it in GitHub Desktop.
Pass UTM query params to all link in web page
(function() {
const params = new URLSearchParams(window.location.search);
const utm_params = [];
params.forEach(function(value, key) {
if (key.startsWith('utm_')) {
utm_params.push(key+'='+value)
}
})
utm_search = utm_params.join('&');
if (!!utm_search) {
document.querySelectorAll('a[href]').forEach(function(ele, idx) {
ele.href = ele.href + (ele.href.indexOf('?') === -1 ? '?' : '&') + utm_search;
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment