<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Cash &#187; ad</title>
	<atom:link href="http://www.earn-web-cash.com/tag/ad/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Randomize AdSense Ads with Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 23:45:30 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Advertising]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[ad]]></category>
		<category><![CDATA[AdSense]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</guid>
		<description><![CDATA[I came across this problem on the forums today. How can you randomize which Ad Sense ad to show using Javascript? The solution, it turns out, is very simple. Why Randomize Ads? There are a few reasons you might want to do this. If you&#8217;re looking into changing the theme of your AdSense ad, you [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the forums today.  How can you randomize which Ad Sense ad to show using Javascript?</p>
<p>The solution, it turns out, is very simple.<br />
<span id="more-164"></span></p>
<h4>Why Randomize Ads?</h4>
<p>There are a few reasons you might want to do this.</p>
<p>If you&#8217;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.</p>
<p>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.</p>
<h4>How the AdSense Code Works</h4>
<p>Before we randomize it, let&#8217;s take a look at how the AdSense code works.</p>
<p>Here&#8217;s the snippet of code that creates the skyscraper on the left side of this page.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">google_ad_client = &quot;pub-2399151883698113&quot;;</span>
<span style="color: #808080; font-style: italic;">/* Web Cash Tall Skyscraper */</span>
<span style="color: #808080; font-style: italic;">google_ad_slot = &quot;8702750734&quot;;</span>
<span style="color: #808080; font-style: italic;">google_ad_width = 160;</span>
<span style="color: #808080; font-style: italic;">google_ad_height = 600;</span>
<span style="color: #808080; font-style: italic;">//--&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>You&#8217;ll notice two major pieces to this code.  The first piece &#8211; in the first <code>&lt;script&gt;</code> element &#8211; defines the ad unit&#8217;s properties.  It sets google_ad_client (your publisher id), google_ad_slot (the saved ad unit&#8217;s id with color info), google_ad_width, and google_ad_height.</p>
<p>The second section &#8211; the script element with src &#8220;show_ads.js&#8221; &#8211; actually displays the ad based on the variables that were set previously.</p>
<p>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.</p>
<h4>The Javascript Code&#8230;</h4>
<p>And here&#8217;s the moment you&#8217;ve all been waiting for &#8211; the Javascript code to randomize which ad is shown.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5396010225&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3900536874&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//--&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
&lt;/script&gt;</pre></div></div>

<p>You&#8217;ll notice that there&#8217;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().</p>
<p>Previously, we built a <a href="http://www.earn-web-cash.com/2008/02/24/random-number-function">function to get a useful random number out of Math.random()</a>.  This is actually a situation where we can use the value from Math.random() just the way it is.</p>
<p>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.</p>
<p>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.</p>
<p>I hope you weren&#8217;t looking for something more complicated because, well, it isn&#8217;t.  It&#8217;s just that simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

