Created
May 31, 2023 15:00
-
-
Save sb8244/172fa5617d3ebb2f702e3958b3445f64 to your computer and use it in GitHub Desktop.
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
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