Code Monkey home page Code Monkey logo

experiment's Introduction

Warning

With the release of Laravel 10, there is an official Pennant package that can replace the current one. So consider upgrading to it.

Experiment

tests

An A/B Testing suite for Laravel, which allows multiple experiments.

Installation

Download using Composer:

$ composer require orchid/experiment

Base Usage

Your cache driver will be used by default.

use Orchid\Experiment\Experiment;

$experiment = new Experiment();

// Distribution
$ab = $experiment->start([
    'A' => 1,
    'B' => 1,
]); // A or B

The experiment is transmitted in an array, where the keys are the names, and the values are the required ratios. For example, if you specify two values containing A -> 50 and B -> 100, there will be 50 users with the value A, then there will be 100 users with the value B. It allows us to define how the testing will be distributed clearly.

use Orchid\Experiment\Experiment;
use Illuminate\Support\Facades\Cache;

$storage = Cache::store('redis');
$experiment = new Experiment('my-key', $storage);

$ab = $experiment->start([
    'A' => 50,
    'B' => 100,
]); // A or B

You can also install via your request:

http:://example.com?my-key=A

Cookie

I recommend putting this on middleware and immediately install a cookie using.

namespace App\Http\Middleware;

use Closure;
use Orchid\Experiment\Experiment;

class Experiments
{
    
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     *
     * @return mixed
     * @throws \Exception
     */
    public function handle($request, Closure $next)
    {
        $experiment = new Experiment('AB');

        $experiment->startAndSaveCookie([
            'A' => 50,
            'B' => 50,
        ]);
        
        return $next($request);
    }
}

It allows you to transfer data to Google analytics and similar services using javascript.

alert( document.cookie );

Laravel encrypts all cookies by default, so do not forget to specify your key in the exceptions app/Http/Middleware/EncryptCookies.php:

namespace App\Http\Middleware;

use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;

class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        'AB'
    ];
}

Blade

If you want to use the blade, you still must install the middleware after this call is as example:

@experiment('my-key', 'A')
    <button>Click me</button>
@else
    <button>Push me</button>
@endexperiment

Tests

php vendor/bin/phpunit --coverage-html ./logs/coverage ./tests

License

The MIT License (MIT). Please see License File for more information.

experiment's People

Contributors

dravenshorst avatar tabuna 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

experiment's Issues

Improve usage guide

It would be great if you could provide usage examples for:

  • Adding and removing tests
  • Changing view contents based on which tests a user is part of

Thanks,

Will

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.