orchid/experiment

此包已被废弃,不再维护。未建议替代包。

Laravel的AB测试

2.4 2022-06-22 08:02 UTC

This package is auto-updated.

Last update: 2023-03-13 22:12:33 UTC


README

警告

随着Laravel 10的发布,有一个官方的Pennant包可以替代当前的包。因此请考虑升级到它。

实验

tests 68747470733a2f2f7374796c6563692e696f2f7265706f732f3135393733303034332f736869656c643f6272616e63683d6d6173746572 68747470733a2f2f636f6465636f762e696f2f67682f746162756e612f6578706572696d656e742f6272616e63682f6d61737465722f67726170682f62616467652e737667 68747470733a2f2f706f7365722e707567782e6f72672f6f72636869642f6578706572696d656e742f762f737461626c65 68747470733a2f2f706f7365722e707567782e6f72672f6f72636869642f6578706572696d656e742f646f776e6c6f616473 68747470733a2f2f706f7365722e707567782e6f72672f6f72636869642f6578706572696d656e742f6c6963656e7365

为Laravel提供的A/B测试套件,允许进行多个实验。

安装

使用Composer下载

$ composer require orchid/experiment

基本用法

默认情况下将使用您的缓存驱动程序。

use Orchid\Experiment\Experiment;

$experiment = new Experiment();

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

实验通过数组传递,键是名称,值是所需的比率。例如,如果您指定了包含A -> 50和B -> 100的两个值,则将有50个具有A值的用户,然后将有100个具有B值的用户。它允许我们清楚地定义测试的分配方式。

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

您也可以通过请求安装

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

Cookie

我建议将其放在中间件中,并立即使用它安装一个cookie。

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

它允许您使用JavaScript将数据传输到Google分析等类似服务。

alert( document.cookie );

Laravel默认加密所有cookie,所以不要忘记在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

如果您想使用Blade,您仍然必须在这次调用之后安装中间件,例如

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

测试

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

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件