示例用法

云眼About 2 min

示例用法

本主题提供如何使用云眼灰度发布(特性标帜)AB实验 Android SDK 评估灰度发布(特性标帜)、激活 A/B 测试或功能测试的简短代码示例。

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

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

  1. 使用 Decide 方法计算具有键product_sort的标帜。作为副作用,Decision函数还会向Eyeofcloud发送决策事件,以记录当前用户已暴露于实验。

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

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

Kotlin

// Build a manager 
val eyeofcloudManager = EyeofcloudManager.builder()
         .withSDKKey("<Your_SDK_Key>")         
         .build(context) 
// Instantiate a client synchronously with a bundled datafile 
// copy datafile JSON from URL accessible in app>settings 
val datafile = "REPLACE_WITH_YOUR_DATAFILE" 
val eyeofcloudClient = eyeofcloudManager.initialize(context, datafile)  

// Create a user-context 
val attributes: MutableMap<String, Any> = HashMap() 
attributes["logged_in"] = true 
val user = eyeofcloudClient.createUserContext("user123", attributes)!!  

// Call the decide method 
val decision = user.decide("product_sort")  

// did the decision fail with a critical error? 
val variationKey = decision.variationKey 
if (variationKey == null) {    
    val reasons = decision.reasons    
    Log.d("Eyeofcloud", "decision error: $reasons") 
}  

// execute code based on flag enabled state 
val enabled = decision.enabled 
val variables = decision.variables 
if (enabled) {    
    var vs: String? = null    
    try {
       vs = variables.getValue("sort_method", String::class.java)    
    } catch (e: JsonParseException) {       
        e.printStackTrace()    
        } 
    }  
    
    // 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")

Java

// Build a manager 
EyeofcloudManager eyeofcloudManager = EyeofcloudManager.builder()
        .withSDKKey("<Your_SDK_Key>")        
        .build(context); 
        
// Instantiate a client synchronously with a bundled datafile 
// copy datafile JSON from URL accessible in app>settings 
String datafile = "REPLACE_WITH_YOUR_DATAFILE"; 
EyeofcloudClient eyeofcloudClient = eyeofcloudManager.initialize(context, datafile);  

// Create a user-context 
Map<String, Object> attributes = new HashMap<>(); 
attributes.put("logged_in", true); 
EyeofcloudUserContext user = eyeofcloudClient.createUserContext("user123", attributes);  

// Call the decide method 
EyeofcloudDecision decision = user.decide("product_sort");

// did the decision fail with a critical error? 
String variationKey = decision.getVariationKey(); 
if (variationKey == null) {  
    List<String> reasons = decision.getReasons();
    Log.d("Eyeofcloud", "decision error: " + reasons);
} 

// execute code based on flag enabled state 
boolean enabled = decision.getEnabled(); 
EyeofcloudJSON variables = decision.getVariables(); 
if (enabled) {    
    String vs = null;
    try { 
        vs = variables.getValue("sort_method", String.class);
    } catch (JsonParseException e) {       
        e.printStackTrace();    
    } 
}  

// or execute code based on flag variation: 
if (variationKey.equals("control")) {    
    // Execute code for control variation 
} else if (variationKey.equals("treatment")) {    
    // Execute code for treatment variation 
}  

// Track an event
user.trackEvent("purchased");`

Last update:
Contributors: “zhangweixue”,zhangweixue