Skip to content

A way to set predefined variation #12

@jangaraev

Description

@jangaraev

I need a way to set the variation manually. Here is my original code:

class SplitTestService
{
    const COOKIE_NAME = 'ab_seed';

    protected $container;


    public function __construct()
    {
        $this->container = new Container();

        $this->setSeed();
    }

    public function getHomepageVariation(): string
    {
        return tap(new Test('homepage'), function (Test $test) {
            $this->container->add($test);

            $test->setVariations([
                SplitTests::HOMEPAGE_DEFAULT => 7,
                SplitTests::HOMEPAGE_NEW => 3
            ]);
        })->getVariation();
    }

    private function setSeed(): void
    {
        $seed = request()->cookie(self::COOKIE_NAME);

        if (!$seed) {
            $seed = mt_rand();

            Cookie::queue(self::COOKIE_NAME, (string)$seed);
        }

        $this->container->setSeed($seed);
    }
}

But now in order to ease the development and testing I need to preset the variation from GET variable, something like this:

        return tap(new Test('homepage'), function (Test $test) {
            $this->container->add($test);

            $test->setVariations([
                SplitTests::HOMEPAGE_DEFAULT => 7,
                SplitTests::HOMEPAGE_NEW => 3
            ]);

            // a code to pre-set variation if the specific request variable is passed
            if (!App::environment('production') && ($homepageVariation = request()->query('homepage-variation'))) {
                $test->setVariation('b' === $homepageVariation ? SplitTests::HOMEPAGE_NEW : SplitTests::HOMEPAGE_DEFAULT);
            }
        })->getVariation();

What do you think of it @odino ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions