0

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!

Max Di Campo
  • 408
  • 1
  • 4
  • 12
  • Put the URLs in to an array then choose one of those items randomly: https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array, then use `window.location.assign()` to redirect – Rory McCrossan Jan 26 '18 at 13:49
  • hey ok, sounds great, but my JS skills are pretty low. I have no idea, how to combine. Could you maybe give me a quick example, how this could look like? – Max Di Campo Jan 26 '18 at 13:55

1 Answers1

2

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"; 
}
EmilioPeJu
  • 361
  • 1
  • 5
  • ah maybe you got me wrong. 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 – Max Di Campo Jan 26 '18 at 13:58
  • Hey, I created this fiddle by trying to connect your code with click code, but it doesn't work. What is wrong? Here is my fiddle: https://jsfiddle.net/maxbln/kcok4yru/1/ – Max Di Campo Jan 26 '18 at 14:22
  • I believe jsfiddle doesn't allow redirections, I've tested it locally and it works – EmilioPeJu Jan 26 '18 at 14:33
  • Oh perfect, my fault. Thank you, I will test it! – Max Di Campo Jan 26 '18 at 14:39