Skip to content

Instantly share code, notes, and snippets.

@jamesasu
Created May 24, 2023 22:52
Show Gist options
  • Save jamesasu/bf151fecd3845e56bf7f748bc1db68e5 to your computer and use it in GitHub Desktop.
Save jamesasu/bf151fecd3845e56bf7f748bc1db68e5 to your computer and use it in GitHub Desktop.
(function() {
window.addEventListener("DOMContentLoaded", (event) => {
const hostname = window.location.hostname;
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) {
if (hostname && ele.href.indexOf(hostname) == -1) {
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