Link rewriting for affiliate codes

Whoa, what a fantastic and educational tutorial!

The JSFiddle was an just an example I found. This new potential partner of ours uses a https:// -prefix, rather than and affiliate ID at the end. They use the adtraction.com platform.

Posting my modified version here for others to exploit.


<script type="text/discourse-plugin" version="0.8.42">
api.decorateCookedElement(
  element => {
        const affiliate = 'https://their-ad-network.com/...';
        const affiliate_links = element.querySelectorAll('a[href*="potentialparterwebsite.com"]');

        affiliate_links.forEach(function(el) {
            if (!el.href.startsWith(affiliate)) {
                el.href = affiliate +  encodeURIComponent(el.href);
            }
        });
    },
  {
    onlyStream: true
  }
);
</script>

So now the code ads the ad networks URL first, that tracks the clicks, and then appends with partner link.

3 likes