Support

Account

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

Solving

Use repeater field for percentage based A/B testing

  • I am currently using the ACF repeater field for A/B testing and am using several different text options for a newsletter signup form on my client’s website. And then I am tracking each of the various options and their success using Google Analytics. This all works great.

    However, I am wondering if there are any recommendations of how I could add another field to each repeater field/option where I could add in a percentage so that the options would be displayed on the site based on the percentage (right now it is just randomized). So, for example, if I had three options and one should be shown 80% of time on the site and then 10% and 10% for the other two.

    It seems like I would have to somehow write to the database every time the repeater field was shown on the site so that I could log which option was shown and then check against that to determine which one to show so the percentage based usage could be adhered to.

    Does that seem correct? Any recommendations would be appreciated.

  • You would need to have at least 2 other fields, or ways to store the information. You would need a field to set % of time to show a row and you would need some way to store how many times that row was shown and this field would need to be updated every time that row was shown.

    Code your currently using would be helpful for someone that wanted to help you figure it out.

  • No problem.

    Below is the code I am using to grab a random selection from a repeater field (‘after_options’).

    <?php 
    	$options = get_field( 'after_options', 'option' );
    	if( is_array( $options ) ) {
    		$option = array_rand( $options ); 
    		$signupHTML = $options[$option]['html'];
    	} 
    ?>
    <div class="signupAfter">
    	<div class="signupText">
    		<?php echo $signupHTML; ?>
    	</div>
    	<div class="signupForm">
    		<input type="text" name="fname" class="fname" placeholder="First">
    		<input type="text" name="lname" class="lname" placeholder="Last">
    		<input type="text" name="email" class="email" placeholder="Email">		 
                    <span class="button">Subscribe</span>
    	</div>
    </div>
  • 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'];
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.