Last active
May 15, 2024 19:45
-
-
Save xandreafonso/7828b60ddcfab738312e118f3dd175b3 to your computer and use it in GitHub Desktop.
Script para repassar as UTMs da página para o botão de ação (CTA)
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
<script> | |
document.addEventListener("DOMContentLoaded", () => { | |
const urlParams = new URLSearchParams(window.location.search); | |
const utm_source = urlParams.get("utm_source"); | |
const utm_medium = urlParams.get("utm_medium"); | |
const utm_campaign = urlParams.get("utm_campaign"); | |
const utm_content = urlParams.get("utm_content"); | |
const utm_term = urlParams.get("utm_term"); | |
const linksUtm = document.querySelectorAll(".utm"); | |
linksUtm.forEach((element) => { | |
var a = element; | |
if ("A" != element.tagName) a = element.querySelector("a"); | |
var url = new URL(a.getAttribute("href")); | |
if (utm_source) url.searchParams.set('utm_source', utm_source); | |
if (utm_medium) url.searchParams.set('utm_medium', utm_medium); | |
if (utm_campaign) url.searchParams.set('utm_campaign', utm_campaign); | |
if (utm_content) url.searchParams.set('utm_content', utm_content); | |
if (utm_term) url.searchParams.set('utm_term', utm_term); | |
a.setAttribute("href", url.href); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment