# Link rewriting for affiliate codes

**URL:** <https://meta.discourse.org/t/link-rewriting-for-affiliate-codes/52949>\
**Category:** Feature\
**Created:** [16 november 2016 om 02:51 UTC](https://meta.discourse.org/t/link-rewriting-for-affiliate-codes/52949 "2016-11-16T02:51:40Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![ljpp](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ljpp/32/96506_2.png) [@ljpp](https://meta.discourse.org/u/ljpp)\
**Post date:** [29 januari 2021 om 20:41 UTC](https://meta.discourse.org/t/link-rewriting-for-affiliate-codes/52949/9 "2021-01-29T20:41:21Z")

</div>

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](http://adtraction.com) platform.

Posting my modified version here for others to exploit.

```plaintext

<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.

---

_[View the full topic](https://meta.discourse.org/t/link-rewriting-for-affiliate-codes/52949)._
