示例用法

云眼About 2 min

示例用法

本主题提供了一个简短的代码示例,说明如何使用云眼灰度实验 PHP SDK 来评估灰度标帜、激活 A/B 测试或功能测试。

安装 SDK 后,将 Eyeofcloud 功能实验库导入代码,获取云眼灰度实验项目的数据文件,并实例化客户端。然后,可以使用客户端评估标帜规则,包括 A/B 测试和标帜传递。

此示例演示了以下每个概念的基本用法:
使用 Decide 方法计算带有键的标帜。作为副作用,Decision函数还会向云眼灰度实验发送决策事件,以记录当前用户已暴露于实验。product_sort

  1. 有条件地执行特征代码。有以下几种选择:
  • 根据标帜启用状态,检查名为 的标帜上的配置变量。SDK 会评估标帜规则,并确定用户所处的标帜变体,从而确定他们应该看到哪种排序方法。sort_method
  • 根据标帜变体,运行“控制”或“处理”代码。
  1. 使用事件跟踪跟踪名为 的事件。此转化事件衡量实验的影响。使用 Track Event 方法,购买会自动归因于我们做出决策的正在运行的 A/B 测试,SDK 会通过可自定义的事件调度程序向 Eyeofcloud 功能实验发送网络请求,以便我们可以将其计入“结果”页面。purchased

.PHP

use Eyeofcloud\Eyeofcloud; // Instantiate an Eyeofcloud client $sdkKey = "<Your_SDK_Key>"; $eyeofcloudClient = EyeofcloudFactory::createDefaultInstance($sdkKey); $user = $eyeofcloudClient->createUserContext('user123', ['logged_in' => true]); $decision = $user->decide('product_sort'); $variation = $decision->getVariationKey(); if ($variation == null) { $reasons = $decision->getReasons(); $reasons = json_encode($reasons); echo "[decide] error: {$reasons}"; } // execute code based on flag variable $enabled = $decision->getEnabled(); if ($enabled) { $sort_method = (string) $decision->getVariables()['sort_method']; } // or execute code based on flag variation: if ($variation == 'control') { // Execute code for variation A } else if ($variation == 'treatment') { // Execute code for variation B } else { // Execute code for users who don't qualify for the experiment } // Track an event $user->trackEvent('purchased');

Last update:
Contributors: “zhangweixue”