Hide Google Ads based on Geographic Location (Country or City)

We’re thinking about starting an entrepreneurial (Make Money Online) extra-curricular computer club at school: applying critical reading, writing and thinking skills while trying our hand at online fundraising for our school. 

We’ll probably start with making niche-websites that are monetized by contextual ads (i.e. Google Adsense). Google has a zero-tolerance policy when it comes to click fraud (i.e. clicking on your own ads.) And while making money online ethically will be the corner-stone of our club, it’s hard to believe that students won’t be tempted to click on the ads on their own at home. 

So, we need a way to hide the google ads from showing up to students in our city so they can’t click on their own ads, but still show google ads to the rest of the world.

Background information about Google Ads for Teachers

There seem to be two types of people on the internet. People who are trying to make money online (MMO), and people who don’t. Teachers, as a general rule, fall into the second category.  

Since this blog is directed towards helping teachers use classroom technology, we’ll need to spend some time going over some background information which is common knowledge to the MMO world.

Briefly, there are two parts to Google’s contextual ads: Google AdWords and Google Adsense. 

1. Google AdWords

Google AdWords is for the people buying the ads. Companies (and individuals) bid on specific keywords related to their product. If they pay enough money (per click), then Google will serve up their ads, either beside the search results, or on content websites (i.e. see Google AdSense).

With AdWords, you can control where in the world your ads are served, how much you’re willing to spend on the keywords, as well as what you want the ads to say. The more people click on your ads and the more relevant your ads are to the landing page (where the visitor goes after  they click on your ad), the better rates you’ll get per click. 

2. Google AdSense

Many people show Google Ads on their blogs and websites as a way to make money and off-set webhosting costs. This is the Google AdSense part of the equation. Everytime a visitor clicks on a google ad, you make money. (Of course, Google doesn’t pay out until you get $100, but you get the idea.)

You can decide the size of the Google ad box, as well as where on your blog you want the ads. You can choose the colours of the ads, the type of font, the number of ads, but not the content of the ads. (There are certain restrictions imposed by Google basically to prevent you from misleading visitors into clicking the ads.)

Google’s algorithm reads your website, figures out what it’s about, and then delievers ads that Google thinks are appropriate. (It’s a computer, so sometimes it can match your website content well, and sometimes it doesn’t.)

Interestingly, Google doesn’t currently have an option to hide Google ads based on certain criteria (like IP address or the geographic location of the visitor.) 

Google makes money by charging the people running the AdWords ad campaigns more than what they pay the people running AdSense on their websites.  Of course, the actual formula is a trade secret and it’s against Google’s AdSense and AdWords policies to disclose specific information about clicks and the amount you have to pay, etc. So while we never know how much of a mark up Google is taking, it’s clearly a profitable model for them. (Of course, it helps that most people using Google Ads are small time and never reach the $100 minimum.) 

Overall, it’s in Google’s best interest to keep click fraud to a minimum and to provide quality search traffic  so the AdWords publishers will continue to advertise with them. 

There are several other ad networks out there, but Google is a tier 1 search engine. You don’t want to have your Google AdSense account banned.

How to put Google Ads in your WordPress blog

There are several excellent WordPress plugins that let you place Google Ads into your WordPress blog. (Note, you can’t do this in the free version of WordPress.com. You can only do it on a self-hosted blog using the free version from WordPress.org)

AdSense Manager by Martin Fitzpatrick (now Advertising Manager) is a simple way to manage the code that Google generates and get it into your WordPress blogs. (I’m not sure if Martin is in education, but lately he’s been working on spenglr, a site to help students conduct their own learning. Hats off to him.) 

We’re trying out Ozh’ Who Sees Ads WordPress plugin because it allows you to set up a list of rules that decides when your Google Ads should be displayed.

(We really would have prefered to use the No Adverts for Friends plugin by Donncha O Caoimh because it’s compatible with his WP Super Cache plugin, but we prefer the click and drag interface of Ozh’ plugin better. Ozh’ Who Sees Ads plugin doesn’t work with WP Super Cache out of the box, but when we get the chance we’ll look at Donncha’s and Thaya Kareeson’s post on adapting WordPress plugins to work with WP Super Cache.)

