lemonde/phalcon-abtest

Phalcon应用程序的AB测试库

3.0.1 2023-09-12 09:18 UTC

README

Phalcon AB测试

一个用于进行AB测试和结果计数的库

先决条件

为了正确运行,您需要在Phalcon的依赖注入中暴露一个名为 cache 的服务。

该服务至少应公开以下2个公共方法: hIncrByhScan。这些方法在 \Redis 类中是原生的(见 hIncrByhScan)。使用 \Phalcon\Cache\Backend\Redis 类可以如下定义它们:

namespace App;

class Redis extends \Phalcon\Cache\Backend\Redis {

    /**
     * @param string $key
     * @param string $hashKey
     * @param int $value
     * @return int
     */
    public function hIncrBy($key, $hashKey, $value)
    {
        return $this->_redis->hIncrBy($key, $hashKey, $value);
    }
    
    /**
     * @param string $key
     * @param string $pattern
     * @param int $count
     * @return array
     */
    public function hScan($key, $pattern = null, $count = 0)
    {
        $iterator = null;
        $results = [];
        $this->_redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
    
        do {
            $arr_keys = $this->_getRedis()->hScan($key, $iterator, $pattern, $count);
    
            if (!$arr_keys) {
                break;
            }
    
            foreach ($arr_keys as $str_field => $str_value) {
                $results[$str_field] = $str_value;
            }
        } while ($arr_keys);
    
        return $results;
    }
}

安装

版本

  • 1.X:Phalcon 3
  • 2.X:Phalcon 4
  • 3.X:Phalcon 5
  1. 添加依赖项

    您需要在Composer配置中添加git仓库

    {
       "repositories": [
           {
               "type": "vcs",
               "url": "https://github.com/lemonde/phalcon-abtest.git"
           }
       ],
    }
    
    composer require lemonde/phalcon-abtest
    
  2. 添加PHP配置

    1. 添加 dispatch 事件监听器

      $eventManager->attach('dispatch', new \ABTesting\Plugin\AnnotationListener());
    2. 添加volt扩展

      $volt->getCompiler()->addExtension(new \ABTesting\Volt\ABTestingExtension());
    3. 将控制器添加到路由

      # il faut forcément un paramètre nommé testName
      # et un autre nommé winner
      $router->add('/_my_ab_redirection/{testName:[a-zA-Z0-9\_]+}/{winner:[a-zA-Z0-9\_]+}', ['controller' => 'ab_test', 'action' => 'count', 'namespace' => 'ABTesting\Controller'])->setName('ab_test_redirect');
    4. (可选) 将报告添加到路由

      $router->add('/_my_ab_dashboard', ['controller' => 'ab_test', 'action' => 'report', 'namespace' => 'ABTesting\Controller'])->setName('ab_test_report');
  3. 添加AB测试配置(通过名为 config 的服务使用 \Phalcon\Config

    $config = new Phalcon\Config([
        
        // ...
        
        'ab_test' => [
            'home_text_content' => [
                'default' => 'home_test_A',
                'variants' => [
                    'home_test_A' => 'something',
                    'home_test_B' => 'some other thing',
                ],
                'chooser' => [\ABTesting\Chooser\PercentChooser::class]
            ],
            'home_link_url' => [
                'default' => 'https://www.google.com',
                'variants' => [
                    'home_test_A' => 'https://www.google.fr',
                    'home_test_B' => 'https://www.google.be',
                ],
                'chooser' => [\ABTesting\Chooser\PercentChooser::class]
            ],
            'home_partial' => [
                'default' => 'path/to/default',
                'variants' => [
                    'home_test_A' => 'path/to/A',
                    'home_test_B' => 'path/to/B',
                ],
                'chooser' => [\ABTesting\Chooser\PercentChooser::class]
            ],
        ],
    
    ]);

    更多信息 这里

  4. 使用注解 @AbTesting('home_text_content') 声明受AB测试影响的行为

  5. 使用volt函数显示所需的元素,例如

    • 测试一个文案

      <a {{ ab_test_href('home_text_content', 'https://www.google.com') }}>
          {{ ab_test_result('home_text_content') }}
      </a>
    • 测试一个定义为测试的链接

      <a {{ ab_test_href('home_link_url', ab_test_result('home_link_url')) }}>
          Lien
      </a>
    • 测试两种格式

      {# home.volt #}
      
      {{ partial('path/to/specific/partial/dir/' ~ ab_test_result('home_partial')) }}
      {# path/to/specific/partial/dir/path/to/A.volt #}
      
      <!-- your content -->
      <a {{ ab_test_href('home_partial', 'https://example.org/link/for/A') }}>
          Lien
      </a>
      {# path/to/specific/partial/dir/path/to/B.volt #}
      
      <!-- your content -->
      <a {{ ab_test_href('home_partial', 'https://example.org/link/for/B') }}>
          Lien
      </a>
      {# path/to/specific/partial/dir/path/to/default.volt #}
      
      <!-- your content -->
      <a href="https://example.org">
          Lien
      </a>

AB测试配置

要配置AB测试,请在配置文件中以数组形式进行

'nom_du_test' => [ # Définition du test
    'variants' => [ # Définition des résultats possibles
        'varianteA' => 'une valeur',
        'varianteB' => 'une autre valeur',
        'varianteC' => 1337,
    ],
    'default' => 'valeur par défaut', # Valeur par défaut du résultat (s'il n'y a pas eu de bataille par exemple)
    'chooser' => ['\La\Classe\Du\Chooser', 'les', 'arguments', 'du', 'constructeur']
]
  • 变体可以是与 var_export 兼容的任何类型。
  • 选择器必须是一个实现 ABTesting\Chooser\ChooserInterface 的类。
  • 您可以监听 ABTesting\Engine 的事件
    • abtest:beforeBattle:在计算测试之前
    • abtest:afterBattle:在计算测试之后
    • abtest:beforePrint:在通过volt显示结果之前
    • abtest:beforeClick:在通过测试链接进行重定向之前