Welcome to WebmasterWorld Guest from 54.197.171.28

Forum Moderators: open

Message Too Old, No Replies

playing around with adsense split testing code and html tags

   
6:57 am on Jun 13, 2011 (gmt 0)

5+ Year Member



I'm trying to do some A/B testing using google's own javascript code:

<script type="text/javascript">
var random_number = Math.random();
if (random_number < .5){
//your first ad unit code goes here
} else {
//your second ad unit code goes here
}
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>


pretty simple to add the adsense code into that, but I want to go one step further and float one ad to the left and leave the other unfloated. I've tried everything to get my

<div style="float:left;"></div>


into this javascript and nothing works. This was the closest I got, but the result was the opening and closing tags appearing before the ad itself.


<script type="text/javascript">
var random_number = Math.random();
if (random_number < .5){
document.write('<div style="float:left;">')
<!--
google_ad_client = "ca-pub-#*$!";
/* floated left */
google_ad_slot = "#*$!";
google_ad_width = #*$!;
google_ad_height = #*$!;
//-->
document.write('</div>')
} else {
document.write('<div="margin-left:10px;">')
<!--
google_ad_client = "ca-pub-#*$!";
/* non-floated */
google_ad_slot = "#*$!";
google_ad_width = #*$!;
google_ad_height = #*$!;
//-->
document.write('</div>')
}
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


is there something simple I'm overlooking?

added: I did sort of solve it by putting one single close /div tag outside of the whole thing, thus solving the issue of the two tags appearing together followed by the ad. There must be a cleaner way than that?
9:58 am on Jun 24, 2011 (gmt 0)

10+ Year Member



A cleaner way to do it:
- wrap the ad code in a div with a specified id
- add one or another classname to that div using JS depending on the outcome of the random number
- use the classnames to position the ads appropriately

A sample:

<div id="ad-holder">
<script type="text/javascript">
var random = 0 | Math.random() * 10; // it's a way of getting a random number between 0 and 10
var adHolder = document.getElementById("ad-holder");
if(random > 5){
adHolder.className = "ad-a";
/* your A ad code here */
} else {
adHolder.className = "ad-b";
/* your B ad code here */
}
</script>
</div>


Hope it helps,
M.