Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Simple A/B Testing Script?

    Hi,

    I'm looking for a simple script that will redirect exactly 50% of my visitors to one page and 50% to another. I tried a random script but it wasn't doing this evenly. Is there any way of doing this easily with javascript without the need for a database?

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi denhamd2, you cannot redirect exactly 50% using JS, because to do that you need some server side testing for example using database.. something like.. check the last direction... side A? ok.. you will be redirected to site B.. and store last redirection, but if you want to use just JS, you can check for example actual time / seconds and test if that number is odd.

    Like this:

    Code:
    <script>
    var date = new Date();
    if (date.getSeconds() % 2 == 0)
    document.location = 'http://www.yahoo.com/'; else
    document.location = 'http://www.google.com/';
    </script>
    PHP (better way)

    Code:
    <?php
     if (date('s')%2 == 0) 
     header('Location: http://www.yahoo.com'); else
     header('Location: http://www.google.com');
    ?>
    Last edited by smalldog; 05-18-2007 at 12:01 PM.
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm going to go with javascript, however i notice it doesn't give a 50/50 split, ie attempt 1 it went to google, attempt 2 it went to google, attempt 3 it went to yahoo. i need for instance a counter where all the even numbers go to google and all the odd numbers go to yahoo, that would be the most accurate way i could think of. therefore, is there a way i could maybe have a counter stored in a text file where it increments for every visit and all the odd numbered hits go to one page and all the even numbered hits go to another. would anyone know how to do this in javascript?
    Last edited by denhamd2; 05-18-2007 at 12:49 PM.

  • #4
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I said this method is not exactly 50/50, however it's better than generate a random number I think. I said you must use PHP and MySQL or PHP and server file to write a last direction, you cannot do that using javascript.
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #5
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok, i know this is now in the wrong forum but do you know how i would have this counter text file in php and the redirector code?

  • #6
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can try following code.. first of all you must create an empty text file myfile.txt and upload it with this script to your server..

    Code:
    <?php
     $myFile = "myfile.txt";
     $fh = fopen($myFile, 'r+') or die("can't open file");
     $last = fgets($fh);
     rewind($fh);
     fwrite($fh, ($last=='1'?'2':'1'));
     fclose($fh);
     if ($last == '1') 
     header('Location: http://www.yahoo.com'); else
     header('Location: http://www.google.com');
    ?>
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #7
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks is this possible with javascript instead of php?

  • #8
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •