-4

I have in one case 2 links and in other 3 links. Need to find out what is the best. My plan is split test them all. So I want make this:

  • 50% - document.write('link1');
  • 50% - document.write('link2');

So 50% visitors will see the first link and other 50% the second.

How can I do this in poor javasript?

Mike Poole
  • 1,582
  • 3
  • 21
  • 36
mrdeath4
  • 147
  • 1
  • 13
  • 1
    I think your question needs a *lot* more context. –  Nov 02 '18 at 14:42
  • why do you think so? – mrdeath4 Nov 02 '18 at 14:44
  • 2
    Because it contains none. We have no idea of your environment. Are you using NodeJS? Some other server-side system? Are you using any frameworks? Are you intending this code to run in the browser? There's no point to split testing unless you gather statistics. How do you intend to gather that data? What system? We have no idea. –  Nov 02 '18 at 14:47
  • I don't have a question about data gathering. Just about the javasrcipt realization. PS. I gather with a tracking system – mrdeath4 Nov 02 '18 at 14:49
  • Using `document.write` sounds like a bad idea. But what is your actual question? Do you want to know how to generate random numbers in JS? – melpomene Nov 02 '18 at 14:50
  • yes - what is the best solution? just random from 1 and 2 ?and why doc.write is a bad idea? – mrdeath4 Nov 02 '18 at 14:51
  • https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – melpomene Nov 02 '18 at 14:58
  • 1
    @mrdeath4 Contextual information is important because it helps us provide you with better answers. Not providing context is not helpful and only works against you. We know you are not asking about data gathering, but each of the questions I asked are pretty basic and influence the answers you receive. –  Nov 02 '18 at 15:37

1 Answers1

1

You could do something like this :

function randomLinks() { 
    if (Math.random() > 0.5) {
        //A-test
        document.write('link1');
    }
    else {
        //B-test
        document.write('link2');
    }
} 

Maybe add some click gathering system or just logs the views on link1 and link2 while logging the Referer.

Using document.write is a bad practice, it can still works in your case, it's up to you. You can use JQuery for easy DOM Manipulation for example :

JQuery Dom Manipulation Examples

xalo
  • 235
  • 1
  • 9