Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to set predefined variation #12

Open
jangaraev opened this issue Jun 29, 2022 · 1 comment
Open

A way to set predefined variation #12

jangaraev opened this issue Jun 29, 2022 · 1 comment

Comments

@jangaraev
Copy link

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 ?

@jangaraev
Copy link
Author

jangaraev commented Jun 29, 2022

I just want to resolve it a way to stay consistent with a seed set on container level. So that if a user first accesses the page with the query parameter next time the script acts accordingly even without that parameter.

Otherwise I can easily do like as follows:

public function getHomepageVariation(): string
{
    if (!App::environment('production') && ($homepageVariation = request()->query('homepage-variation'))) {
        return 'b' === $homepageVariation ? SplitTests::HOMEPAGE_NEW : SplitTests::HOMEPAGE_DEFAULT;
    }

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant