I’ve made some behind the scenes changes to my short-fiction.co.uk website (the current jewel in the crown, with 1,500 visitors a day!).
I’ve always had some amazon links/adverts on the site, as being a free story site, it made sense to link to their books. It’s never made much money, just some vouchers every other month or so. I realised that I probably wasn’t doing as well as I could, due to the fact I only had links and a bookshop using the UK version of Amazon, and as more than 2/3rds of my visitors from America, I was probably losing out on their potential sales.
So I read around the net, and decided to ‘geo-code’ the visitors IP address, working out where in the world they are from using their IP. It’s not 100% foolproof, but is probably better than looking at their browser country/language settings (as UK people may well be set to US). I managed to download a free database of IP ranges to country codes (106,000 rows in CSV file), which I loaded into MySql. I read up and actually did that encoding the IP ranges as spacial co-ordiates, building virtual ‘rectangles’ out of them. Then using some custom/fancy SQL, I take the vistor IP, make it a ‘point’ in this virtual IP space, and find which range of IPs the point falls in. The reason for doing that instead of a simple ‘between’ comparing strings? Performance! Using the spacial method is far faster, especially under load.
Once done and tested, I just user some simple PHP if statements, showing either USA Amazon widgets/adverts or UK, depending if their IP falls in Great Britain or not.