Skip to content

Commit

Permalink
Adding the export command
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jul 27, 2014
1 parent fada01b commit 8140161
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 12 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,31 @@ This will generate a simple output containing the results for each experiment an
| c | 173,073 | 5.0 % (8,653) | 1.0 % (1,730) | 1.3 % (5,538) | 3.2 % (5,538) |
+------------+----------+----------------+---------------+---------------+---------------+

You can also export these reports to .csv format using this command:

php artisan ab:export /path/to/file.csv

If you run that command without a filepath, it will write it to the console.

Advanced
--------

### AB::complete($goal)
***AB::pageview()**

Used to manually trigger goals. Useful when you want to track goals that are not linked to urls or routes.
Used to manually trigger an pageview.

### AB::interact()
***AB::interact()**

Used to manually trigger an interaction which results in engagement.

**AB::complete($goal)**

Used to manually trigger goals. Useful when you want to track goals that are not linked to urls or routes.

**AB::getExperiments()**

Get the list of experiments.

**AB::getGoals()**

Get the list of goals.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
],
"license" : "MIT",
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"illuminate/support": "~4.0",
"pear/console_table": "*"
"pear/console_table": "*",
"league/csv": "5.*"
},
"require-dev": {
"orchestra/testbench": "2.2.*",
Expand Down
114 changes: 114 additions & 0 deletions src/Commands/ExportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php namespace Jenssegers\AB\Commands;

use Jenssegers\AB\Models\Experiment;
use Jenssegers\AB\Models\Goal;

use SplTempFileObject;
use League\Csv\Writer;
use Illuminate\Support\Facades\File;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ExportCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'ab:export';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Export the A/B testing repor to a CSV file.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* 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->setEncoding("utf-8");
$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);
}
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('file', InputArgument::OPTIONAL, 'The target CSV file to write the output to.')
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array();
}

}
33 changes: 26 additions & 7 deletions src/TesterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

use Jenssegers\AB\Session\LaravelSession;
use Jenssegers\AB\Session\CookieSession;
use Jenssegers\AB\Commands\InstallCommand;
use Jenssegers\AB\Commands\FlushCommand;
use Jenssegers\AB\Commands\ReportCommand;

use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -46,11 +43,33 @@ public function register()
return new Tester(new CookieSession);
});

$this->registerCommands();
}

/**
* Register Artisan commands.
*
* @return void
*/
protected function registerCommands()
{
// Available commands.
$commands = ['install', 'flush', 'report', 'export'];

// Bind the command objects.
foreach ($commands as &$command)
{
$class = 'Jenssegers\\AB\\Commands\\' . ucfirst($command) . 'Command';
$command = "ab::command.$class";

$this->app->bind($command, function($app) use ($class)
{
return new $class();
});
}

// Register artisan commands.
$this->app['ab.commands.install'] = new InstallCommand;
$this->app['ab.commands.flush'] = new FlushCommand;
$this->app['ab.commands.report'] = new ReportCommand;
$this->commands('ab.commands.install', 'ab.commands.flush', 'ab.commands.report');
$this->commands($commands);
}

}
23 changes: 23 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,27 @@ public function testReport()
$this->assertContains('42', $report);
}

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);
}

}

0 comments on commit 8140161

Please sign in to comment.