orchid / experiment
此包已被废弃,不再维护。未建议替代包。
Laravel的AB测试
2.4
2022-06-22 08:02 UTC
Requires
- ext-json: *
- laravel/framework: ^9.0
Requires (Dev)
- orchestra/testbench: ~7.0
- phpunit/php-code-coverage: ^9.0
- phpunit/phpunit: ^9.0
README
警告
随着Laravel 10的发布,有一个官方的Pennant包可以替代当前的包。因此请考虑升级到它。
实验
为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)。有关更多信息,请参阅许可文件。