How to hide your Google Ads from certain countries.

Ozh’ Who Sees Ads plugin lets you display (or not display) your ads to visitors based on a number of user-defined criteria: if they come from the search engines, if they’re a regular reader (i.e. click on several posts on your site), if the post is old or new, if the ad has been displayed a certain number of time, etc. 

rules_all

One of the ways you could prevent students from seeing Google Ads is by setting up your ads so they are only displayed to search engine traffic (which is arguably good anyways).

If you type in the website address directly, or got referred there through a link on someone else’s site, the ads won’t display. 

This might work, but students who arrive on your site by looking for it in Google will be able to see the Google ads and click on their own ads. (Even if students are given the website address directly, many of them type it into Google and then try to click on the correct website. Apparently, not everyone who is born in this digital age is actually net-savvy.)

The beauty of Ozh’ Who Sees Ads plugin, however, is that you can set up some very complex rules in the advanced mode if you know php. (Or, if you can copy in php code from the examples below.)

If you want to only show your ads to visitors from a certain country only (or if you want to hide your Google Ads from visitors from a certain country), you can install Ozh’ IP to Nation plugin as well and then use something like this: 

rules_us

This will display Google ads only to visitors who are from the United States. (The IP to Nation code for US, Canada, and England are ‘us’, ‘ca’, and ‘gb’ respectively.)

To block Canadian visitors, the condition would be  wp_ozh_getCountryCode(0)==’ca’  and then choose, don’t display. 

While this would certainly prevent your students from seeing or clicking on their own Google ads, it might also cut out a significant source of traffic and online income.

How to hide your Google Ads from certain cities or regions.

We wanted a way to block our Google ads from displaying to specific cities or provinces and not just  specific countries. 

The IP country data used by Ozh is free and is compiled by ip2nation but it doesn’t include city data.

MaxMind provides IP geographical data and offers both commercial and free solutions. For example, they offer GeoLite City data (for free) that is accurate for over 99.5% on a country level and 79% on a city level for the US within a 25 mile radius.

In comparison, the GeoIP city data costs $370 initially with an additional $90 for the monthly updates. With the paid version, you get over 99.8% accuracy on a country level and 83% on a city level for the US within a 25 mile radius.

For our purposes, the free GeoLite data will be fine. The geographic information contained in an IP address is actually the location of your internet service provider, and not your actual home, so student privacy shouldn’t be an issue.

You can use javascript to load geographical information about your visitors but we couldn’t get this to work with Ozh’s Who Sees Ad plugin. 

We did, however, get the PHP code to work. The majority of the PHP code you need to use this data is provided by MaxMind.  (The library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public. They also provide sample code to pull out the city data for a specific IP address.) 

The data (in binary format) along with installation instructions can be downloaded from here.

In the end, we created a function that looks like this:

function wpip_func($type) { $dir = str_replace("", "/", dirname(__FILE__)); include_once($dir.'/geoipcity.inc'); include_once($dir.'/geoipregionvars.php'); $gi = @geoip_open($dir.'/GeoLiteCity.dat', GEOIP_STANDARD); $ip = strip_tags($_SERVER['REMOTE_ADDR']); $record = geoip_record_by_addr($gi,$ip); geoip_close($gi); if ($type == "country_code") {    return $record->country_code; } elseif ($type == "country_code3") {    return $record->country_code3; } elseif ($type == "country_name") {    return $record->country_name; } elseif ($type == "region") {    return $record->region; } elseif ($type == "city") {    return $record->city; } else {    return ""; }; }; 

While we’re not professional webdesigners, it works, and that’s good enough for us. If you know a better way to do it, please let us know.

We put the following condition into Ozh’s Who Sees Ads and all of a sudden, the ads don’t display for our city, but do display for everyone else.

who-sees-ads-settings-1

We’re using the condition, wpip_func(“city”)==”YourCity” to hide google ads from being displayed to computers visitng our website in our city. But you could also use wpip_func(“region”)==”YourRegion” to hide google ads from displaying to visitors in your province or state. 

