Daily Ubuntu / Linux news and application reviews.

ads

I used an 468 x 60 Adsense banner ad under post titles and wanted to experiment with a large rectangle (336 x 280) to see which one performs better. But page impressions / clicks are not constant so how do you know if one ad or another is actually performing better? Well, you can display each ad on 50% of the pages, randomly and automatically, by using a simple JavaScript trick. The same JavaScript code can also display different ads so you can rotate ads from multiple services. Here is how:

This is the normal JavaScript for an 468x60 Adsense ad:

<script type="text/javascript"><!--
google_ad_client = "pub-5111779714716360";
google_ad_slot = "5012632955";
google_ad_host = "pub-1556223355139109";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Note that the google_ad_host isn't in the script you receive from Adsense and is automatically generated, so copy that from your website code before proceeding to implementing this.

OK, so to display 2 ads randomly, each on 50% of the pages, we are going to use a function called random() that generates a number between 0 and 1 with equal probability so that each ad is displayed the same amount of times. Here is my code for displaying 2 different Adsense ads using this random function:

<script type='text/javascript'>
var google_ads = Math.random();
if (google_ads < .5){
google_ad_width = 468;google_ad_height = 60;
google_ad_client = "pub-5111779714716360";google_ad_host = "pub-1556223355139109";google_ad_slot = "6221737480";
} else {
google_ad_width = 336;google_ad_height = 280;
google_ad_client = "pub-5111779714716360";google_ad_host = "pub-1556223355139109";google_ad_slot = "5019549445";
}

</script>
<script src='http://pagead2.googlesyndication.com/pagead/show_ads.js' type='text/javascript'>
</script>

Modify the google_ad_width, google_ad_height, google_ad_client, pub-5111779714716360, google_ad_host and google_ad_slot with your values for each of the ads.

Add a channel for each ad so you'll be able to see which one is actually getting more clicks.

Basically this can be used for 3 purposes:

-rotating different ad sizes from Adsense
-alternating ads from different ad networks
-displaying ads from 2 different persons on the same website

And all the ads will be displayed an equal number of times.

NOTE for Blogger blogs: to insert Adsense code into your template (not widget), for the ad to actually show up, you need to convert it using something like this: Blogger Ad Code Converter.

Worried that this is against AdSense program policies, read THIS. Which is actually from where I found out about the random() trick. It's on the Adsense blog ;)
«
Next
Newer Post
»
Previous
Older Post