morilion / split-test
一个用于获取分割测试(A/B测试)功能的库
v0.3.2.3
2022-01-14 09:19 UTC
Requires
- php: >=7.1 || >=8.0
- ext-iconv: *
Requires (Dev)
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^3.8
README
这是一个小型的库,可以帮助您将自定义的分割测试(A/B测试)逻辑添加到项目中。
简单用法
<?php use Mordilion\SplitTest\Container; use Mordilion\SplitTest\Model\Experiment; // receive seed cookie if available $seed = $_COOKIE['seed'] ?? time(); $experimentName = 'an-experiment'; // set up the container with tests $container = new Container(); $experiment = new Experiment($experimentName, true); $experiment->addVariation(new Experiment\Variation('a', 50)); // 50% $experiment->addVariation(new Experiment\Variation('b', 50)); // 50% $container->addExperiment($experiment); // select the variation based on the provided seed $variation = $container->getExperimentVariation($experimentName); if ($variation === null) { // happens only, if the experiment wasn't defined throw new \RuntimeException(sprintf('Experiment "%s" does not exist!', $experimentName)); } if ($variation->getName() === 'b') { echo 'Variation B'; } else { echo 'Variation A'; } // set seed cookie for next visit setcookie('seed', $seed, time() + (3600 * 24 * 30)); // 30 days valid - after 30 days the user is unknown!