...

View Full Version : Simple A/B Testing Script?



denhamd2
05-18-2007, 11:29 AM
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?

smalldog
05-18-2007, 11:52 AM
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:



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



<?php
if (date('s')%2 == 0)
header('Location: http://www.yahoo.com'); else
header('Location: http://www.google.com');
?>

denhamd2
05-18-2007, 12:43 PM
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?

smalldog
05-18-2007, 01:27 PM
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.

denhamd2
05-18-2007, 02:33 PM
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?

smalldog
05-18-2007, 03:30 PM
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..



<?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');
?>

denhamd2
05-18-2007, 03:46 PM
thanks :) is this possible with javascript instead of php?

smalldog
05-18-2007, 03:57 PM
no :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum