2

I am collecting some sample data on my site and for now just want to collect data from say 10% of my visitors using a javascript function.

One way I can think of is to pick a number between 1 to 10 randomly and if its 10 then call the JS function to collect data else do nothing.

Also the data set need not be exactly from the 10% of users but a approximate number would also do for me.

I am Looking for suggestions on an alternate or better way to accomplish this.

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Rols
  • 153
  • 11
  • 2
    If it's client-side only, then I see no better way then just doing a "random 10%". It also seems to meet the "not exactly 10%" requirement ;-) Go for it. –  Sep 07 '11 at 03:23
  • Err, actually over time it *will be 10% of visits, not visitors* if just using `random` -- just not "every 10th". Cookies could be used to limit it to *visitors*. –  Sep 07 '11 at 03:32

2 Answers2

2

You could keep a server side counter (in a DB for example) that checks before it delivers the page - if the counter is divisible by 10 then it includes the Javascript in the template, otherwise, the Javascript is omitted.

Of course, this is a very generic answer - how easy this would be to implement completely depends on your knowledge of server side scripting and the framework of your site.

mal-wan
  • 4,391
  • 4
  • 26
  • 37
  • Of course, if you were to go this route, you would want to make sure to give the user a cookie or session data which prevents the same user from getting the function twice and stops the counter from going up on repeated visits from the same user. This method will ensure every tenth visitor will get the function. – Yoshiyahu Sep 07 '11 at 03:36
0

I would do something like so. Meets the requirement for not exactly 10% as well. If you needed more exact results however this would definitely be something better done on the server side.

Live Demo

if(Math.random() * 100 >90){
   // greater than 90, so theoretically should only happen 1 out of 10 times do your stuff.  
}else{
    // Do nothing
}
Loktar
  • 34,764
  • 7
  • 90
  • 104