Last active
November 7, 2019 14:16
-
-
Save khanhicetea/3e4026d98fcc32e6c5e3913bc9f7b26a to your computer and use it in GitHub Desktop.
Pass UTM query params to all link in web page
This file contains hidden or 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() { | |
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