Categories: I have an active account: Working with AdSense :

Having trouble with the new A/B testing option for Adsense

Showing 1-11 of 11 messages
Having trouble with the new A/B testing option for Adsense Shea W 11/17/13 3:56 PM
I've decided to try the new A/B testing for Adsense and I'm having a problem.  When I set it up, I end up with both ads displaying at the same time with this tag "} else {" showing between the two ads.

As the instructions indicated, I am replacing "//your first ad unit code goes here" with the ad code.  I'm sure I must be making a simple mistake.

This is the JavaScript template that I am using:

<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>

Can someone kindly point out what I may be doing wrong?

Thank you.

Re: Having trouble with the new A/B testing option for Adsense galeksic 11/17/13 4:19 PM
Maybe because you shouldn't paste the entire ad code in if-else blocks, just some of google_ad_* variables, usually only google_ad_slot :)

<script type="text/javascript">
   
var random_number = Math.random();

    google_ad_client
= "ca-publisher-id";
    google_ad_width
= 728;
    google_ad_height
= 90;

    if (random_number < .5){
        google_ad_slot
= "1234567890"; // <- this is actually first ad unit code, because google_ad_width google_ad_height & google_ad_client are identical for both our units here
   
} else {
        google_ad_slot
= "2345678901"; // <- this is the second unit. width, height and pub ID are the same as for the first unit, so we are just swithing google_ad_slot
   
}

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

Modification of the AdSense ad code

BTW - perhaps you shouldn't bother with A/B testing, because since recently you have Experiments in your account. Try them :)

About experiments
Re: Having trouble with the new A/B testing option for Adsense Shea W 11/19/13 11:33 AM
Thanks for the response galeksic, but I'm still a bit confused.  I'm aware of the Experiments in Adsense now and I'm excited about that.  However, you can only make changes to the same sized ad unit.

I'm interested in this article from Inside Adsense: http://adsense.blogspot.com/2006/08/abcs-of-ab-testing.html that shows how you can test with different ad sizes. I'm using asynchronous ad code.  

I believe you are saying not to place the entire code in the spot "//your first ad unit code goes here" and "//your second ad unit code goes here".  I'm not sure what I need to remove.

This is what I did that caused both ads to display at the same time with the "} else {" tag/code showing in between:

          <script type="text/javascript">
    var random_number = Math.random();
    if (random_number < .5){
        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Below page only -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-xxxxxxxxxxxx"
     data-ad-slot="7107484836"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
    } else {
        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 300 x 600 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:600px"
     data-ad-client="ca-pub-xxxxxxxxxxxx"
     data-ad-slot="6595683635"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

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

What is it that I need to modify to make it work as well as be compliant with Adsense?

Thank you.

Re: Having trouble with the new A/B testing option for Adsense galeksic 11/19/13 12:37 PM
I think I would rather use sync AdSense code:

<script type="text/javascript">
   
var random_number = Math.random();

    google_ad_client
= "ca-pub-xxxxxxxxxxxx";
   
if (random_number < .5){
        google_ad_slot
= "7107484836";
        google_ad_width
= 336;
        google_ad_height
= 280;
   
} else {
        google_ad_slot
= "6595683635";
        google_ad_width
= 300;
        google_ad_height
= 600;
   
}
</script>
<script type="text/javascript"
   
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Do you need async for A/B testing?
Re: Having trouble with the new A/B testing option for Adsense Shea W 11/19/13 1:44 PM
Thanks galeksic, I got it working! I think my problem was from this line:

"When using this template, remember to replace “//your first ad unit goes here” with your ad code inside the first set of <script></script> tags."  

I was thinking it was the script tags for the template and not the script tags of the actual ad code.... oh well.

I don't have to use async, I can use sync as well.  I can always go back to async after I conclude my tests.  

Thanks!
Re: Having trouble with the new A/B testing option for Adsense w3webpro 2/11/14 5:15 AM
Shea W, I am having the same issue. Can you post what worked for you in relation to the original source code you posted?
w3webpro 2/11/14 5:16 AM <This message has been deleted.>
Re: Having trouble with the new A/B testing option for Adsense Shea W 2/11/14 12:29 PM
Here is the source code to use for the split test:

<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>
 
What I did was insert the section of ad code that is between the <script> </script> tags from my own ad into the spot that says "//your first ad unit code goes here".

This is the standard synchronous ad code for example:

<script type="text/javascript"><!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxxxxxx";
/* Below Content 336 x 280 */
google_ad_slot = "xxxxxxxxxxxx";


google_ad_width = 336;
google_ad_height = 280;
//-->

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

Only use this part of the code:

<!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxxxxxx";
/* Below Content 336 x 280 */
google_ad_slot = "xxxxxxxxxxxx";


google_ad_width = 336;
google_ad_height = 280;
//-->

And insert it like this:

<script type="text/javascript">
    var random_number = Math.random();
    if (random_number < .5){
        <!--
google_ad_client = "ca-pub-xxxxxxxxxxxxxxxxxx";
/* Below Content 336 x 280 */
google_ad_slot = "xxxxxxxxxxxx";

google_ad_width = 336;
google_ad_height = 280;
//-->

    } else {


        //your second ad unit code goes here
    }
</script>
<script type="text/javascript"     src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

Your second ad code (the different one you are using for the test) would be inserted in the same manner in the part that says "//your second ad unit code goes here"

I hope that helps,
 
Shea

Re: Having trouble with the new A/B testing option for Adsense w3webpro 2/11/14 10:36 PM
Thanks for clearing that up Shea. That worked for me.

Although, I did realize that I was using async instead of sync and that's what confused me in the beginning because the ad code is a little different.

Cheers!

Re: Having trouble with the new A/B testing option for Adsense w3webpro 2/11/14 10:51 PM
galeksic, can you show an example of ASYNC for A/B testing?
Re: Having trouble with the new A/B testing option for Adsense galeksic 2/12/14 1:03 AM
There is already official example on "Modifications" page:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
   
style="display:inline-block;width:728px;height:90px"
   
data-ad-client="ca-publisher-id">
</ins>
<script>
   
if (Math.random() < .5) {
      mySlotId
= '1234567890';
   
} else {
      mySlotId
= '2345678901';
   
}
   
(adsbygoogle = window.adsbygoogle || []).push({
      params
: { google_ad_slot: mySlotId }
   
});
</script>

Modification of the AdSense ad code

Could you say more about your specific issue? Maybe you don't even need to use A/B testing, maybe you could use Experiments?

About experiments