bastuijnman/flagpost

Provides basic A/B test results for Laravel Pennant

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

This package is auto-updated.

Last update: 2024-05-31 00:22:30 UTC


README

Flagpost is a package that integrates with Laravel & Laravel Pennant to slightly enhance the basic A/B testing capabilities and lets you setup goals and track "conversion" rates.

Installation

Install Flagpost into your project:

composer require bastuijnman/flagpost

When installed make sure you run the migrations to make sure conversions can be tracked in your database.

php artisan migrate

Usage

After defining your Laravel Pennant features you can track user conversions with them by using the Goal facade. Flagpost aims to follow a similar interface to pennant, so in order to track a goal you would simply do

<?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
    }
 
    // ...
}

Which would mean that once you start listening a podcast you'd have reached the goal of your feature.

Scopes

Just like with Pennant you can specify the scope of your feature by doing

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

If you've defined a different Pennant default scope Flagpost should pick this up by default.

Results

Flagpost allows you to grab the results of your Pennant features through the following methods:

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

You can also retrieve timeseries data by doing the following:

$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 ],
 *     ...
 *   ],
 * ]
 * 
 */

When any of the default periods of Laravel Pulse are given in the timeseries method Flagpost will automatically infer the "best" interval to display in a chart. However if you just want to retrieve data you can pass in the interval parameter like so:

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

Where the interval is specified in seconds

Pulse

Flagpost ships with a card for Pulse by default, if your application has installed Pulse you can configure a feature card as follows:

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

It will render a card that allows you to either view the total results or in a timeseries based on the selected period.

Laravel Pulse card

Testing

After cloning the repo and doing composer install you can run

./vendor/bin/phpunit

To run the test suite