[GET] PHP Code To Geo Target Your Wordpress Ads

dealsopoly

Power Member
Joined
Feb 12, 2012
Messages
526
Reaction score
279
Hey fellow Balckhatters! Here's my first share. I'm still learning SEO and IM in general, but I'm up to 100 posts and I figured I'd share what I could to give back to this great community and learning center.

I have a website that gets a ton of traffic from US, CANADA and UK and it doesn't convert well with Adsense. I decided to go with affiliate marketing, the issue is that many affiliate programs are country specific. So I had to come up with a way to geo target my affiliate ads depending on the country.

I use wordpress, here's what I came up with so far and it does the trick for now.

INSTRUCTIONS:
-------------
1) Dowload ip2nation plugin from: http://ip2nation.com/
2) Create a PHP file with the following code: (I called it geoads.php)
Code:
function GetCountry()
{
 $server   = ''; // MySQL hostname
 $username = ''; // MySQL username
 $password = ''; // MySQL password
 $dbname   = ''; // MySQL db name

 $db = mysql_connect($server, $username, $password) or die(mysql_error());
       mysql_select_db($dbname) or die(mysql_error());
 $sql = 'SELECT
             country
         FROM
             ip2nation
         WHERE
             ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
         ORDER BY
             ip DESC
         LIMIT 0,1';
 list($country) = mysql_fetch_row(mysql_query($sql));
 if($country == 'ca') //CANADA
  return 1;
 else if($country == 'us') //USA
  return 2;
 else if($country == 'uk') //UK
  return 3;
 else //REST OF THE WORLD
  return 4;
}

function DisplayAd()
{
 $country = GetCountry();
 if($country == 1)
 {
  ?>
  // DISPLAY YOUR CANADA AD
  <?php
 }
 else if($country == 2)
 {
  ?>
  // DISPLAY YOUR USA AD
  <?php
 }
 else
 {
  ?>
  // DISPLAY AD FOR THE REST OF THE WORLD
  <?php
 }
}


3) In your WP header theme, somewhere between the head tags, put in the following
Code:
 <?php require("geoads.php"); ?>

4) You can now call the DisplayAd() function anywhere in your templates to display a country specific ad. You could also install Exec-PHP plugin and call the function directly within your posts if you don't want to modify the theme templates directly.

You can create as many functions as you want just like the DisplayAd() function for different ads. Ex. I've got one for TopAd(), MiddleAd(), SidebarAd(), FooterAd(), etc...
My next goal is to set up some kind of ad rotation so it will display one of 3-4 Ads. Otherwise, the site gets pretty ordinary with the same ad showing up all the time. ;)

Anyways, I'm in no means an expert coder, I just learn as I go along and thought this might be useful to someone. Have a great weekend all.
 
If you get lots of visitors, then I would recommend downloading maxminds GeoIP apache/php plugin. It uses a flat database and is a direct plugin/addon to php and is available on every single request. The plugin you linked requires inclusion, mysql and lots of "unneeded" coded. Switch and see if the performance benefit is there :)

http://www.maxmind.com/app/ip-location
 
If you get lots of visitors, then I would recommend downloading maxminds GeoIP apache/php plugin. It uses a flat database and is a direct plugin/addon to php and is available on every single request. The plugin you linked requires inclusion, mysql and lots of "unneeded" coded. Switch and see if the performance benefit is there :)

http://www.maxmind.com/app/ip-location

Could u please explain this.
 
If you get lots of visitors, then I would recommend downloading maxminds GeoIP apache/php plugin. It uses a flat database and is a direct plugin/addon to php and is available on every single request. The plugin you linked requires inclusion, mysql and lots of "unneeded" coded. Switch and see if the performance benefit is there :)

http://www.maxmind.com/app/ip-location

Not really, you're just including 2 functions and they're in a totally seperate page, not at all related to your wp theme. Whatever database maxmind uses is still coded and included in the page. So you're making database calls regardless. Plus, my code is free ;)
 
Not really, you're just including 2 functions and they're in a totally seperate page, not at all related to your wp theme. Whatever database maxmind uses is still coded and included in the page. So you're making database calls regardless.

This is very misguided in so many levels :o

What Zapdos says is simple. Use the PHP extension instead of a userland function for 2 reasons:

  • An extension is always much faster than userland code
  • A flatfile is probably faster for this kind of data that the sql overhead
 
This is very misguided in so many levels :o

What Zapdos says is simple. Use the PHP extension instead of a userland function for 2 reasons:

  • An extension is always much faster than userland code
  • A flatfile is probably faster for this kind of data that the sql overhead

Agreed 100%. I'm just saying, unless my site is going viral, i'd much rather a free alternative.
 
did google already do this? or this is related to affiliate promoting?
 
Well, it's a pretty smart thing to do.. But yet so simple. Thanks mate for sharing this, can come in handy sometime :)
 
Agreed 100%. I'm just saying, unless my site is going viral, i'd much rather a free alternative.

It is free. The difference between the free version and paid version is .02% or .2% accuracy. Also, the database used by maxmind is a flat file. It's not in a relational database like yours.
 
Nice one, this may come in handy some time.

Also the Maxmind file is probably a Berkeley DB? But yeah I also agree it would have much less overhead then SQL would. But it's probably not worth your effort unless you are getting really hammered.
 
Back
Top