bastuijnman/flagpost

为 Laravel Pennant 提供基本的 A/B 测试结果

1.0.0-beta2 2024-01-31 22:36 UTC

This package is auto-updated.

Last update: 2024-09-26 14:47:44 UTC


README

Flagpost 是一个与 Laravel 和 Laravel Pennant 集成的包,它略微增强了基本的 A/B 测试功能,并允许您设置目标和跟踪“转换”率。

安装

将 Flagpost 安装到您的项目中

composer require bastuijnman/flagpost

安装后,请确保您运行迁移以确保可以在数据库中跟踪转换。

php artisan migrate

用法

在定义您的 Laravel Pennant 功能后,您可以使用 Goal 门面通过它们跟踪用户转换。Flagpost 旨在遵循与 pennant 相似的界面,因此为了跟踪一个目标,您只需这样做

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Pennant\Feature;
use Bastuijnman\Flagpost\Goal;
 
class PodcastController
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): Response
    {
        return Feature::active('new-api')
                ? $this->resolveNewApiResponse($request)
                : $this->resolveLegacyApiResponse($request);
    }

    public function listen(Request $request): Response
    {
        Goal::reached('new-api');
        // Return response
    }
 
    // ...
}

这意味着一旦您开始收听播客,您就达到了您功能的转换目标。

作用域

就像与 Pennant 一样,您可以通过以下方式指定您功能的作用域

Goal::for($team)->reached('billing-v2');

如果您已定义不同的 Pennant 默认作用域,Flagpost 应该会默认选择此作用域。

结果

Flagpost 允许您通过以下方法获取您的 Pennant 功能的结果

$results = Goal::results('purchase-button');
/*
 * 
 * [ 
 *   [ 'value' => 'total', 'converted' => 39 ], 
 *   [ 'value' => 'blue-sapphire', 'converted' => 17 ], 
 *   [ 'value' => 'seafoam-green', 'converted' => 13 ],
 *   [ 'value' => 'tart-orange', 'converted' => 9 ] 
 * ]
 * 
 */

您还可以通过以下方式检索时间序列数据

$timeseries = Goal::timeseries('purchase-button', CarbonInterval::hour());
/*
 * 
 * [
 *   'blue-sapphire' => [
 *     [ 'time' => 1706706000, 'converted' => 3 ],
 *     [ 'time' => 1706706300, 'converted' => 2 ],
 *     [ 'time' => 1706706600, 'converted' => 0 ],
 *     ...
 *   ],
 *   'seafoam-green' => [
 *     [ 'time' => 1706706000, 'converted' => 1 ],
 *     [ 'time' => 1706706300, 'converted' => 3 ],
 *     [ 'time' => 1706706600, 'converted' => 2 ],
 *     ...
 *   ],
 *   'tart-orange' => [
 *     [ 'time' => 1706706000, 'converted' => 0 ],
 *     [ 'time' => 1706706300, 'converted' => 2 ],
 *     [ 'time' => 1706706600, 'converted' => 5 ],
 *     ...
 *   ],
 * ]
 * 
 */

当在 timeseries 方法中提供任何 Laravel Pulse 的默认周期时,Flagpost 将自动推断在图表中显示的最佳间隔。但是,如果您只想检索数据,您可以像这样传递间隔参数

$timeseries = Goal::timeseries('purchase-button', CarbonInterval::hours(8), 3600);

其中间隔以秒为单位指定

Pulse

Flagpost 默认附带一个 Pulse 卡片,如果您的应用程序已安装 Pulse,您可以按照以下方式配置功能卡片

<livewire:flagpost.results cols="full" feature="home-button" />

它将渲染一个卡片,允许您根据所选周期查看总结果或基于时间序列。

Laravel Pulse card

测试

在克隆仓库并执行 composer install 之后,您可以运行

./vendor/bin/phpunit

以运行测试套件