Hide Adsense Ads for specific countries

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!

According the Google Adsense terms of service it’s not allowed to manipulate their code snippet. It’s also not allowed to show ads through an IFRAME or to use JavaScript and Ajax to show or hide Adsense ads. But it’s allowed to exclude the whole Adsense snippet whenever you like. Another option is to use some ad server like DoubleClick for Publishers, DFP is a free Google service and I read somewhere that there is an option to filter ads for specific countries too.

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:

function createAdsense468by60() {
	return '<script type="text/javascript">
	google_ad_client = "ca-pub-XXXXXX";
	google_ad_slot = "XXXXX";
	google_ad_width = 468;
	google_ad_height = 60;
	</script>
	<script type="text/javascript"
	src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>';
}
add_shortcode('adsense468-60', 'createAdsense468by60');

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:

function createAdsense468by60() {
	$ad = 'place here some alternative ad code';
	include_once '/home/linuxuser/geoip.inc';
	$gi = geoip_open('/home/linuxuser/GeoIP.dat', GEOIP_MEMORY_CACHE);
	$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
	if(!in_array($country, array('NL', 'DE', 'BE'))) {
		$ad = '
	<script type="text/javascript">
	google_ad_client = "ca-pub-XXXXXX";
	google_ad_slot = "XXXXX";
	google_ad_width = 468;
	google_ad_height = 60;
	</script>
	<script type="text/javascript"
	src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>';
	}
	return $ad;
}
add_shortcode('adsense468-60', 'createAdsense468by60');

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.

Published in: PHP Scripts · WordPress Development

8 Comments

  1. The easiest way to hide Adsense for some countries, is use Google Admanager for small business which is free hosted solution. :-)

  2. Hi Sarangan,
    this is what I mentioned in my second paragraph ;)
    I don’t think it’s the easiest way because the setup is not so straight forward. For me as a web developer it’s much easier to use a tool like GeoIP. Do you know a great resource which leads to the “target” in DFP?

  3. Oh yes. Sorry, I didn’t noticed that. :-)
    Yeah you’re right that it takes some time to learn how DFP works, I think I spent a whole day to find it out. :-) GeoIP is may be easy but it uses our own server resources to find out which country every visitor comes from.

    Here are some usefull video tutorials by Google:
    http://www.youtube.com/dfp

    It takes sometime to understand what Ad unit, Placement and Orders are. If you understand those 3 terms, then everything will be easy.

    1. haha, you spent a day…. (I stopped trying after 1-2 hours)

      But you’re right it’s much better to use some hosted ad-server application that using some home-made solution. If you have several ad publishing partners DFP is the application you need.
      The reason that I published this tutorial was that there are a lot of people searching for this kind of solution (check how many people having problems to get access to DFP)

      Thanks for the link!

    1. Hi Ahmed, actually you can’t use that kind of scripts.
      I remember me some scripts in the past, but most of them might harm your Google Adsense account more than that they help. Think about illegal click activity or non-allowed snippet modifications.

  4. Hi, Olaf Lederer
    Do you know how can i hide adsense ads from blogger blogspot blog for specific country?

    1. Hi, you can’t do that on blogger. Maybe you can do that using “DoubleClick for Publishers” but this it’s much more complicated.

Comments are closed.