GithubHelp home page GithubHelp logo

ab's Introduction

NAMSHI | AB

Build Status

SensioLabsInsight

This library provides a layer to run AB tests on your applications.

AB testing is useful when you want to change anything on your application but want to benchmark different variations of the change (ie. display a button that says "Buy now", "Go to checkout" or "PAY, DUDE!").

Installation

This library is available through composer, as you can see from its packagist page.

Just require it in your composer.json:

"namshi/ab": "1.0.*"

Creating and running a test

Creating tests is very simple, as you only need to define the test name and the variations, with their absolute probability:

use Namshi\AB\Test;

$homepageColorTest = new Test('homepage_color', array(
    'blue' => 1,
    'red' => 1,
));

and at this point you can change the color of the homepage by simply running the test and checking which variation has been picked:

<html>
  ...
  ...
  <body style="background-color: <?php echo $homepageColorTest->getVariation(); ?>">

Of course, the mess of a code above is here just as an example ;-)

Handling multiple tests

Taken by an AB-test-rage, you might want to start using AB tests for everything: that's why we added a test container where you can register as much tests as you want, and retrieve them easily:

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

// instantiate the container with a test
$container = new Container(array(
    new Test('homepage_color', array(
        'blue'  => 1,
        'black' => 2,
    )),
));

// add another test
$container->add(new Test('checkout_button_text', array(
    'Buy now'               => 4,
    'Go to checkout'        => 1,
    'Proceed to checkout'   => 1,
)));

// then you can retrieve all the tests
$container->getAll();

// or a single test, by its name
$container->get('checkout_button_text');

The Container implements the ArrayAccess and Countable interfaces, so that you can access its tests like if it is an array:

$tests = $container; // I only do this for readability

foreach($tests as $test) {
    echo sprintf("Test '%s' has the variation %s", $test->getName(), $test->getVariation());
}

// if you have time to waste, count the tests :)
echo count($tests);

Variations

Variations' weight must be expressed in absolute values: if, for example, you have A: 1, B: 2 and C: 1, that means that the percentage of picking each variation is 25% (A), 50% (B) and 25%(C), as the sum of the weights is 4.

Variations can be set while constructing the test or later on:

$test = new Test('checkout_button_text', array(
    'Buy now!'          => 1,
    'Go to checkout!'   => 1,
));

// or you can set them afterwards
$test = new Test('checkout_button_text');

$test->setVariations(array(
    'Buy now!'          => 1,
    'Go to checkout!'   => 1,
));

Remember to set the variations before running the test with getVariation, else an exception is thrown:

$test = new Test('checkout_button_text');

$test->getVariation(); // will throw a BadMethodCallException

How to present the same variations across multiple requests

Let's say that you are running a test that defines whether the background color of your website should be black or white.

Once a user hits the homepage, he will get the white one, but as soon as he refreshes the page he might get the black one!

To be consistent with the variations, for a user's session, you should store a unique number (seed) and pass it to the tests before running them, so you will always be sure that specific user will always get the same variations of the tests:

$test = new Test('homepage_color', array(
    'white' => 1,
    'black' => 1,
));

// set the seed
$test->setSeed($_SESSION['seed_for_homepage_color_test']); // 326902637627;

$test->getVariation(); // black

In the next request, since the seed won't change, the user will get again the same variation, black.

This functionality is implemented thanks to PHP's mt_rand and mt_srand functions.

You shouldn't specify a different seed for each of your tests, but use the container instead:

$container = new Container(new Test('homepage_color', array(
    'black' => 1,
    'white' => 1,
)));

$container->setSeed($_SESSION['seed']); // 326902637627;);

The advantage of setting the seed through the container is that you don't have to maintain a seed for every test you run in the session, you can just use a global seed and the container will assign a unique seed to each test.

Disabling tests

Sometimes you might want to disable tests for different purposes, for example if the user agent who is visiting the page is a bot.

$test = new Test('my_ab_test', array(
    'a' => 0,
    'b' => 1,
));

