namshi / ab
在代码中运行AB测试的库。
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-14 14:50:20 UTC
README
此库提供了一个在应用程序上运行AB测试的层。
当您想要更改应用程序中的任何内容,但又想比较更改的不同变体时(例如,显示一个按钮,上面写着“立即购买”、“前往结账”或“付钱,哥们!”),AB测试非常有用。
安装
您可以通过composer获取此库,就像从其Packagist页面中看到的那样。
只需在您的composer.json
中添加它
"namshi/ab": "1.0.*"
创建和运行测试
创建测试非常简单,您只需定义测试名称和变体,以及它们的绝对概率
use Namshi\AB\Test; $homepageColorTest = new Test('homepage_color', array( 'blue' => 1, 'red' => 1, ));
在此阶段,您只需运行测试并检查哪个变体已被选中即可更改主页的颜色
<html> ... ... <body style="background-color: <?php echo $homepageColorTest->getVariation(); ?>">
当然,上面的代码混乱只是为了举例而已 ;-)
处理多个测试
鉴于AB测试狂热,您可能想要将AB测试用于所有事情:这就是为什么我们添加了一个测试容器,您可以注册尽可能多的测试,并轻松检索它们
use Namshi\AB\Container; use Namshi\AB\Test; // instantiate the container with a test $container = new Container(array( new Test('homepage_color', array( 'blue' => 1, 'black' => 2, )), )); // add another test $container->add(new Test('checkout_button_text', array( 'Buy now' => 4, 'Go to checkout' => 1, 'Proceed to checkout' => 1, ))); // then you can retrieve all the tests $container->getAll(); // or a single test, by its name $container->get('checkout_button_text');
Container
实现了ArrayAccess
和Countable
接口,因此您可以像访问数组一样访问其测试
$tests = $container; // I only do this for readability foreach($tests as $test) { echo sprintf("Test '%s' has the variation %s", $test->getName(), $test->getVariation()); } // if you have time to waste, count the tests :) echo count($tests);
变体
变体的权重必须用绝对值表示:例如,如果您有A: 1
、B: 2
和C: 1
,这意味着选择每个变体的百分比是25%(A)、50%(B)和25%(C),因为权重的总和是4。
可以在构建测试时设置变体,也可以稍后设置
$test = new Test('checkout_button_text', array( 'Buy now!' => 1, 'Go to checkout!' => 1, )); // or you can set them afterwards $test = new Test('checkout_button_text'); $test->setVariations(array( 'Buy now!' => 1, 'Go to checkout!' => 1, ));
请记住,在运行测试之前使用getVariation
设置变体,否则将抛出异常
$test = new Test('checkout_button_text'); $test->getVariation(); // will throw a BadMethodCallException
如何在多个请求中展示相同的变体
假设您正在运行一个测试,该测试定义了网站背景颜色应该是黑色还是白色。
一旦用户点击主页,他将获得白色,但当他刷新页面时,他可能会得到黑色!
为了与变体保持一致,对于用户的会话,您应该存储一个唯一的数字(种子)并在运行测试之前将其传递给测试,这样您就可以确保特定用户始终获得测试的特定变体
$test = new Test('homepage_color', array( 'white' => 1, 'black' => 1, )); // set the seed $test->setSeed($_SESSION['seed_for_homepage_color_test']); // 326902637627; $test->getVariation(); // black
在下一次请求中,由于种子不会更改,用户将再次获得相同的变体,即黑色
。
此功能是通过PHP的mt_rand
和mt_srand
函数实现的。
您不应该为每个测试指定不同的种子,而是使用容器
$container = new Container(new Test('homepage_color', array( 'black' => 1, 'white' => 1, ))); $container->setSeed($_SESSION['seed']); // 326902637627;);
通过容器设置种子的好处是您不需要为会话中运行的每个测试维护种子,您只需使用全局种子,容器将为每个测试分配唯一的种子。
禁用测试
有时您可能想要出于不同的目的禁用测试,例如,如果访问页面的用户代理是机器人。
$test = new Test('my_ab_test', array( 'a' => 0, 'b' => 1, )); $test->disable(); $test->getVariation(); // will return 'a'!
一旦您禁用测试并运行它,它将始终返回第一个变体,无论其概率如何!是的,甚至是零...
测试参数
您还可以通过注入(或使用set
方法)将任何参数附加到测试中
$test = new Test('example', array(1, 2), array( 'par1' => 1, 'par2' => new stdClass, )); $test->set('par3', 'Whoooops!')
这样您就可以轻松地在代码的其他部分中检索它们
$test->getParameters(); // returns all the parameters $test->get('par3'); // Whoooops! $test->get('i-dont-exist'); // NULL
测试此库
这个库已经用 PHPUnit 进行了单元测试,所以只需将 cd
命令进入其文件夹,然后运行 phpunit
即可。