As far as I know there is no way in Discourse to handle rewriting Amazon links, which leads to me double-checking to make sure people aren’t trying to pull a fast one when they do post an Amazon link.
This could happen in one of two ways (or both):
Add code to strip all affiliate codes from any Amazon URL
Add code to replace and/or add affiliate codes into any Amazon URL.
I know this happened over at StackExchange a long time ago, so I’m guessing it’s probably fairly easy to do.
I’m not personally that worried about adding the affiliate codes, but it would be nice to make sure that other people aren’t adding them to their links, and I’m guessing it would be a really good feature for forum owners that want to make money from links on their forum.
It’ll add your affiliate tag to any Amazon link, and also rewrite other peoples with tags with your own.
It works on the client side so it’s just a case of adding a javascript file to your head section and configuring it.
It will do the same for eBay and can be setup to work with any affiliate scheme which uses a http://domain.com/?CODE=XYZ style linking structure.
I’ll be happy to answer any questions to help you get set up.
I’m not sure entirely however I’ve just had a look at the setup instructions and it seems a lot easier compared to when I last looked. I’ll fire up a VM when I get a chance and see what I can do. Thanks for your offer and I’ll get back to you if need be.
Just a thought, it would be interesting to make such a script as configurable as possible. There are a lot of affiliate strings out there and it would be interesting to rewrite other links within the same package.
Specifically, I run a gaming forum and there are common links used, some without affiliate codes as well. I’d like to be able to tack on a code to these links to help fund the forum maintenance.
The script as it stands allows you to add any affiliate code as long as it works in the foo.com/?tag=bar type format. There is also another option to allow you to overwrite existing codes or not.
The plugin will just give a pretty GUI to manage it. Shouldn’t be too tricky to create, leave it with me
Personally I think that Discourse should automatically strip out any affiliate codes from Amazon URLs when the post is saved. No reason to recalculate that every single time, and better to have nice clean data in the database.
Adding the codes should probably happen separately, on display, and could be separate.
I’d suggest a plugin…otherwise you’ll end up either integrating other affiliate link processing into the core engine, which becomes unwieldy…or all other affiliate link processing will be configured in plugins and the amazon in another config area…making things confusing.
It might help to look at the cooked_post_processor.rb to see what Discourse does to posts. As part of the post processing after making a post, the plugin would look at the text, strip away any existing affiliate tags and add in your chosen tags (which could be defined as an environment variable in the app.yml?). Thus the text is permanently changed, which would be the cleanest solution.