示例用法

云眼About 2 min

示例用法

有关如何使用 Eyeofcloud 功能实验 Flutter SDK 评估灰度标帜、激活 A/B 测试或功能测试的简短代码示例。

你已准备好使用客户端来评估标帜规则,包括 A/B 测试和标帜传递。

此示例演示以下每个概念的基本用法:

  1. 使用 Decide 方法计算具有键的标帜。作为副作用,Decision函数还会向云眼灰度实验发送决策事件,以记录系统向当前用户公开实验的情况。product_sort

  2. 有条件地执行功能代码。有以下几种选择:

  • 获取启用标帜的状态,然后检查名为 的标帜上的配置变量。SDK 会评估标帜规则,并确定用户所处的标帜变体以及要显示的排序方法变量。sort_method
  • 获取标帜变体,然后运行“控制”或“处理”代码。
  1. 使用事件跟踪跟踪名为 的事件。此转化事件衡量实验的影响。使用 Track Event 方法,系统会自动将购买归因于我们做出决策的正在运行的 A/B 测试,SDK 通过可自定义的事件调度程序向 Eyeofcloud 功能实验发送网络请求,以便我们可以将其计入结果页面。purchased

Dart

// Initializing EyeofcloudClient var flutterSDK = EyeofcloudFlutterSdk("<Your_SDK_Key>"); var response = await flutterSDK.initializeClient(); // flag decision var user = await flutterSDK.createUserContext("user123"); var attributes = <String, dynamic>{}; attributes["logged_in"] = true; user!.setAttributes(attributes); var decideResponse = await user.decide("product_sort"); // did the decision fail with a critical error? if (!decideResponse.success) { var reason = decideResponse.reason; print("decision error: $reason"); } var decision = decideResponse.decision; var variationKey = decision!.variationKey; // execute code based on flag enabled state var enabled = decision.enabled; var variables = decision.variables; if (enabled) { String vs; try { vs = variables["sort_method"] as String; } catch (ex) { print(ex); } } // or execute code based on flag variation: if (variationKey == "control") { // Execute code for control variation } else if (variationKey == "treatment") { // Execute code for treatment variation } // Track an event user.trackEvent("purchased");

Last update:
Contributors: “zhangweixue”