Amazon AJAX Javascript Link Localiser
I recently wrote about how a cache everything on this site on CloudFlare - including the HTML pages. This does not play nice with my geo-aware cache setup which I used to generate specific pages with localised links depending which country my visitor came from. To get around this problem I have written some Javascript to be able to localise links and through an AJAX 1 call get the visitors location. It works by
- Getting the country code from the visitors IP address using AJAX using freegeoip.net 2. This makes it cache friendly
- The country code is mapped to which Amazon store it should redirect to - including which affiliate tag to use. This can be configured / changed.
- The amazon affiliate link is localised as specified in the
data-amazon-asin
attribute on the link itself.
Demo
Since we are talking about actual working amazon affiliate links below, I will present my disclaimer first!
Disclaimer
This post contains links to Amazon where I get a small commission if you purchase anything after clicking on these links - at no extra cost to you! But only if you have explicitly consented to this. I have purchased all the mentioned products myself and I only link to products that I believe are the best for my readers. If you want to help out even more, take a look here.My script will generate real Amazon affiliate links. When clicking on these links Amazon links you consent to installing an Amazon affiliate cookie on your computer. For this demo you do not need to click on the actual links, you can just hover over the link to see the changed link or inspect it using your browser. If you want to withdraw your consent, just delete the cookies from this site using your browser.
Now lets use an example link to Amazon:
With an data-amazon-asin attribute:
That results in the following clickable link:
Test link to Amazon
The link should already be localised depending on the country you arrived from. The buttons below will simulate that you arrive from another country by setting the cookie with a different country and re-running the script. Then the link above should be changed / localised. You can use the developer tools on your browser to see the changes in the HTML or just click on the link.
The expected results are
- US and ES will get a localised link to
B0084A7PI8
- UK and DE will get a localised link to
B00JGFDKBQ
- IT and FR will get a localised link to
B00PQC72ZS
- CA will get an US link to
B0084A7PI8
Notice that all the above countries have an Amazon store. To redirect countries without Amazon stores we could do the following:
Which would redirect a bunch of countries to the US Amazon affiliate store, but it is cumbersome to write each time. Therefore it is possible define the mapping of countries to other countries in this way:
The above is the default mapping. Lets try to play again with some different countries. We use the same links as before:
With an data-amazon-asin attribute:
That results in the following clickable link:
Test link to Amazon
The expected results are
- VA will get an IT link to
B00PQC72ZS
- PL and NO get an DE link to
B00JGFDKBQ
- PT will get an ES link to
B0084A7PI8
- CN will not change the link since it cannot find the
[cn]
tag in thedata-attribute-asin
, but it is defined inamazonAffiliateRegions
. If you try to refresh the page, it should return to the original.com
link when the page is loaded. UPDATE 11-02-2017: On my site I have changed CN to point at US so it will show the US link - RU will get an US link (default) to
B0084A7PI8
since it cannot be found inamazonAffiliateRegions
Installation
WordPress
I have made two simple WordPress plugins - one for getting and storing the country code in a cookie. The second reads the country code form the cookie and transforms the links.
- Go into your WordPress plugin folder, e.g.
/var/www/html/wordpress/wp-content/plugins
- git clone https://github.com/WordPress-plugins-serek/geo-ip
- git clone https://github.com/WordPress-plugins-serek/javascript-amazon-affiliate-link-localiser
- Log into WordPress and activate the above plugins
Custom
If you don’t have WordPress or just want to integrate the files manually do the following:
- Download the file amazonAjaxLinkLocaliser.js
- Download the file geoip.js
- Add the following to your site (replace
yoursite.com
with the correct path to the files)
Configuration
Edit the amazonAjaxLinkLocaliser.js
file and add your own Amazon affiliate IDs in amazonAffiliateTags
Notice that if you do not supply a Amazon affiliate tag for a store that you have configured, I will insert my own tag. Next you might want to tweak the mapping of countries to Amazon stores:
The most important configuration here is the default country. All the countries on the right hand side is the ones you should use in the data-amazon-asin
attribute, e.g. [de][es][uk][it][fr][ca][us][cn][br][in][mx]
. A full list of country codes can be found here. You can also tweak the mapping of Amazon stores to their url endings, but this should not be needed:
Lastly there is a setting that defines if the script should try to localise a link if it is already is localised to the target region:
I use it since I embed link to the .com
Amazon store with my affiliate links already. No need to waste resources localising these again.
Next action
- Unit test that tests all the variants for 404 and makes sure the item is available for purchase
- Switch from cookie to local storage
- Wordpress plugin to automatically generate the
data-amazon-asin
attribute or automatically localise links according to certain rules and mappings. These services already exists, but I need more flexibility in configuring mappings
Asynchronous JavaScript and XML (AJAX) is a set of Web development techniques using many Web technologies on the client side to create asynchronous Web applications. Ajax allows for Web pages, and by extension Web applications, to change content dynamically without the need to reload the entire page ↩
A free and open source public HTTP API to search the geolocation of IP addresses. It uses a database of IP addresses that are associated to cities along with other relevant information like time zone, latitude and longitude. You’re allowed up to 15,000 queries per hour by default. Once this limit is reached, all of your requests will result in HTTP 403, forbidden, until your quota is cleared. Alternatively it can be downloaded and installed locally to bypass these limits. ↩