I have problem when i want to rotate ads in blog hosted at blogger.com . But finally, i can do it.
Thank for the article from http://www.earn-web-cash.comThe solution, it turns out, is very simple.
Why Randomize Ads?
There are a few reasons you might want to do this.
If you’re looking into changing the theme of your AdSense ad, you may want to test the two themes against each other. By giving each theme approximately 50% of the impressions for a given time period, you can make some direct comparisons between their performance.
Or maybe you and a friend both run a website, so you want to split the advertising time between two AdSense publisher ids. That can be done too.
How the AdSense Code Works
Before we randomize it, let’s take a look at how the AdSense code works.
Here’s the snippet of code that creates the skyscraper on the left side of this page.
<script type="text/javascript"><!--
google_ad_client = "pub-2399151883698113";
/* Web Cash Tall Skyscraper */
google_ad_slot = "8702750734";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
You’ll notice two major pieces to this code. The first piece - in the first <script> element - defines the ad unit’s properties. It sets google_ad_client (your publisher id), google_ad_slot (the saved ad unit’s id with color info), google_ad_width, and google_ad_height.
The second section - the script element with src “show_ads.js” - actually displays the ad based on the variables that were set previously.
In order to vary the type of ad shown, we need to come up with a conditional statement to vary the google_ad_client/slot/width/height settings.
The Javascript Code…
And here’s the moment you’ve all been waiting for - the Javascript code to randomize which ad is shown.
<script type="text/javascript">
<!--
if (Math.random() > 0.5) {
google_ad_client = "pub-2399151883698113";
google_ad_slot = "5396010225";
google_ad_width = 250;
google_ad_height = 250;
} else {
google_ad_client = "pub-2399151883698113";
google_ad_slot = "3900536874";
google_ad_width = 250;
google_ad_height = 250;
}
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
You’ll notice that there’s an if/else statement, and each branch of the conditional statement defines a different ad to display. The results of that conditional statement rely on the outcome of Math.random().
(Math.floor( Math.rand() * setSize)) + setOffset;
Math.random() generates a random value between 0 and 1. In other words, it gives you a probability. By checking that Math.random() is greater than 0.5, the statement should evaluate to true approximately 50% of the time.
We could also make the rotation more lop-sided. For example (Math.random() > 0.70) would be true 30% of the time. Therefore the first ad would be displayed 30% of the time, while the second ad would be displayed 70% of the time.
No comments:
Post a Comment