Amazon operates a network of localised sites, covering the United States, Canada, China, Germany, France, Italy, Japan, and the United Kingdom. These localised sites greatly benefit Amazon’s customers, but can cause problems for web site owners wishing to link to Amazon products. If your web site has an international audience, you must choose which Amazon sites to link to. Directing a visitor to the wrong site inconveniences them, but a long list of sites looks cluttered and may confuse them.
If you are an Amazon Associate the problem is not just one of visitor inconvenience, but of lost income. Each Amazon site operates its own affiliate programme, and when a visitor moves from one Amazon site to another the affiliate tracking is lost.
IPToAmazon solves this problem by allowing you to determine the appropriate Amazon sites for your visitors based on their IP addresses. It uses a locally stored database file and can perform over 10,000 look-ups per second on a modern server.
Download IPToAmazon
Download the two files below and upload them to your web server. You should periodically update your copy of the database to the latest version.
| File | Size | Description |
|---|---|---|
| class.IPToAmazon.php | 7,481 bytes | PHP class |
| IPToAmazon.data | 204,255 bytes | Database (updated May 2013) |
Note that the database is derived from the IpToCountry database, and hence is licensed under the terms of the GNU General Public License, Version 3. The PHP class is, like most content on this site, released under the terms of the CC0 1.0 Universal legal code.
Specifying the IP address
The simplest way to create an instance of IPToAmazon is to call the constructor with no parameters, which creates an instance for the visitor’s IP address:
1 2 3 4 5 |
|
Alternatively, an IP address can be specified explicitly:
1 2 3 4 5 |
|
Amazon sites and match types
The ‘getSite’ function can be used on an instance of IPToAmazon to determine the appropriate Amazon site for the IP address. For example:
1 2 3 4 5 6 |
|
The function returns one of eight class constants, corresponding to the eight Amazon sites:
| Constant | Site | Country |
|---|---|---|
| IPToAmazon::AMAZON_COM | amazon.com | United States |
| IPToAmazon::AMAZON_CA | amazon.ca | Canada |
| IPToAmazon::AMAZON_CN | amazon.cn | China |
| IPToAmazon::AMAZON_DE | amazon.de | Germany |
| IPToAmazon::AMAZON_ES | amazon.es | Spain |
| IPToAmazon::AMAZON_FR | amazon.fr | France |
| IPToAmazon::AMAZON_IT | amazon.it | Italy |
| IPToAmazon::AMAZON_CO_JP | amazon.co.jp | Japan |
| IPToAmazon::AMAZON_CO_UK | amazon.co.uk | United Kingdom |
The ‘getMatchType’ function can be used on an instance of IPToAmazon to determine the reason for a specific site being chosen. For example:
1 2 3 4 5 6 |
|
The function returns one of four class constants:
| Constant | Description |
|---|---|
| IPToAmazon::MATCH_COUNTRY | The IP address corresponds to a country with its own Amazon site. For example, a visitor from France will be directed to the Amazon site for France. |
| IPToAmazon::MATCH_LANGUAGE | The IP address corresponds to a country whose primary language is the same as that used by an Amazon site. For example, a visitor from Austria will be directed to the Amazon site for Germany as most Austrians speak German. |
| IPToAmazon::MATCH_NEITHER | Neither of the above conditions is true for the IP address. The visitor will be directed to the main amazon.com site. |
| IPToAmazon::MATCH_RESERVED | The IP address is a reserved address and hasn't been assigned to a country yet. This may occur if your copy of the IPToAmazon database is out of date. The visitor will be directed to the main amazon.com site. |
Creating links
The ‘getURL’ function can be used on an instance of IPToAmazon to create links to Amazon products. It can generate either normal URLs or URLs including an affiliate tracking code, depending on how many parameters are passed.
When called with no parameters, the function returns the address of the appropriate Amazon home page. For example:
1 2 |
|
When called with two parameters, the function returns the address of a particular Amazon product. The first parameter should be one of the Amazon site constants, specifying a ‘default’ site. The second parameter should be an array mapping the site constants onto ASINs (Amazon Standard Identification Numbers — the codes Amazon use to identify their products). If no mapping is specified for the visitor’s site, the function instead returns the equivalent address for the default site. For example:
1 2 3 4 5 6 7 8 |
|
This example function call would return one of two addresses:
- http://www.amazon.co.uk/dp/0349106673/ if the visitor’s site is amazon.co.uk
- http://www.amazon.com/dp/031216985X/ if the visitor’s site is amazon.com or any other site
Creating affiliate links
To create affiliate links, the ‘getURL’ function should be called with three parameters. The third parameter should be an array mapping the site constants onto your Amazon Associates IDs for those sites. For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
This example generates the same links as the previous example, but with the addition of an affiliate tracking code.
Some products have the same ASIN across multiple Amazon sites. If a product has the same ASIN on all the sites for which you are an Amazon Associate, you can instead pass a single string as the second parameter. For example:
1 2 3 4 5 6 7 8 9 |
|
Finally, passing null as the second parameter will cause the function to return the address of the appropriate Amazon home page, including the affiliate tracking code:
1 2 3 4 5 6 7 8 9 |
|