Support

Account

Home Forums Add-ons Repeater Field Use repeater field for percentage based A/B testing Reply To: Use repeater field for percentage based A/B testing

  • Sorry for the late reply on this. Like I said you would need to do away with the rand part.

    I would create 2 more fields, one to hold a number between 1 and 100 for the % of time to show something and another to hold the number of times it is shown.

    
    <?php 
      // get_the_options
      $options = get_field('after_options', 'option');
      // this will be set to index to show
      $show_index = false;
      // get totals
      $shown_total = 0;
      foreach ($options as $index => $row) {
        $shown_total = $row['times_shown'];
      }
      // find the first one shown less times than its % to be shown
      foreach ($options as $index => $row) {
        $percent = 0;
        if ($shown_total > 0) {
          // prevent divide by 0
          $percent = round($row['times_shown']/$shown_total, 2)*100; 
        }
        // if this one is < % for this one to be shown then pick it and exit loop
        if ($percent < $row['percent_to_show']) {
          $show_index = $index;
          break;
        }
      }
      // if all rows have been shown the % it should be shown or more
      // $show_index will be false
      // pick a random one to show
      if (!$show_index) 
        $show_index = array_rand($options);
      }
      // update the number of times this row has been shown
      // and update the row
      $row = $options[$show_index];
      $row['times_shown']++;
      update_row('after_options', $show_index, $row, 'option');
      // get the value to show
      $signupHTML = $row['html'];