Guest User

AB(CD) Algorithm testing Wordpress

a guest
Sep 20th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. <?php
  2.     function abtest($name, $A , $B , $C = false , $D = false ) {
  3.         session_start();
  4.        
  5.         $array_options = array();
  6.         $array_options['A']= $A;
  7.         $array_options['B']= $B;
  8.         if($C != false) {
  9.             $array_options['C']= $C;
  10.         }
  11.         if($D != false) {
  12.             $array_options['D']= $D;
  13.         }
  14.         if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
  15.             return $array_options[$A]; // if bot then don't allow data sqew - returns first value to allow rendering / page function - usage & goal data not stored. As A is often the original method testing it means the original page is served up.
  16.         } else {
  17.             $data = get_post_meta(1 , 'ABtest-' . $name , true);
  18.            
  19.             if(empty($data)) {
  20.                 $data = array();
  21.                 foreach($array_options as $key => $option) {
  22.                     $data[$key]= array( 'served' => '1' , 'goals' => '1');
  23.                 }
  24.             }
  25.            
  26.             if(mt_rand(0,99) <= 23) { //  percent of random serving. In practice found with mt_rand that around 20-25% is a good amount to help ofsetting the nature of "randomness", ensuring a decent enough mix is served up for actually testing the different options.
  27.                 $serve = array_rand($array_options);
  28.                
  29.                 $_SESSION['ABtest-' . $name] = $serve;
  30.                
  31.             } else {
  32.                 $best = 0;
  33.                 foreach ($data as $key => $option) {
  34.                     $Shown = $option['served'];
  35.                     $Goals = $option['goals'];
  36.                    
  37.                     $curr = $Goals / $Shown;
  38.                     if($curr > $best) {
  39.                         $serve = $key;
  40.                         $best = $curr;
  41.                     }
  42.                 }
  43.             }
  44.             $data[$serve]['served']++;
  45.            
  46.             update_post_meta(1 , 'ABtest-' . $name , $data);
  47.            
  48.             $_SESSION['ABtest-' . $name] = $serve;
  49.            
  50.             return $array_options[$serve];
  51.         }
  52.     }
  53.    
  54.    
  55.     function abtestGoalHit($name , $value) {
  56.         if(!empty($_SESSION['ABtest-' . $name])) {
  57.             $Served = $_SESSION['ABtest-' . $name];
  58.             $data = get_post_meta(1 , 'ABtest-' . $name , true);
  59.             $data[$Served]['goals'] = $data[$Served]['goals'] + $value;
  60.             update_post_meta(1 , 'ABtest-' . $name , $data);
  61.         }
  62.     }
  63.    
  64.    
  65.     function resetABtest($name) {
  66.         $metakey = 'ABtest-' . $name;
  67.        
  68.         return delete_post_meta(1, $metakey);
  69.     }
  70.    
  71.    
  72.     ########
  73.    
  74.     $orange = 'orange';
  75.     $apple = 'apple';
  76.     $pear = 'pear';
  77.     $straw = 'strawberry';
  78.    
  79.     if(isset($_POST['conversion'])) {
  80.         abtestGoalHit('fruitchoice' , 1);
  81.     } else if(isset($_POST['conversion2'])) {
  82.         abtestGoalHit('fruitchoice' , 2);
  83.     } else if(isset($_POST['clear'])) {
  84.         delete_post_meta(1 , 'ABtest-fruitchoice');
  85.         unset($_SESSION['ABtest-fruitchoice']);
  86.         die;
  87.     }
  88.     ?>
  89.  
  90. <h1>My fave fruit...</h1>
  91.  
  92. <p>I love <?php echo abtest('fruitchoice' , $orange , $apple , $pear , $straw); ?> the most</p>
  93.  
  94. <form method="post">
  95. <input name="conversion" type="submit" value="I agree!" />
  96. <input name="conversion2" type="submit" value="I couldn't agree more" />
  97. <input name="clear" type="submit" value="Clear!" />
  98. </form>
  99.  
  100. <a href="">No / Bounce</a>
Add Comment
Please, Sign In to add comment