In a previous article that I posted on finalwebsites.com, I explained how to redirect visitors by country of origin using maxMind’s GeoIP database (more information below). The tutorial today is about how to hide Google Adsense ads for specific countries by using the same GeoIP database. Many “regular” websites doesn’t use (or need) a file cache function, it’s more a requirement for content management system based sites. These site are generating a lot of database queries and without a cache function your site might becomes slow if the number of visitors becomes bigger. If your site doesn’t need a file cache function it’s possible to use the country code which belongs to a visitor and decide to show an advertisement or not. If your website or CMS need to use a file cache module, this simple condition becomes more complicated…
Don’t change the Google Adsense snippet!
Why should you hide Adsense ads for specific countries?
Google Adsense will show the best paying ads only to visitors from those countries where advertisers like to pay more money for a click or impressions. If your site has a lot of traffic from countries where advertisers pay less for a click, Adsense will show less paying ads even to other visitors. That’s the theory, if you like read more about this, search Google for “Google Adsense smart pricing”. Your visitor’s country is not the only criteria to receive good paying ads on your site, but it’s the only one you can’t control (if you don’t like to block the traffic from specific countries).
Showing Google Adsense on your WordPress site
There are several Adsense plugins for WordPress, but none of them is able to hide Adsense for visitors from one or more countries. Most plugins are place ads in the theme or sidebar in a generic way, I’m sure you don’t like this. Place your ads on those places with this highest CTR (click through rate) and where they are less disturbing. I use for Google Adsense ads the WordPress shortcode feature:
Place this code into your theme’s “functions.php” file and use the short code [adsense468-60] inside your blog post and voila! the Adsense ad is placed on the position of your choice. But how to hide these ad from specific countries/visitors?
The MaxMind GeoIP database
The (free) GeoLite Country database is according to the MaxMind website updated once a month and the accuracy is 99.5%. You can access the database in different ways, for this tutorial I used the binary version which I need to download to my server. Use the following function inside the Linux user directory.
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz; gunzip -f GeoIP.dat.gz
You can download the file to any directory where your PHP script has access to. I use also a CRON job and update the database file at the 5th of each month. You need to download also the GeoIP PHP module and with the GeoIP database we’re able to show/hide the Google Adsense snippet based on the visitor’s country code:
How about WordPress Cache functions and plugins?
It’s important to cache your WordPress site even if your site has less traffic. Search engine bots, hackers and other “non-human” visitors can “eat” your server’s memory and the site might getting slow or even worst.
While using regular file caching functions, it’s often not possible to use a script like above. We tried different solutions:
- W3 Total Cache plugin with APC cache enabled
This is maybe the best solution, it seems like that the this plugin is able to handle this kind of functions automatically. If this is your situation, use it and don’t try anything else.
- W3 Total Cache plugin without any opcode cache function
You can use the function above but you need to disable “page caching”. If your server is strong enough you can try this solution with only object and database caching enabled.
- Alternative solution for the W3 Total Cache or WP Super Cache plugin
If you need to use page caching or if you use WP Super Cache plugin, the short-code function is a problem. A workaround is to use the function in your WordPress theme together with the mfunc tag which is supported by both plugins. The FAQ section from each plugin provides some code examples.
If you know a snippet or solution where it’s possible to use the short code on WordPress sites where opcode caching is not available or whenever you use WP Super Cache, please share your solution here.