Example #1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $experiments = Experiment::active()->get();
     $goals = array_unique(Goal::active()->orderBy('name')->lists('name'));
     $columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals));
     $writer = new Writer(new SplTempFileObject());
     $writer->insertOne($columns);
     foreach ($experiments as $experiment) {
         $engagement = $experiment->visitors ? $experiment->engagement / $experiment->visitors * 100 : 0;
         $row = [$experiment->name, $experiment->visitors, number_format($engagement, 2) . " % (" . $experiment->engagement . ")"];
         $results = $experiment->goals()->lists('count', 'name');
         foreach ($goals as $column) {
             $count = array_get($results, $column, 0);
             $percentage = $experiment->visitors ? $count / $experiment->visitors * 100 : 0;
             $row[] = number_format($percentage, 2) . " % ({$count})";
         }
         $writer->insertOne($row);
     }
     $output = (string) $writer;
     if ($file = $this->argument('file')) {
         $this->info("Creating {$file}");
         File::put($file, $output);
     } else {
         $this->line($output);
     }
 }
Example #2
0
 public function testExport()
 {
     Artisan::call('ab:install');
     Experiment::find('a')->update(['visitors' => 153, 'engagement' => 35]);
     Goal::create(['name' => 'foo', 'experiment' => 'a', 'count' => 42]);
     $output = new Symfony\Component\Console\Output\BufferedOutput();
     Artisan::call('ab:export', [], $output);
     $report = $output->fetch();
     $this->assertContains('Foo', $report);
     $this->assertContains('153', $report);
     $this->assertContains('35', $report);
     $this->assertContains('42', $report);
     $output = new Symfony\Component\Console\Output\BufferedOutput();
     Artisan::call('ab:export', ['file' => '/tmp/test.csv'], $output);
     $report = $output->fetch();
     $this->assertContains('Creating /tmp/test.csv', $report);
 }
Example #3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $experiments = Experiment::active()->get();
     $goals = array_unique(Goal::active()->orderBy('name')->lists('name')->toArray());
     $columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals));
     $table = new Table($this->output);
     $table->setHeaders($columns);
     foreach ($experiments as $experiment) {
         $engagement = $experiment->visitors ? $experiment->engagement / $experiment->visitors * 100 : 0;
         $row = [$experiment->name, $experiment->visitors, number_format($engagement, 2) . " % (" . $experiment->engagement . ")"];
         $results = $experiment->goals()->lists('count', 'name');
         foreach ($goals as $column) {
             $count = array_get($results, $column, 0);
             $percentage = $experiment->visitors ? $count / $experiment->visitors * 100 : 0;
             $row[] = number_format($percentage, 2) . " % ({$count})";
         }
         $table->addRow($row);
     }
     $table->render();
 }
Example #4
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $connection = Config::get('ab::connection');
     // Create experiments table.
     if (!Schema::connection($connection)->hasTable('experiments')) {
         Schema::connection($connection)->create('experiments', function ($table) {
             $table->increments('id');
             $table->string('name');
             $table->integer('visitors')->unsigned()->default(0);
             $table->integer('engagement')->unsigned()->default(0);
             $table->timestamps();
         });
     }
     // Create goals table.
     if (!Schema::connection($connection)->hasTable('goals')) {
         Schema::connection($connection)->create('goals', function ($table) {
             $table->increments('id');
             $table->string('name');
             $table->string('experiment');
             $table->integer('count')->unsigned()->default(0);
             $table->timestamps();
         });
     }
     $this->info('Database schema initialized.');
     $experiments = Config::get('ab')['experiments'];
     if (!$experiments or empty($experiments)) {
         return $this->error('No experiments configured.');
     }
     $goals = Config::get('ab')['goals'];
     if (!$goals or empty($goals)) {
         return $this->error('No goals configured.');
     }
     // Populate experiments and goals.
     foreach ($experiments as $experiment) {
         Experiment::firstOrCreate(['name' => $experiment]);
         foreach ($goals as $goal) {
             Goal::firstOrCreate(['name' => $goal, 'experiment' => $experiment]);
         }
     }
     $this->info('Added ' . count($experiments) . ' experiments.');
 }
Example #5
0
 /**
  * Check if there are active experiments.
  *
  * @return string
  */
 public function hasExperiments()
 {
     $count = Experiment::count();
     return $count > 1;
 }
Example #6
0
 public function testFirstPageView()
 {
     // Register fake named route
     Route::any('/foobar', function () {
         return 'hello world';
     });
     $headers = Request::instance()->server->getHeaders();
     $request = Request::create('http://localhost/foobar', 'get', [], [], [], $headers);
     Route::dispatch($request);
     $ab = App::make('ab');
     $ab->track($request);
     $ab->experiment();
     $this->assertEquals(1, Experiment::find('a')->visitors);
     $this->assertEquals(0, Experiment::find('a')->engagement);
 }
Example #7
0
 /**
  * Add experiments to the database.
  *
  * @return void
  */
 protected function checkExperiments()
 {
     // Check if the database contains all experiments.
     if (Experiment::active()->count() != count($this->getExperiments())) {
         // Insert all experiments.
         foreach ($this->getExperiments() as $experiment) {
             Experiment::firstOrCreate(['name' => $experiment]);
         }
     }
 }