Skip to content

Instantly share code, notes, and snippets.

@presswizards
Created March 18, 2025 06:58
Show Gist options
  • Save presswizards/7a0052eb1efad1d07eb7324f7926d830 to your computer and use it in GitHub Desktop.
Save presswizards/7a0052eb1efad1d07eb7324f7926d830 to your computer and use it in GitHub Desktop.
UTM to button link
document.addEventListener('DOMContentLoaded', function () {
const params = new URLSearchParams(window.location.search);
const utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
let utmString = '';
utmParams.forEach(param => {
if (params.has(param)) {
utmString += `${utmString ? '&' : '?'}${param}=${encodeURIComponent(params.get(param))}`;
}
});
if (utmString) {
document.querySelectorAll('.ctabutton').forEach(button => {
const href = button.getAttribute('href').split('?')[0]; // Remove existing query
button.setAttribute('href', href + utmString);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment