Home > Technical & Creative Skills > Tracking Campaigns

Need A Geolocation Script To Redirect Users Based On Their Location? Here's One! (18)


07-25-2013 04:06 PM #1 caurmen (Administrator)
Need A Geolocation Script To Redirect Users Based On Their Location? Here's One!

One of the most common things that you'll do in affiliate marketing is redirect people based on their country. Whether you're making use of a traffic source where visitors come from every which where, or whether you're making sure Facebook ad approvers don't freak out, you'll use this code over and over again.

So, here's a quick plug-and-play script to get everything working straight away.

NOTE - if you're using the Stack That Money mobile tracker (which works for Web too), you don't need to do this - redirection is built in.

Setup

First of all, you'll need to follow these instructions to install the new Maxmind GeoIP database. That means that your IP-based location detection will be as accurate as possible.

Creating The Script

Next, create a new PHP file on the server you've installed the GeoIP database on - call it whatever you like.

Copy and paste this code into it:

Code:
<?PHP

require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$ipaddress = $_SERVER["REMOTE_ADDR"];

use \GeoIp2\Database\Reader;
$reader = new Reader('$_SERVER['DOCUMENT_ROOT']./geolocation/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);

$subid = $_GET['subid'];

switch($record->country->isoCode) {
Setting Up Your Redirects

Next, you'll set up all your locations.

For each country you want to redirect, add a block of code like this:

Code:
case 'GEOCODE':
        $redirect = 'OFFERURL'.$subid;
        header("Location:$redirect");
        exit;
Replace GEOCODE with the standard ISO geocode for that country and OFFERURL with the offer URL for the offer you want to redirect visitors from that country too, formatted as if for CPVLab (ie ending in "=").

Here's an example:

Code:
case 'US':
        $redirect = 'http://f5media.com/aff_offer.php?aff_id=325423&s1=sampleoffer&s2='.$subid;
        header("Location:$redirect");
        exit;
Finally, paste in this block of code, and replace OFFERURL with the offer you want any visitors whose country didn't detect to see:

Code:
default:
        $redirect = 'OFFERURL'.$subid;
        header("Location:$redirect");
        exit;
}
?>
Completed Script Example

Here's an example of a completed script, which will redirect UK visitors to an offer URL and everyone else to Google:

Code:
<?PHP

require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$ipaddress = $_SERVER["REMOTE_ADDR"];

use \GeoIp2\Database\Reader;
$reader = new Reader('$_SERVER['DOCUMENT_ROOT']./geolocation/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);

$subid = $_GET['subid'];

switch($record->country->isoCode) {
case 'GB':
        $redirect = 'http://f5media.com/aff_offer.php?aff_id=325423&s1=sampleoffer&s2='.$subid;
        header("Location:$redirect");
        exit;
default:
        $redirect = 'http://www.google.com'.$subid;
        header("Location:$redirect");
        exit;
}
?>
Setting Up The Link Flow

Once you've done this, all you need to do is to set up your tracker, lander or links to send everyone you want to be geo-redirected to the page you just created, with the parameter "subid=" and their subid at the end. In Prosper202, you'd do that with

Code:
http://www.yourserver.com/REDIRECT_PAGE.php?subid=[[subid]]
and in CPVLab, you'd use

Code:
http://www.yourserver.com/REDIRECT_PAGE.php?subid=
And that's it!

I hope you found that helpful! Any questions, comments, suggestions, or confusion? Post below!


07-25-2013 04:18 PM #2 julien (Member)

Thank you very much Caurmen, this is awesome.
I'll try this right now.

Also, in this thread:
http://stmforum.com/forum/showthread...our-Own-Server

You wrote:

You can get the longditude and latitude of the user with $record->location->longditude or $record->location->latitude. You can use this to get a list of nearby cities - I'll write a tutorial on how to do that soon!
You can detect if the IP belongs to an anonymous proxy with $record->traits->isAnonymousProxy . Proxied IPs might well be spy accounts, so you could theoretically then redirect them or show them an entirely fake lander!
Have you had the time to do these tutorials?

Thanks for all the value you're providing here man, you're awesome.


07-25-2013 04:20 PM #3 caurmen (Administrator)

@julien - Thanks!

I haven't had time to look into those tutorials yet - there's some original research to do, and I've got some non-tech stuff I need to catch up with on STM (look for exciting improvements soon!), so it'll be a couple of weeks - but they are coming.


07-25-2013 04:23 PM #4 julien (Member)

Oh, and I have an other question if you don't mind.
Since we're using the maxmind database with the STM Tracker V3/V4 and like you said, redirection based on country already works like a charm...
Is there a simple way to display the country in our landing pages starting from this setup?

PS: thank you for your answer above


07-25-2013 05:45 PM #5 dconstrukt (Member)

this is SO fucking gangster.


07-26-2013 08:55 AM #6 kyuss (Member)

Quote Originally Posted by julien View Post
Oh, and I have an other question if you don't mind.
Since we're using the maxmind database with the STM Tracker V3/V4 and like you said, redirection based on country already works like a charm...
Is there a simple way to display the country in our landing pages starting from this setup?

PS: thank you for your answer above
Julien, I decided to post this mini mod for the STM Tracker V4 after seeing your post, hope it helps!
http://stmforum.com/forum/showthread...306#post113306

Thanks for the awesome share Caurmen!


07-28-2013 09:35 AM #7 julien (Member)

Wow this is huge!
Thanks kyuss!


09-17-2013 04:54 PM #8 drumminlogan (Member)

So I'm trying to get this working and followed all of the steps above. When I write the php code, I get the following errors. Any suggestions?

Warning: require(vendor/autoload.php) [function.require]: failed to open stream: No such file or directory in .......
Fatal error: require() [function.require]: Failed opening required 'vendor/autoload.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in ......

Thanks


09-17-2013 06:12 PM #9 stackman (Administrator)

Beauty! Will this work with mobile?


09-18-2013 04:58 PM #10 caurmen (Administrator)

@drumminlogan - Hmm, that sounds like there's a problem with the installation of the MaxMind databases. Is there a "vendor" directory in the same directory as your LP?

You may need to modify the "require" line to include an absolute path to the vendor directory - something like

Code:
require '/var/www/vendor/autoload.php';
@stackman - yep, this should work just as well for mobile.


09-18-2013 05:37 PM #11 drumminlogan (Member)

Quote Originally Posted by caurmen View Post
@drumminlogan - Hmm, that sounds like there's a problem with the installation of the MaxMind databases. Is there a "vendor" directory in the same directory as your LP?

You may need to modify the "require" line to include an absolute path to the vendor directory - something like

Code:
require '/var/www/vendor/autoload.php';
Thanks Caurmen. There isn't a vendor directory on my lip page. I changed the require code to

Code:
require '/usr/local/apache/htdocs/vendor/autoload.php';
Now I get this error:

Warning: fopen(GeoLite2-City.mmdb) [function.fopen]: failed to open stream: No such file or directory in /usr/local/apache/htdocs/vendor/maxmind-db/reader/src/MaxMind/Db/Reader.php on line 38

Warning: fstat() expects parameter 1 to be resource, boolean given in /usr/local/apache/htdocs/vendor/maxmind-db/reader/src/MaxMind/Db/Reader.php on line 202

Fatal error: Class 'MaxMind\Db\InvalidDatabaseException' not found in /usr/local/apache/htdocs/vendor/maxmind-db/reader/src/MaxMind/Db/Reader.php on line 217


09-18-2013 06:21 PM #12 quantum27 (Member)

I had your problem. Try ssh into the user directory where your LP is held. Also, you have to run the installation at the LP root. eg ~user/public_html/mytestlp/

I'm still trying to figure out how I can just use one install and have it shared among different LPs.


09-18-2013 06:26 PM #13 quantum27 (Member)

On another note, I've been testing with this Geo script for my UK offer, but the level of detail is kinda not worth it. Just says London, which doesn't really make a compelling sell because London is so vague and general.

Ideally, I would like to see a postcode (I know the US database has it) or at least a town more specific.

I'm not even sure if it's worth adding for my UK offer, if it's just going to say London all the time. Thoughts?


09-19-2013 04:16 PM #14 caurmen (Administrator)

@quantum27 - IP-based geoscripts in the UK are a total pain in the arse, I'm afraid. For various reasons, they're usually imprecise and sometimes flat-out wrong, thanks to the locations that ISPs run from against the location that users are in.

I don't know of a way to locate a user by postcode in the UK aside from using HTML5 geolocation - and you'll have to get them to agree to that before the results come up.

@drumminlogan - Hmm. Try copying your GeoLite2-City.mmdb file into the same directory as your LP - that might well solve it. (Let me know!)

I have another solution which will probably work if that doesn't, but it's a bit more of a pain.


09-19-2013 04:47 PM #15 drumminlogan (Member)

Quote Originally Posted by caurmen View Post
@drumminlogan - Hmm. Try copying your GeoLite2-City.mmdb file into the same directory as your LP - that might well solve it. (Let me know!)

I have another solution which will probably work if that doesn't, but it's a bit more of a pain.
Awesome, that worked. I guess I'll just need to copy it into every directory I have landing pages in, but at least it works. Thanks Caurmen. I greatly appreciate it.


09-19-2013 05:01 PM #16 caurmen (Administrator)

Hurrah! Excellent.

Quick tip on the "copying" front - if you have SSH access to your server, you could actually just use symbolic links to the file. Here's a decent tutorial on that. That'll make the process easier and neater than copying over every time.


09-23-2013 01:16 PM #17 caurmen (Administrator)

OK, here's a MUCH better solution, courtesy of a PM from an extremely helpful STMer:

Replace the starting block of the original code with

Code:
<?PHP

require 'vendor/autoload.php';

$ipaddress = $_SERVER["REMOTE_ADDR"];

use \GeoIp2\Database\Reader;
$reader = new Reader('/PATH_TO_YOUR/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);

$subid = $_GET['subid'];

switch($record->country->isoCode) {
That means you can have your GeoLite2-City.mmdb anywhere you like, and just point to it in the script.


11-23-2013 10:47 AM #18 caurmen (Administrator)

Note: tutorial updated to provide an elegant solution to the above bug. Should Just Work now!


Home > Technical & Creative Skills > Tracking Campaigns