Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!