title: Replace parameter in a url string using JavaScript inside Google Tag Manager
body: <p>I am trying to replace a parameter in a url with another using GTM. In the link below, I want to replace amazon affiliate tag ID "bestelectricridesweight-20" with "electricridessg-20"</p>
  
  <blockquote>
    <p><a href="https://rads.stackoverflow.com/amzn/click/com/B01NH2WD3Z" rel="nofollow noreferrer" rel="nofollow noreferrer">https://www.amazon.com/SWAGTRON-T6-Off-Road-Hoverboard-Certified/dp/B01NH2WD3Z?psc=1&amp;SubscriptionId=AKIAIWWM3HVERXCJ5HLA&amp;tag=bestelectricridesweight-20</a></p>
  </blockquote>
  
  <p>I tried this javascript code but it's not working:</p>
  
  <pre><code>(function () { 
      var linkss = document.querySelectorAll('a[href*="amazon.com"]') //change amazon.com to any domain you want to target
      var searchString = "bestelectricridesweight-20" //the string to be searched for
      var replacementString = "electricridessg-20" //the replacement for the searched string 
  
      linkss.forEach(function(linkd){
          var original = linkd.getAttribute("href");
          var replace = original.replace(searchString,replacementString)
          linkd.setAttribute("href",replace)
  </code></pre>