How do you make money from visitors when they could be coming from anywhere in the world?
There are a couple of simple solutions to this, if you are using Adsense then Google have got this covered for you already. They’ll already be geo-targeting their ads to your readers, so a reader sitting in America will get US based ads. Services like Skimlinks SkimWords are also capable of some geo-targeting. But that’s not what we’re discussing today.
In this post we are looking at adding geo-targeting to your WordPress plugins.
Note that the technique I discuss on this page is more about small tweaks to a user’s experience,
for example showing a different link or a currency conversion. Google’s advice for fully international sites is to have dedicated pages for each region. This makes sense because if you really are targeting different regions then the information should be different
for each area.
You can read more here.
A further consideration, Amazon supposedly does not approve of re-directing links based on geo-location. I can’t see anything specific about this in their Ts&Cs but I have read posts from people who have had their accounts closed down. Specifically showing the price in pounds and then redirecting to the US store front is confusing to users. I guess you can’t believe everything you read on the net but it’s enough to put me off. So whilst I will show users different Amazon links depending on where they live, it will be clear where that link will take them and which Amazon store I am talking about. The risky scenario is having a single link that will redirect people to different stores depending on their location. A subtle difference but potentially costly to get wrong.
There are services which offer Amazon geo-locating links, and even WordPress plugins so I guess it is a grey area. But given the importance of Amazon associates to my online income I’m not prepared to risk it.
Geo locating is usually done by grabbing the user’s IP address and looking it up in a database.
This is not 100% accurate unfortunately but is “good enough” for most applications.
There are a few different databases to choose from, for example:
Which is the best free geolocation service? I don’t know. I’m not sure anyone is going to be able to run a good objective test that answers this question either. But if it gets it right for 99% of my users then Im happy with that.
I am going to use ip2c for this example, for no other reason than it’s free and requires no setup, this should work for anyone. By all means investigate other options, but I couldn’t comment on who has the best geo lookup database, by all accounts any will be “good enough” for a hobby blog.
Using ip2c in a WordPress Plugin
I’ll run you through the PHP code you’ll need to add to your WordPress plugin. First off, you need to get the user’s IP address. You can use a PHP global variable for this:
$userIP = $_SERVER['REMOTE_ADDR']
This PHP variable is considered the safest way to get the IP of your user. It might not be their true IP, they could be behind a proxy, but for the purposes of a hobby blog it’s going to be just fine for us. We then need to send this to ip2c.org and get the result, you can find out more about passing an IP to this site from here.
Fortunately WordPress makes it easy to send an http request to another site, you can use a built in function:
$data = wp_remote_get("http://ip2c.org/?ip=".$userIP);
You can find out more about the output returned from ip2c here.The WordPress function will return the entire http response array but we only want the information held in the body, this will look something like: 1;GB;GBR;United Kingdom
And from this you will most likely want to pull out the country code. I used the PHP explode function to separate the string and pull out the elements I needed.
Now that you have a variable holding the country code of your user you can use this to control what they are seeing. For example, on my UK targeted sites I want to channel people to a UK store, but actually I find even my UK sites will get a lot of US visitors. So when I detect a US visitor I can add in some Adsense ads, or perhaps an additional link to a US store. A couple of small extras that will maximise my chance of earning something from my non-UK visitors. The user experience for my core UK visitors is unchanged. But the foreign visitors get a little extra targeted content.
Have you added geolocation to your WordPress plugin? Let me know!