Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bart committed Jul 18, 2017
0 parents commit fd3e655
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Rene Bartkowiak <info@rene-bartkowiak.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Laravel A/B Testing
===================
AB is a server-side A/B testing tool for Laravel applications and provides a pretty simple feature set that is a great free alternative to services like Optimizely. It allows you to experiment with different variations of your website while test selection will be handled automatically.

Installation
------------
Install using composer:

composer require bart/ab

Add the service provider to `app/config/app.php`:

Bart\Ab\ServiceProvider::class,

Register the AB alias:

'AB' => Bart\Ab\Facade::class,

Configuration
-------------

Publish the included configuration file like this:

php artisan vendor:publish --provider="Bart\Ab\ServiceProvider"

Next, edit the `config/packages/bart/ab/config.php` file. The following configuration options are available:


### Enabled
Enables or disbales the A/B testing.

'enabled' => true


### Default
If A/B testing is disbaled, `AB::getCurrentTest()` will return this.

'default' => 'none'


### Tests
An array of test identifiers with an assigned level of distibution.

'tests' => [
'teaser1' => 1,
'teaser2' => 2,
'teaser3' => 1,
]

The above (default) configuration will display teaser version 2 to 50% of your users, whereas version 1 and 3 will be displayed to 25% of your users each.


Usage
-----
After you have defined your tests and enabled testing in the config you can start designing your A/B tests. It's as easy as 1-2-3 because the only thing you need to do is displaying a different peace of content for each test. Let's assume you have defined the tests from above, your view could look like this:

@test('teaser1')
Teaser 1 is being displayed
@endtest

@test('teaser2')
Teaser 2 is being displayed
@endtest

@test('teaser3')
Teaser 3 is being displayed
@endtest


### Tracking
This package doesn't handle any goal or conversion tracking because every company is approaching this in a slightly different way. We would suggest to use a custom Google Analytics dimension and pass the assigned test version in your master view:

dataLayer.push({'version': '{{ AB::getCurrentTest() }}'});


Contribution and questions
-------
If you have any questions or suggestions please feel free to ask or create megre request. Happy testing!
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "bart/ab",
"description": "A/B testing tool for Laravel apps",
"keywords": ["laravel","optimizely","ab","split","testing"],
"homepage": "https://github.com/bart/ab",
"license": "MIT",
"authors": [
{
"name": "Rene Bartkowiak",
"email": "info@rene-bartkowiak.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"illuminate/support": "^5.0",
"namshi/ab": "^1.1"
},
"autoload": {
"psr-4": {
"Bart\\Ab\\": "src/"
}
}
}
38 changes: 38 additions & 0 deletions src/Ab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Bart\Ab;

use Namshi\AB\Container;
use Namshi\AB\Test;

class Ab
{
private $tests = [];
private $default = null;

public function __construct()
{
if (!session('ab_seed')) {
session(['ab_seed' => mt_rand()]);
}

$this->tests = new Container([new Test('tests', $this->getTestsFromConfig())], session('ab_seed'));
$this->default = config('ab.default', 'none');
}

public function getCurrentTest() {
if (!$this->isEnabled()) {
return $this->default;
}

return $this->tests['tests']->getVariation();
}

private function getTestsFromConfig() {
return config('ab.tests', []);
}

private function isEnabled() {
return config('ab.enabled', false);
}
}
11 changes: 11 additions & 0 deletions src/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Bart\Ab;

class Facade extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor()
{
return 'ab';
}
}
42 changes: 42 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Bart\Ab;

use Illuminate\Support\Facades\Blade;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->publishes([__DIR__.'/config/ab.php' => config_path('ab.php')]);

Blade::directive('test', function($expression)
{
return "<?php if({$expression} === app('ab')->getCurrentTest()): ?>";
});

Blade::directive('endtest', function()
{
return '<?php endif; ?>';
});
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(Ab::class, function () {
return new Ab();
});

$this->app->alias(Ab::class, 'ab');
}
}
14 changes: 14 additions & 0 deletions src/config/ab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [

'enabled' => true,
'default' => 'none',

'tests' => [
'teaser1' => 1,
'teaser2' => 2,
'teaser3' => 1,
],

];

0 comments on commit fd3e655

Please sign in to comment.