Amazon link rewriting for affiliate codes

(Lowell Heddings) #1

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.

Editing Old Posts and Adding Links Doesn't Alert Anybody
(Juffin) #2

Yes please.

(Dean) #3

I created a script to do exactly this, it’s here: GitHub - deanbarrow/Affililink: A free way to automate your affiliate scheme.

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 Domain Name Registration and Web Hosting | style linking structure.

I’ll be happy to answer any questions to help you get set up.

(Jeff Atwood) #4

Have you tested it in Discourse? Does it work on a Discourse forum?

(Dean) #5

I don’t see why not but unfortunately I don’t have a forum running at the
moment. If it doesn’t by default I will be able to patch it.

Is anyone willing to help? Please get in touch.

(Sam Saffron) #7

This should be fairly simple in a plugin. @codinghorror do you think this is a core feature or plugin thing?

(Camille Roux) #8

Any news about the plugin?

(Travis) #9

I second the love for this plugin.

(Dean) #10

If someone could give me access to a working installation I could put a plugin together.
I don’t currently have the resources to setup discourse.

(Travis) #11

How much access do you need? I’m willing to help if I can. My forum is at

(Dean) #12

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.

(Travis) #13

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.

(Dean) #14

The script as it stands allows you to add any affiliate code as long as it works in the 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 :wink:

(Camille Roux) #15

Did someone start to work on it?
I’m still interested.

(Camille Roux) #16

I started working on a solution. Can some one help me to finish it?

(Lowell Heddings) #17

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.

(Dave Mackey) #18

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.

(Camille Roux) #19

It’s a good idea. Could you tell me how to do that?


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.

(Dean) #21

I’ve got a local version of Discourse running now so I’m going to start looking at this.