Amazon Associate Link Localiser

July 14th, 2009

I’ve been a member of Amazon’s Associate program for a number of years and have posted affiliate links to books discussed on this very blog. The program means that by putting my affiliate ID into any links to Amazon products, I receive a small referral fee.

I’ve always linked to as that’s my local Amazon store, but of course that’s not necessarily true for my visitors – or yours…

To solve this problem, I’ve written some JavaScript and a little bit of PHP which will convert any Amazon links on your page into affiliated links to your visitor’s local Amazon store. So US visitors will get a .com link, whilst UK visitors will get a link to the same product.



Edit amazon-localiser.js to insert your regional Associate IDs into the array at the top, for example:

var arrAffiliates = {
	'' : 'petewill-19',
	'com'	: 'petewill-20',
	'de'	: 'petewill05-21',
	'fr'	: 'petewill-21',
	'ca'	: 'petewill00-20',
	'jp'	: '',
	'it'	: petewill04-21',
	'cn'	: petewill-23',
	'es'	: petewill0d4-21'

If you do not have an ID for each country, you can just leave them blank, as above for .jp.

In most cases, you will also need to set the path to amazon-localiser.php, for example:

var strUrlAjax = '/inc/amazon-localiser.php';

Upload and Embed

As well as linking to amazon-localiser.js you need to link to the Google JS API file (5 kb) or the script will not work. Insert the following code just before your </body> tag:

<script type="text/javascript" src=""></script>
<script type="text/javascript" src="amazon-localiser.js"></script>

Upload your modified amazon-localiser.js file, and make sure the above link points to the right location.


That’s it – job done! You can now just link to any product on any Amazon site with a normal, unaffiliated link and your users will get one affiliated for their country. Initially, the script looks for a product with the same product ID as the one in your link, but if it can’t find one on the user’s local store, it will link to a search results page searching for the name of your product. This means that it will still be found even if international versions have different product IDs, and if it’s not stocked at all, your user will be shown similar alternatives.


The script is running on this page, so hopefully this example should be affiliated specially for you!


Follow me on Twitter to be sure you hear about new updates.


If you find this plugin helpful and it’s increasing your sales. please consider donating just $1 or more to say thanks for the many, many hours I’ve spent on it. I’d really appreciate it!


If you do not insert your own affiliate IDs the script will use mine. Hopefully this will help fund my book addiction!

21 Responses to “Amazon Associate Link Localiser”

  1. Tricia says:

    Wow this looks like a great script!

    just one question – where exactly do we put amazon.js and the Google JS API file? In our websites header or footer?


  2. Hi Tricia, it’ll work fine wherever you put it, but for performance issues, I’d recommend you put it at the bottom of your page, just before the </body> tag.

  3. Tricia says:

    Hi Pete

    So I could put both of them in say in my blogs footer? Just want to be sure that I’ll be doing it right.


  4. Yes, that would be fine.

  5. Gareth says:

    thanks for this :-)

  6. [...] If you want to achieve the same result on a non-Wordpress site, you can simply install the pure JavaScript version. [...]

  7. Slavi says:

    Do you have to signup for each amazon TLD separately ?

  8. Yes, each store as a separate affiliate program which you need to sign up to. They will each give you a unique affiliate ID which you store in the plugin settings.

  9. Glumbo says:

    Thanks for the script. Perfect for my drupal website!

  10. Karim says:

    Great script and very useful, I’ll be trying it on my WordPress and hopefully it’ll save me a lot of time of manual links entry .

  11. [...] des mêmes avantages que sur WordPress sur d’autres plateformes de blog, vous pouvez installer la version Java sur votre [...]

  12. Hi Pete, script looks great but I have one question:

    You stated that I can insert an unaffiliated link to the post.
    But, if I put an affiliated link, will this script works and change the link anyway?

    I want to install your script to my blogs which already has Amazon contents and don’t feel like going through all the troubles of changing all those inserted links.


  13. Yes that’ll work fine.

  14. Hi Holger,

    Yes those will work fine.


  15. Holger says:

    Thanks Pete. Will try it out :)

  16. Great tip this. I’m going to try to follow the example above. I feel I may run into trouble as I often favour Amazon’s “short url” so will report back if I get any questions with it.

  17. Mathias says:

    Hi and thanks for this script. It looks like it’s exactly what I need but I would like to know if anyone here has yet been successful at using it on a Drupal site. I haven’t but i might be doing something wrong. Is there a plan to port this to Drupal 7 as a module ?
    That would be great… :)

  18. Hi, I’ve no experience with Drupal, so I had no plans to migrate the module. That said, it shouldn’t be too hard to get across, so if anyone has any experience of Drupal modules and wants to help out, then I should be able to get it working.

  19. I tried using this script with my Joomla Site. I put the script in my root folder and then modified the template directly to include the two links (Google’s API and yours) and also put the php file in the root. I modified the js to include my tag and also to point it to the location of the php.

    I checked that both your js and Google’s load up in my page and both are found and executing. However, when I clicked on a link it didn’t replace the affiliate tag. The links I’m trying to replace are in the weblinks (on the right site of my site. They currently already have a affiliate link so I chose a different one in your script (to see if it will replace) and it didn’t work.

    This may be (but I’m not certain) about how Joomla handles weblinks and redirects them. Could you take a look and see if there is a way to make it work? My website is live site and I will leave the js in there.

    I wanted to use this because there is no plugins for Joomla that will do the trick to the weblinks modules. And there is not hooks for me to make my own plugin to do this. So JS was my only hope.

  20. Axel says:

    I have installed this on my test-system today.
    Works perfectly with Drupal and the Amazon Module.
    Simple install the Amazon module.
    Then install the script of this site.
    Add the java script to your template.
    Use Views to create a page or block of Amazon Products.

    Thanks for this script.
    Just donated some bucks to you.

    Best Regards

Add a comment