Search code examples
javascriptjqueryhttp-redirectab-testing

Random redirect like an A/B test


I want to test a part of my website with an A/B test. For this I want to redirect users to one of two different URLs randomly, by clicking a button.

The user is on e.g. /register and now I want to randomly sent him to /success-v1 or /success-v2 by clicking the "register now" button on /register

Here is the HTML

<div class="button" id="testbutton">
  Go for it
</div>

Here the JS:

$(document).ready(function(){
    $('#testbutton').click(function(){
        if(Math.random() > 0.5) { 
            window.location.href = "http://host/versionA";
            } else {
            window.location.href = "http://host/versionB"; 
            }
    });
});

But somehow, it's not working :/

Thank you!


Solution

  • This could be in a parent page:

    if(Math.random() > 0.5) { 
        window.location.href = "http://host/versionA"; 
    } else {
        window.location.href = "http://host/versionB"; 
    }