Discourse Affiliate Linker: A Theme Component to Simplify Making Affiliate Links

What at first seemed silly, is just what I needed!

Discourse Affiliate Linker: A Theme Component to Simplify Making Affiliate Links

How a one-off project became a tool I am excited to use myself

On the Discourse Marketplace I noticed a call for a theme component to rewrite urls. They wanted any link to example.com to instead link to https://example.com/affiliate/my-code. Strangely, they did not want links to https://example.com/product/1234 to include a referral code, just links to the homepage.

The Challenge

The race was on. The request was posted at 4AM my time. If I was going to get this gig, I’d need to decide that I could do it for their offered price, and make an offer before someone else who’s actually good at javascript would do it.

Javascript is still confusing to me. I can manage the language itself just fine, but things like how to target elements on the page so that you can act on them is still something where I need a solid example to follow. Thankfully, I had already written a theme component that rewrites Garmin and Strava oneboxes into iframes, so I had an example to follow, which, since I wrote it, I should be able to understand!

Challenge accepted. I posted publicly that I could help out, and sent a PM.

Success!

In what seemed like a reasonable amount of time for a competent developer, which did include a few clever asks to chatGPT, like “here’s some code that targets oneboxes, I need to target <a> tags in <dig class="cooked">—See? The most basic stuff I still barely understand. (element.querySelectorAll(".cooked a") is obvious once I saw it; perhaps by publicly shaming myself I’ll remember this time.)

I added a theme setting for their affiliate code, checked that it worked on a test site, resisted the urge to move the hostname into a setting too, told them that I could have it done tomorrow, and did they want to use one of several payment links or did they need a proper invoice. They were from the UK, so I was pretty sure that they’d want something proper.

They added me to a github repo and after I received payment, I commmitted the code. Job well done! (Actually, and predictably, several other small things still stood in way of the job actually being done, which ate at least as much time as it took to write the code.)

I still thought it odd that the site wanted referrals for their home page and not their products, and while I was going on about how silly that was, it occurred to me how often I need to generate referal links for Digital Ocean, Bunny.net, and massive.io and when I do that, I only want to replace the home page with the referral link, and not links to other stuff on their sites.

So, I swapped out the hostname and the referral link settings for a single affiliate_link_map that takes a list of comma separated “hostname,referral link” settings and re-writes all of them.

Screenshot of theme settings

Theme settings dialog for discourse-affiliate-linker

I made a decent readme document, posted it publicly on github, and created a topic on meta.discourse.org.

You can see it in action over on dashboard.literatecomputing.com.

Cookies
essential