Example #1
0
 /**
  * Record a pageview on a particular test and variation.
  *
  * @access  public
  * @param   string  $test_id
  * @param   array   $variation_id
  * @return  mixed
  */
 public function addPageview($test_id, $variation_id)
 {
     $this->getClient();
     // transaction timestamp
     $now = new \DateTime('now', new \DateTimeZone('UTC'));
     $now = $now->format('U');
     // begin a transaction
     $responses = $this->client->multiExec(function ($tx) use($test_id, $variation_id) {
         // increment the object hash counts
         $this->client->hincrby('test:' . $test_id, 'pageviews', 1);
         $this->client->hincrby('variation:' . $variation_id, 'pageviews', 1);
         // increment the sorted set counts for pageview rankings
         $testPageviews = $this->client->zincrby('tests:sorted_by_views', 1, $test_id);
         $variationPageviews = $this->client->zincrby('variations:sorted_by_views', 1, $variation_id);
         // retrieve the hash wins
         $variationWins = $this->client->hget('variation:' . $variation_id, 'wins');
         // calculate ranking change
         $rank = 0.0;
         if ($variationPageviews > 0) {
             $rank = $variationWins / $variationPageviews;
         }
         // update the variation rankings
         $this->client->zadd('variations:sorted_by_rank', $rank, $variation_id);
     });
 }