Mapping visitor IP addresses to Amazon sites

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,581 bytes PHP class
IPToAmazon.data 233,305 bytes Database (updated July 2014)

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
// include the IPToAmazon class
require_once './class.IPToAmazon.php';

// create an instance of IPToAmazon for the visitor’s IP address
$ipToAmazon = new IPToAmazon();

Alternatively, an IP address can be specified explicitly:

1
2
3
4
5
// include the IPToAmazon class
require_once './class.IPToAmazon.php';

// create an instance of IPToAmazon for the IP address 12.34.56.78
$ipToAmazon = new IPToAmazon('12.34.56.78');

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
// check whether the visitor should use amazon.co.uk
if ($ipToAmazon->getSite() == IPToAmazon::AMAZON_CO_UK){

  // the visitor should use amazon.co.uk

}

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_IN amazon.in India
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
// check whether the visitor is from a country with its own Amazon site
if ($ipToAmazon->getMatchType() == IPToAmazon::MATCH_COUNTRY){

  // the visitor is from a country with its own Amazon site

}

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.

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
// determine the address of the visitor’s Amazon home page
$url = $ipToAmazon->getURL();

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
// determine the address of The Rise And Fall Of The British Empire
$url =
    $ipToAmazon->getURL(
        IPToAmazon::AMAZON_COM,
        array(
          IPToAmazon::AMAZON_COM   => '031216985X',
          IPToAmazon::AMAZON_CO_UK => '0349106673'
        ));

This example function call would return one of two addresses:

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
// determine the address of The Rise And Fall Of The British Empire
$url =
    $ipToAmazon->getURL(
        IPToAmazon::AMAZON_COM,
        array(
          IPToAmazon::AMAZON_COM   => '031216985X',
          IPToAmazon::AMAZON_CO_UK => '0349106673'
        ),
        array(
          IPToAmazon::AMAZON_COM   => 'examplecom',
          IPToAmazon::AMAZON_CO_UK => 'examplecouk'
        ));

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
// determine the address of The Lambda Calculus, Its Syntax And Semantics
$url =
    $ipToAmazon->getURL(
        IPToAmazon::AMAZON_COM,
        '0444875085',
        array(
          IPToAmazon::AMAZON_COM   => 'examplecom',
          IPToAmazon::AMAZON_CO_UK => 'examplecouk'
        ));

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
// determine the address of the visitor’s Amazon home page
$url =
    $ipToAmazon->getURL(
        IPToAmazon::AMAZON_COM,
        null,
        array(
          IPToAmazon::AMAZON_COM   => 'examplecom',
          IPToAmazon::AMAZON_CO_UK => 'examplecouk'
        ));

Where now?

Found this useful? Share it:

Also in PHP: