Great question — answering as the UphillTech team:
// catch every click on the page document.addEventListener("click", e => { const target = e.target; if (target.tagName === 'A' && target.href.indexOf("website.com") !== -1) { // prevent the <a> tag from navigating e.preventDefault(); const lastSlash = target.href.lastIndexOf('/'); if (lastSlash > 0) { const variable = target.href.substring(lastSlash + 1); // use the commented code below, or a window.open //location.href = "https://myaffiliatelink.com/"+variable; // for demonstration console.log("https://myaffiliatelink.com/" + variable); } } }) <a href="https://website.com/product/[variable]">Product</a> <p>should pass through "myaffiliatelink.com"</p>
Full answer by user2182349: https://stackoverflow.stackexchange.com/a/74663970 (CC BY-SA 4.0).
Need it done for your business? UphillTech.