Is it foolproof? Of course not. There are ways to surf the internet “anonymously” without showing your actual IP address. (That’s one of the ways we tested our plugin.)

I’m sure it’ll look very suspicious to Google if people with masked IP addresses are clicking on the ads.  But surfing the net anonymously is a little harder for your average student to do.

The code above should be enough for the programmer types to tweak their websites. If you want to download our WordPress plugin that sends back the city or region geographic information for your visitor, leave a comment below and we’ll get it to you.

Have you tried to show your students how to make money online? How did it go? How did you prevent click fraud by the students?

15 thoughts on “Hide Google Ads based on Geographic Location (Country or City)”

  1. I have applied to Google Adsense twice and been declined both times. They no longer specify a reason so I am clueless. Hence the adbrite ads I have. I have also found Kontera in text to be very valuable. Any ideas as to why Google declined me twice? Thx for the help.

    Brian

  2. Hey Brian, Actually, I’m surprised that Google declined creating an account for you. I’ve only heard of people who had an account and then were banned for click fraud. After all, there are millions of people with made-for-adsense blogs with Blogger.

    Have you tried creating an account using a different email address and website address for a Blogger account? Perhaps it’s a specific website or IP address that’s triggering the ban.

  3. Yes the best way is to use the blogger to apply for an adsene account that gives the adsene ID in a day or 2. Yes try applying from different email address and IP address and hope u will get ur account.

  4. I think google adsense is gamble you even dont know when n why google gonna ban you I recently start my blog on blog spot and own my own domain on .Com but most of webmaster I met they said one thing google will ban you even I am not into fraud clicks
    But mrAspinal I observe that adbrite dont show much ads so earning chances are low i guess .

  5. That’s an interesting comment NutraBrite – can you show us where this is against Google’s policy?

    I don’t think hidng Google Ads based on geographic location is against Google’s policy because we’re not modifying Adsense code. It’s simply using the Who Sees Ads plugin to determine whether or not it should display the Adsense code at all.

    I’m going to go re-read Google’s Adsense policies.

  6. Hey Guno, We haven’t made a plugin – we just tweaked Ozh’ Who Sees Ads plugin (above) so that it can filter based on geographic location. You could probably add a function or plugin to return the IP address of the visitor and filter based on that, but we’re teachers – coding is just a hobby for us. Then again, a plugin would make filtering out the google ads from our students a little easier.

  7. im a complete noob when it comes to this sort of stuff

    i would like my ads to NOT displey if the visitor is in the state of California, can you help me do this?

    ive already installed Who Sees Ads but am not certain how to get the region thing to work. any help would be greatly appreciated

    thanks

  8. Hi Mr. Kuroneko
    i just want to ask if there is anyway to block the whole blog – imean blogger blog- for certain countries, so that no one from those countries can access my block and click on my ads

    1. Hi Sam, there are ways to block certain places from accessing your website using the .htaccess file. Unfortunately, if you’re on a blogger account, there’s no way to access this file. (http://www.google.com/support/forum/p/blogger/thread?tid=3bbeb7cac2555ca4&hl=en)

      This is one of the reasons why we switched to a self-hosted WordPress account. Just a lot more flexibility in running our class blog the way we wanted to.

      That’s probably not the answer you wanted to hear. Sorry about that and good luck with your blog!

    1. Hey Jacks… This is a pretty old post. I don’t use this code right now. Digging around my site, I just threw the wpip_func above into a plugin and activated it, although I’m sure you could just throw it into your theme functions or mu-plugins. It’s pretty old code – I haven’t played around with the MaxMind IP data or the Ozh’s Who Sees Ads plugin in a while. The function worked, but I didn’t use it in the classroom setting because I think students would simply get around it and I didn’t want to risk my personal AdSense account. (I think there was also a glitch with caching plugins, and I was streamlining things.) I also wasn’t sure how accurate the data was in the free version. I used Ozhs IP to Nation to get the IP country data and display country flags in the comments. Sometimes people would leave comments on my site but it wouldn’t display their location accurately.

Leave a Reply

Your email address will not be published. Required fields are marked *