rAthus

[JavaScript] AB testing

May 23rd, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** AB testing **/
  2. jQuery('.ab-testing > *').css({'display':'none'});
  3. jQuery('.ab-testing').each(function() {
  4.     var $els = jQuery(this).find('>*');
  5.     var slots = [];
  6.     var i = -1;
  7.     $els.each(function() {
  8.         i++;
  9.         var nbSlots = (jQuery(this).attr('slots')||1)*1;
  10.         for (var j=0; j<nbSlots; j++)
  11.             slots.push(i);
  12.     });
  13.     var iSlot = Math.floor(Math.random()*(slots.length||0));
  14.     var iFinal = slots[iSlot];
  15.     jQuery(this).find('>*:eq('+iFinal+')').css({'display':''}).addClass('ab-ok');
  16.     jQuery(this).find('>*:not(.ab-ok)').remove();
  17. });
Add Comment
Please, Sign In to add comment