Skip to content

Instantly share code, notes, and snippets.

@sb8244
Created May 31, 2023 15:00
Show Gist options
  • Save sb8244/172fa5617d3ebb2f702e3958b3445f64 to your computer and use it in GitHub Desktop.
Save sb8244/172fa5617d3ebb2f702e3958b3445f64 to your computer and use it in GitHub Desktop.
var links = document.getElementsByTagName('a');
var params = new URLSearchParams(window.location.search);
var utmParams = {};
params.forEach(function (val, name) {
if (name.startsWith('utm_')) {
utmParams[name] = val;
}
});
for(var i = 0; i < links.length; i++) {
var link = links[i];
var url = new URL(link.href, window.location);
if (link.href && (url.protocol === 'http:' || url.protocol === 'https:')) {
var changed = false;
Object.keys(utmParams).forEach(function(param) {
changed = true;
url.searchParams.set(param, utmParams[param]);
});
if (changed) {
link.href = url.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment