Reputation: 4050
I'm trying to redirect 10% of my users to a beta site we are testing. I am using the Codeigniter framework and I have added the following to the routes.php file:
$absplit = 0.1;
if((mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() < $absplit))
{
header('location: '.str_replace($_SERVER[HTTP_HOST],"x.example.com","http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"),true,302);
exit;
}
However, I'm noticing that it's redirecting nearly 40% of the traffic and seems to redirect in chunks of time (e.g. all users for 10 minutes go to one site). Can anyone spot the problem?
Upvotes: 1
Views: 1411
Reputation: 7686
If you don't want to change the backend code you can just write a JavaScript that does the redirect. For example,
if (Math.random() <= 0.1) {
location.href = '/url-to-redirect-to';
}
If you want the users to have consistent experience. You can save some value to the cookie which will indicate that they have or have not to be redirected.
var cookieValue = cookie.get(key);
if (cookieValue === 'redirect' || !cookieValue && Math.random() <= 0.1) {
cookie.set(key, 'redirect');
location.href = '/url-to-redirect-to';
} else {
cookie.set(key, 'no-redirect');
}
This is how essentialy front-end A/B testing works. Here you can see a more complex example of traffic allocation between many experiments with different percentages of traffic and assignment conditions.
However, in your case a simple if will be enough.
Upvotes: 0
Reputation: 5428
Create a table with one field of type int. In your main controllers index function, increment that value by 1. Then check it's value, if it is >= to 10 then use a redirect() call and set the database value back to 0.
Upvotes: 0