Using Adsense with geo and language targeting on cached pages
Marc
So if you have been reading my blog (yeah right) you might have read the entry about my successful attempt to do fullsite caching with my Mambo driven website. Because of the way I implemented this caching method, all unregistered users get the same page which ist very fast and therefor great most of the time. Now, I also use Google's Adsense program on my site which works, as you probably know, simply by including javascript code into your pages. One thing I like doing with Adsense is to serve their referal ads for Firefox or the Google Pack, but since the Google code does not check whether the user is already on Firefox, useless ads are served. Google also lacks a language testing routine, meaning that German users will get the English referal link or banner for either product. This means that these checks have to be done by us, the publishers. We need to identify the users' preferred language and the browser they're using. Using PHP that is pretty easy, however, my pages are cached and no longer dynamically served to most of my visitors!
So what to do?
Its a fairly simple trick: We do what Google does! We move our dynamic ad selection routine to a non cached PHP script and implement that via Javascript.
Let's say you want to serve German and English Firefox ads when a visitor uses Internet Explorer and Google Pack ads when he's already on Firefox.
Open a new PHP file and call it something like adsense.php. Of course your webserver needs to support PHP scripts.
First we have to identify the user's languages. This is easy, so start with
PHP:
Now we're going to check for German and for non Firefox users and add the according Google code to an array, because that's the most straight forward way of doing it by copy and paste ;-)
PHP:
-
...
-
// Firefox Ad in German:
-
$ad_stack[] = '<script type="text/javascript"><!--
-
google_ad_client = "pub-7003942742816714";
-
google_ad_width = 468;
-
google_ad_height = 60;
-
google_ad_format = "468x60_as_rimg";
-
google_cpa_choice = "CAAQ9aeTlwIaCIJLKBR7D8vWKL2093M";
-
google_ad_channel = "3294717074";
-
//--></script>
-
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
-
</script>';
-
} else {
-
// Google Pack Ad in German:
-
$ad_stack[] = '<script type="text/javascript"><!--
-
google_ad_client = "pub-7003942742816714";
-
google_ad_width = 468;
-
google_ad_height = 60;
-
google_ad_format = "468x60_as_rimg";
-
google_cpa_choice = "CAAQsMXDmwIaCJPp7vyZnLjIKPS4uosB";
-
google_ad_channel = "5743124350";
-
//--></script>
-
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
-
</script>';
-
}
Those were our German visitors. Lets just imagine that the rest will be OK with English ads. Of course (and that's what I have done) more language can be added. This is a very simple example. Let's add the English ads:
Note: Just for the joy of it we're going to serve Google Pack ads to IE users aswell. Using the array, we can simply add it to our stack of ads. That's why the Google Pack won't be in an else as it was for our German friends.
PHP:
-
...
-
} else {
-
// Firefox Ad in English
-
$ad_stack[] = '<script type="text/javascript"><!--
-
google_ad_client = "pub-7003942742816714";
-
google_ad_width = 468;
-
google_ad_height = 60;
-
google_ad_format = "468x60_as_rimg";
-
google_cpa_choice = "CAAQlYyYhAIaCNFy17maAhMqKOm293M";
-
google_ad_channel = "9242922888";
-
//--></script>
-
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
-
</script>';
-
}
-
// Google Pack Ad in English
-
$ad_stack[] = '<script type="text/javascript"><!--
-
google_ad_client = "pub-7003942742816714";
-
google_ad_width = 468;
-
google_ad_height = 60;
-
google_ad_format = "468x60_as_rimg";
-
google_cpa_choice = "CAAQ6OSVyAIaCG7XSvCMcOY1KPC34IEB";
-
google_ad_channel = "4235463274";
-
//--></script>
-
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
-
</script>';
-
}
We could stop here. But why not throw in some normal 468x60 banners too? Just add it to the stack!
After that we'll pick one ad out of the stack at random which will then be served after being properly formatted to our Javascript needs.
PHP:
-
...
-
// Add a normal image ad banner to the stack:
-
$ad_stack[] = '<script type="text/javascript">
-
<!--
-
google_ad_client = "pub-7003942742816714";
-
google_ad_width = 480;
-
google_ad_height = 60;
-
google_ad_format = "468x60_as";
-
google_ad_type = "image";
-
google_ad_channel = "0091379350";
-
google_color_border = "FFFFFF";
-
google_color_bg = "FFFFFF";
-
google_color_link = "191919";
-
google_color_text = "000000";
-
google_color_url = "008000";
-
//--></script>
-
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
-
</script>';
-
-
-
// Pick a random ad from the array:
-
-
// Show the properly formatted ad code so that it can be included in our static page by Javascript:
-
echo 'var googlead = "\\
-
';
-
echo str_replace("/script","\\/script",str_replace("\"","\\\"",str_replace("\n", "\\n\\\n", $ad_stack[$rand_keys])));
-
echo '\\n\\
-
"
-
';
-
-
?>
Save and upload to your webserver.
Finally add the Javascript code to your static page like this:
HTML:
That's it! You're done. Your static pages will now serve browser specific Google ads.

February 1st, 2007 at 8:30 pm
I hope you know that posting your pub id could lead to all sorts of trouble.. I suggest blanking out the pub id with XXX or something.
February 1st, 2007 at 9:05 pm
Thanks for your comment.
Pub-id's aren't a secret, hence the name. They have to be in every advertising page's html source code.
July 23rd, 2009 at 1:52 am
tks for the effort you put in here I appreciate it!
July 23rd, 2009 at 5:47 am
The best information i have found exactly here. Keep going Thank you
August 5th, 2009 at 4:05 am
Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Reader.
August 23rd, 2009 at 4:03 am
Naruto Shippuden Episode 32
October 9th, 2009 at 8:07 pm
Do you have to have the internet and can u use every thing that is on the phone. Like the iphone apps store.
________________
iphone 3g
February 27th, 2010 at 7:28 am
This is my first post I'd love to thank you for such a great quality site!
Was thinking this is a nice way to introduce myself!
If you want to compile property it is usually a wise conception to start a savings or investing game plan as early in life as imaginable. But don't worry if you have not thought of saving your capital until later on in life. With honest work, that is analyzing the best investment vehicles for your capital you can slowly but surely increase your riches so that it numbers to a big sum by the time you wish to retire. Look at all of the applicable asset classes from stocks to real estate as investments for your money. A researched and diversified portfolio of investments in different asset classes can help your money increase through the years.
-Ashlee Markgraf
currency exchange rates
February 27th, 2010 at 9:43 pm
Hi readers
Do not miss your chance to get a free iPhone 4G. Visit http://bit.ly/d9QOON
February 28th, 2010 at 7:37 am
As a Newbie, I am always searching online for articles that can help me. Thank you