$test->disable();

$test->getVariation(); // will return 'a'!

Once you disable the test and run it, it will always return the first variation, no matter what its odds are! Yes, even zero...

Test parameters

You can also attach any parameter you want to a test by just injecting them (or with the set method):

$test = new Test('example', array(1, 2), array(
    'par1' => 1,
    'par2' => new stdClass,
));

$test->set('par3', 'Whoooops!')

So that you can then easily retrieve them in other parts of the code:

$test->getParameters(); // returns all the parameters

$test->get('par3'); // Whoooops!

$test->get('i-dont-exist'); // NULL

Testing this library

This library has been unit tested with PHPUnit, so just cd into its folder and run phpunit.

ab's People

Contributors

hpatoio avatar hzarka avatar muitsfriday avatar odino avatar pborreli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ab's Issues

retrieve test variation id

Hi all,
is there a way to retrieve variation id for a test?

$test = new Test('homepage_color', array(
'white' => 1,
'black' => 1,
'yellow' => 1,
));

$test->getVariation(); // black

$test->getVariationId(); // 2

It should be useful in case of multivariation test to obtain a cross test unique id (tracking + analytics purpose).

Thanks,
Fabio

Same variation in seed

Hi,
test work well, but when i add seed to container it use only 2 type of variation, all first on last test choices, hereby the code:

    if (!$session->get('seed')) {
        $session->set('seed', mt_rand());
    }

    $abt = new Container(array(
        new Test('promo_code.layout', array(
            'TestAB:step1a.html.twig' => 1,
            'TestAB:step1b.html.twig'   => 1,
        )),
        new Test('promo_code.text', array(
            'Discount code'    => 1,
            'Promo code'     => 1,
        )),
        new Test('promo_code.button', array(
            'button'    => 1,
            'arrow'     => 1,
        )),
    ), $session->get('seed'));

    echo "seed: ".$session->get('seed');

What can be the issue? Thanks

No lisence statement

Hi,
I would like to use this nice code in our company, but I confuse because there is no license statement.
Can I use this code for commercial purpose?
We are not going to re-distribute, just only use and customize the code.

So does it evenly cycle?

I need to try to have as close as possible the same amount of viewers to each content so that I can properly chart the data. If I have 100 visitors, it would be ideal if 50 saw Content A and 50 saw Content B. Does your solution do this out of the box?

A way to set predefined variation

I need a way to set the variation manually. Here is my original code:

class SplitTestService
{
    const COOKIE_NAME = 'ab_seed';

    protected $container;


    public function __construct()
    {
        $this->container = new Container();

        $this->setSeed();
    }

    public function getHomepageVariation(): string
    {
        return tap(new Test('homepage'), function (Test $test) {
            $this->container->add($test);

            $test->setVariations([
                SplitTests::HOMEPAGE_DEFAULT => 7,
                SplitTests::HOMEPAGE_NEW => 3
            ]);
        })->getVariation();
    }

    private function setSeed(): void
    {
        $seed = request()->cookie(self::COOKIE_NAME);

        if (!$seed) {
            $seed = mt_rand();

            Cookie::queue(self::COOKIE_NAME, (string)$seed);
        }

        $this->container->setSeed($seed);
    }
}

But now in order to ease the development and testing I need to preset the variation from GET variable, something like this:

        return tap(new Test('homepage'), function (Test $test) {
            $this->container->add($test);

            $test->setVariations([
                SplitTests::HOMEPAGE_DEFAULT => 7,
                SplitTests::HOMEPAGE_NEW => 3
            ]);

            // a code to pre-set variation if the specific request variable is passed
            if (!App::environment('production') && ($homepageVariation = request()->query('homepage-variation'))) {
                $test->setVariation('b' === $homepageVariation ? SplitTests::HOMEPAGE_NEW : SplitTests::HOMEPAGE_DEFAULT);
            }
        })->getVariation();

What do you think of it @odino ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.