1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[GET] PHP Code To Geo Target Your Wordpress Ads

Discussion in 'PHP & Perl' started by dealsopoly, Apr 28, 2012.

  1. dealsopoly

    dealsopoly Power Member

    Joined:
    Feb 12, 2012
    Messages:
    526
    Likes Received:
    278
    Home Page:
    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.
     
  2. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    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
     
  3. finerpleasures

    finerpleasures Regular Member

    Joined:
    Feb 17, 2009
    Messages:
    322
    Likes Received:
    97
    Could u please explain this.
     
  4. dealsopoly

    dealsopoly Power Member

    Joined:
    Feb 12, 2012
    Messages:
    526
    Likes Received:
    278
    Home Page:
    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 ;)
     
  5. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    This is very misguided in so many levels :eek:

    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
     
  6. dealsopoly

    dealsopoly Power Member

    Joined:
    Feb 12, 2012
    Messages:
    526
    Likes Received:
    278
    Home Page:
    Agreed 100%. I'm just saying, unless my site is going viral, i'd much rather a free alternative.
     
  7. diandr4

    diandr4 Newbie

    Joined:
    Oct 22, 2008
    Messages:
    43
    Likes Received:
    3
    Home Page:
    did google already do this? or this is related to affiliate promoting?
     
  8. Brottaren

    Brottaren Newbie

    Joined:
    Apr 29, 2012
    Messages:
    17
    Likes Received:
    1
    Well, it's a pretty smart thing to do.. But yet so simple. Thanks mate for sharing this, can come in handy sometime :)
     
  9. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    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.
     
  10. jcbizzled

    jcbizzled Registered Member

    Joined:
    Aug 23, 2010
    Messages:
    50
    Likes Received:
    12
    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.