Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
How to add traffic ID's to multiple different links?

I'm not a coder, but I know this should not be too hard.

Currently I'm running this script on my site to track where the traffic is coming from when they purchase through the affiliate links on my website:

 <script>
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return("");
}

window.onload = function(){
    var anchors = document.querySelectorAll('a[href*="https://example.com/c/215029/222"]');
    Array.prototype.forEach.call(anchors, function (element, index) {
    element.href = "https://example.com/c/215029/222/?tid=" + getQueryVariable("tid");
});
}
</script> 

When someone visits my site with this url https://mywebsite.com/tid=facebook the above script is changing the specific link on my website to https://example.com/c/215029/222/?tid=facebook

My question is how to change the code so that it can change multiple links on my website to accept /?tid=facebook when I add /?tid=facebook at the end of the visitors url?

Let's say I also have a second link on my website https://example.com/c/215029/example2 that needs to adapt the /?tid=facebook so that it becomes https://example.com/c/215029/example2/?tid=facebook

What needs to be changed in the code above to make this work?

I hope I explained correctly

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Consider the logic of the following example:

function setUrlQuery(link, params)
{
  console.log("[setUrlQuery]: INPUT URL # ", link);
  const url = new URL(String(link));
  const se = new URLSearchParams(url.search);
  for (const [key, val] of Object.entries(params)) {
    se.set(key, val);
  }
  url.search = se.toString();
  return url.toString();
}

function delUrlQueryParam(link, ...par)
{
   console.log("[delUrlQueryParam]: INPUT URL # ", link);
   const url = new URL(String(link))
   const se = new URLSearchParams(url.search);
   if (par.length > 0) {
      par.forEach((val) => se.delete(String(val)));
   }
   url.search = se.toString();
   return url.toString();
  }

// set new parametres if not exists
const result = setUrlQuery("https://example.com/c/215029/example2/", {
  foo: "123",
  baz: "false"
});
console.log("[setUrlQuery]: RESULT # ", result);

// Update an existing values in the url query string.
const result_2 = setUrlQuery(result, {
  tid: "facebook",
  foo: "0",
  baz: "true"
});
console.log("[setUrlQuery]: RESULT # ", result_2);

// Delete an existing values
const result_3 = delUrlQueryParam(result_2, "foo", "baz");
console.log("[delUrlQueryParam]: RESULT # ", result_3);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda