A/B Testing Javascript


This is the A/B testing javascript that was published by Google Adsense team in their blog. I find it quite useful for testing other than Adsense ad units. I saved it here for my easy access in case I need it. Who knows the post in their blog might get deleted in future.

<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>
There is another example found at the Google Adsense Help page and the code looks like this:

Synchronous ad code:

<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";
    } else {
        google_ad_slot = "2345678901";
    }
</script>
<script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Asynchronous ad code:

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


No comments :

Post a Comment

Thank you for visiting Almost a Technocrat. Due to many spam comments, your comment will be moderated.