Advertisement Geotargetting with PHP

Geotargeting refers to the practice of displaying specific content or advertisements to a website visitor based on their physical location. Most ad networks handle the technical aspects of geotargeting their ads to specific countries themselves so that web publishers don’t need to worry about it. However, there are some applications where you can benefit greatly from setting up your own geotargeting solution. For example:

Geotargeting setup

Maxmind.com offers a very nice free version of their geotargeting solution, GeoIP. In this article we’ll cover setting this up to work in a php-driven website, but Maxmind also offers code libraries that work under C, Perl, Java, and ASP. All you'll need to follow the steps below is a server or web hosting account that supports php. A database such as mySQL is not required.

1. Download the geoIP.dat file, uncompress it, and upload it to your website – Go to www.maxmind.com/app/geoip_country and click on the “Download the latest GeoLite Country Binary Format” link. Save the file, uncompress it (Winzip can handle uncompressing the .gz file), and upload the geoIP.dat file to your website.

2. Download the php API file and upload it to your website – Go to www.maxmind.com/download/geoip/api/php and download the geoip.inc file. Upload it to the same directory on your website as the GeoIP.dat file.

3. Test it out – Create a new php file in the same directory as the other files to test everything out. Name it test.php and paste in the following code:

<?php
/*
test.php -- Outputs the current visitor's
country code and country name in the following format:
US -- United States
*/

include("geoip.inc"); // include the geoip functions
$geofile = geoip_open("GeoIP.dat",GEOIP_STANDARD);
echo geoip_country_code_by_addr($geofile, $_SERVER['REMOTE_ADDR']);
echo " -- ";
echo geoip_country_name_by_addr($geofile, $_SERVER['REMOTE_ADDR']);
geoip_close($geofile);
?>

Save the file to your server and open it in a web browser. If everything is set up correctly you should see your correct country code and country name. Note that in all of the examples below we reference countries by their country code. For a complete list of country codes see this site.

Now that everything’s set up and working we can start using our geotarteting system for something useful!

Geotargeting for YPN

Many web publishers are suddenly becoming very interested in geotargeting due to YPN’s policy of banning sites that allow YPN ads to be displayed to non-US visitors. Here’s some example code you can use to display YPN ads only to US visitors and Adsense ads to everyone else:

include("geoip.inc"); // include the geoip functions
$geofile = geoip_open("GeoIP.dat",GEOIP_STANDARD); // open the geoip data file
$cc = geoip_country_code_by_addr($geofile, $_SERVER['REMOTE_ADDR']);
geoip_close($geofile); // close the data file

if($cc == "US") {
// It’s a US visitor. Display YPN ads.

// Replace the YPN code below with your own YPN code.
// Be sure to precede all "quotes" in the ad code with backslashes as shown in the example.

echo "
<script language=\"JavaScript\" type=\"text/javascript\">
<!--
ctxt_ad_partner = \"xxxxxxxxxxxxxxx\";
ctxt_ad_section = \"xxxxxx\";
ctxt_ad_bg = \"\";
ctxt_ad_width = 468;
ctxt_ad_height = 60;
ctxt_ad_bc = \"FFFFFF\";
ctxt_ad_cc = \"FFFFFF\";
ctxt_ad_lc = \"FF6600\";
ctxt_ad_tc = \"004E82\";
ctxt_ad_uc = \"004E82\";
// -->
</script>
<script language=\"JavaScript\" src=\"http://ypn-js.overture.com/partner/js/ypn.js\">
</script>
";
} // end if US

else {
// It’s a non-US visitor. Display Adsense ads.

// Replace the Adsense code below with your own Adsense code.
//Be sure to precede all "quotes" in the ad code with backslashes as shown in the example.
echo "
<script type=\"text/javascript\"><!--
google_ad_client = \"pub-xxxxxxxxxxxxxxxxx\";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = \"468x60_as\";
google_ad_type = \"text\";
google_ad_channel =\"xxxxxxxxxxxxxxxx\";
google_color_border = \"FFFFFF\";
google_color_bg = \"FFFFFF\";
google_color_link = \"FF6600\";
google_color_url = \"004E82\";
google_color_text = \"004E82\";
//--></script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>
";
} // end else

Be sure to replace the YPN and Adsense codes with your own, and to escape all quotes in the ad code with backslashes \”like this\”. Paste or include() this code into your page where you want the geotargeted YPN/Adsense ads to appear and you should be all set.

CPA Multiple countries example

Many CPA ad network offers only pay for leads from a specific country. For example, Azoogle is currently running 3 different “Free Smiley” offers – one that only pays for US leads, one that only pays for UK leads, and one that pays (less) for leads from any country. Using our geotargeting solution, we can show the US offer to US visitors, the UK offer to UK visitors, and the international offer to all other visitors, thus maximizing our revenue. Here's some example php code:

[php]

include("geoip.inc"); // include the geoip functions
$geofile = geoip_open("GeoIP.dat",GEOIP_STANDARD); // open the geoip data file
$cc = geoip_country_code_by_addr($geofile, $_SERVER['REMOTE_ADDR']);
geoip_close($geofile); // close the data file

if($cc == "US") {
// It’s a US visitor. Display the US ad here.
echo "US ad code goes here.";
} // end if US

elseif($cc == "GB") {
// It’s a UK visitor. Display the UK ad here.
echo "UK ad code goes here.";
} // end if GB

else {
// It’s an international visitor (not from one of the above countries). Display the international ad here.
echo "International ad code goes here.";
} // end else.

[/php]

This code can be easily modified to include more country-specific ads for other countries if needed.

Other considerations

Conclusion

Hopefully this article has given you a basic understanding of how to put the power of geotargetting to use on your sites. With a basic knowledge of php each of the examples above can be extended to accomplish almost any geotargeting need you may have. At the very least they should keep you from being kicked out of YPN and help you maximize your revenue from country-specific ads. I visit the Website Publisher Forum regularly, so please feel free to post any questions or problems there and I’ll do my best to help you out.