创建标帜变体
About 7 min
创建标帜变体
描述标帜变体以及如何在云眼灰度发布(特性标帜)AB实验中创建它们。
标帜变体是一组可重用的标帜变量值(也称为远程功能配置或远程变量),可避免在代码中硬编码值。云眼灰度发布(特性标帜)AB实验根据标帜规则将这些变量值动态分配给标帜。有关什么是标帜变体的更多信息,请参阅标帜变体。要创建标帜变体,请执行以下步骤:
在云眼灰度发布(特性标帜)AB实验中设置标帜
(先决条件)创建默认变量
在创建变体之前,必须创建默认标帜_变量_。可以创建布尔值、字符串、整数、双精度或 JSON 类型的变量。JSON 变量值没有字符限制。
执行以下步骤,在 云眼 应用程序中创建具有默认值的标帜变量:
- 导航到“标帜”,然后选择一个标帜。
- 导航到变量,然后单击添加变量按钮(“+”图标)。
- 选择类型,然后编辑“键”和**“默认值**”。
- 为该标帜创建所需数量的变量。
创建标帜变体:
创建默认变量后,请执行以下步骤来创建标帜变体:
导航到**标帜>**变体。
点击添加变体按钮(“+”图标。
为您在上一节中创建的标帜变量配置新值。
使用标帜变体
若要控制最终用户的体验,请通过标帜规则提供标记变体。有关详细信息,请参阅:
实现标帜变体
在云眼灰度发布(特性标帜)AB实验应用中,你会看到标帜变体,这些_变体_允许你对变量值进行分组和重用。在代码实现中,可以使用返回的 云眼Decision 的变体键有条件地执行代码,但为了充分利用变量,_我们建议直接获取_变体变量的值。
下面的代码示例演示如何使用标帜变量。
📘 注意
此代码对用户所在的标帜规则类型(标帜传递或实验)无关,因此可以重复使用相同的代码来运行不同的规则。
/**
* Fetch enabled state for the "product_sort" flag.
* Then use flag variations to expose different sorting methods and pagation.
*/
//Define flag variable defaults (if the flag is disabled, you fall back to these values)
var sort_method = "alphabetical"
var page_limit = 10
// Is the flag enabled for the user?
let user = eyeofcloud.createUserContext(userId: userId)
let decision = user.decide(key: "product_sort")
let enabled = decision.enabled
/**
* In the online Eyeofcloud app, go define flag variations using
* sort_method and page_limit variables.
* For example, variation_1 might sort by popularity and limit results to 15 pages.
*/
if (enabled) {
// get flag variable values depending on the variation the user bucketed into
let sortMethod: String? = decision.variables.getValue(jsonPath: "sort_method")
let pageLimit: Integer? = decision.variables.getValue(jsonPath: "page_limit")
}
// Fetch products using the flag variables, using something like this pseudocode:
let products = productProvider.get(sortMethod, pageLimit)
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagation.
//Define flag variable defaults (if the flag is disabled, you fall back to these values)
sortMethod := "alphabetical"
pageLimit := 10
// Is the flag enabled for the user?
user := eyeofcloud.CreateUserContext(userId, nil)
decision := user.Decide("product_sort", nil)
enabled := decision.Enabled
// In the online Eyeofcloud app, go define flag variations using
// sort_method and page_limit variables.
// For example, variation_1 might sort by popularity and limit results to 15 pages.
if enabled {
// get flag variable values depending on the variation the user bucketed into
err1 := decision.Variables.GetValue("sort_method", &sortMethod)
err2 := decision.Variables.GetValue("page_limit", &pageLimit)
}
// Fetch products using the flag variables, using something like this pseudocode:
products := productProvider.get(sortMethod, pageLimit)
"""
Fetch enabled state for the "product_sort" flag.
Then use flag variations to expose different sorting methods and pagination.
"""
# Define flag variable defaults (if the flag is disabled, you fall back to these values)
sort_method = "alphabetical"
page_limit = 10
# Is the flag enabled for the user?
user = eyeofcloud.create_user_context("user123")
decision = user.decide("product_sort")
enabled = decision.enabled
"""
In the online Eyeofcloud app, go define flag variations using
sort_method and page_limit variables.
For example, variation_1 might sort by popularity and limit results to 15 pages.
"""
if enabled:
# get flag variable values depending on the variation the user bucketed into
sort_method = decision.variables["sort_method"]
page_limit = decision.variables["page_limit"]
# Fetch products using the flag variables, using something like this pseudocode:
products = ProductProvider.get(sort_method, page_limit)
/*
* Fetch enabled state for the "product_sort" flag.
* Then use flag variations to expose different sorting methods and pagation.
*/
// Define flag variable defaults (if the flag is disabled, you fall back to these values)
$sort_method = 'alphabetical';
$page_limit = 10;
$userId = "user123";
// Is the flag enabled for the user?
$user = $eyeofcloud->createUserContext($userId);
$decision = $user->decide('product_sort');
$enabled = $decision->getEnabled();
/**
* In the online Eyeofcloud app, go define flag variations using
* sort_method and page_limit variables.
* For example, variation_1 might sort by popularity and limit results to 15 pages.
*/
if ($enabled) {
// get flag variable values depending on the variation the user bucketed into
$sortMethod = (string) $decision->getVariables()['sort_method'];
$pageLimit = (int) $decision->getVariables()['page_limit'];
}
// Fetch products using the flag variables, using something like this pseudocode:
$products = $productProvider->get($sortMethod, $pageLimit);
# Fetch enabled state for the "product_sort" flag.
# Then use flag variations to expose different sorting methods and pagination.
# Define flag variable defaults (if the flag is disabled, you fall back to these values)
sort_method = "alphabetical"
page_limit = 10
# Is the flag enabled for the user?
user = eyeofcloud.create_user_context(userId)
decision = user.decide("product_sort")
enabled = decision.enabled
# In the online Eyeofcloud app, go define flag variations using
# sort_method and page_limit variables.
# For example, variation_1 might sort by popularity and limit results to 15 pages.
if (enabled)
# get flag variable values depending on the variation the user bucketed into
sortMethod = decision.variables["sort_method"]
pageLimit = decision.variables["page_limit"]
end
# Fetch products using the flag variables, using something like this pseudocode:
products = productProvider.get(sortMethod, pageLimit)
/**
* Fetch enabled state for the "product_sort" flag.
* Then use flag variations to expose different sorting methods and pagation.
*/
// Define flag variable defaults (if the flag is disabled, you fall back to these values)
var sortMethod = "alphabetical";
var pageLimit = 10;
// Is the flag enabled for the user?
var user = eyeofcloud.CreateUserContext(userId: "userId");
var decision = user.Decide(key: "product_sort");
var enabled = decision.Enabled;
/**
* In the online Eyeofcloud app, go define flag variations using
* sort_method and page_limit variables.
* For example, variation_1 might sort by popularity and limit results to 15 pages.
*/
if (enabled)
{
// get flag variable values depending on the variation the user bucketed into
sortMethod = decision.Variables.GetValue<string>("sort_method");
pageLimit = decision.Variables.GetValue<int>(jsonPath: "page_limit");
}
// Fetch products using the flag variables, using something like this pseudocode:
var products = productProvider.Get(sortMethod, pageLimit);
/**
* Fetch enabled state for the "product_sort" flag.
* Then use flag variations to expose different sorting methods and pagation.
*/
// Define flag variable defaults (if the flag is disabled, you fall back to these values)
String sortMethod = "alphabetical";
int pageLimit = 10;
// Is the flag enabled for the user?
EyeofcloudUserContext user = eyeofcloud.createUserContext("userId");
EyeofcloudDecision decision = user.decide("product_sort");
Boolean enabled = decision.getEnabled();
/**
* In the online Eyeofcloud app, go define flag variations using
* sort_method and page_limit variables.
* For example, variation_1 might sort by popularity and limit results to 15 pages.
*/
if (enabled)
{
// get flag variable values depending on the variation the user bucketed into
try {
sortMethod = decision.getVariables().getValue("sort_method", String.class);
pageLimit = decision.getVariables().getValue("page_limit", Integer.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
}
// Fetch products using the flag variables, using something like this pseudocode:
List products = productProvider.get(sortMethod, pageLimit);
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagination.
// Define flag variable defaults (if the flag is disabled, you fall back to these values)
let sort_method = 'alphabetical';
let page_limit = 10;
// Is the flag enabled for the user?
var user = eyeofcloud.createUserContext(userId);
var decision = user.decide('product_sort');
var enabled = decision.enabled;
//In the online Eyeofcloud app, go define flag variations using
// sort_method and page_limit variables.
// For example, variation_1 might sort by popularity and limit results to 15 pages.
if (enabled) {
// get flag variable values depending on the variation the user bucketed into
sortMethod = decision.variables['sort_method'];
pageLimit = decision.variables['page_limit'];
}
// Fetch products using the flag variables, using something like this pseudocode:
let products = productProvider.get(sortMethod, pageLimit);
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagination.
// Define flag variable defaults (if the flag is disabled, you fall back to these values)
let sort_method = 'alphabetical';
let page_limit = 10;
// Is the flag enabled for the user?
var decision = useDecision('product_sort');
var enabled = decision.enabled;
//In the online Eyeofcloud app, go define flag variations using
// sort_method and page_limit variables.
// For example, variation_1 might sort by popularity and limit results to 15 pages.
if (enabled) {
// get flag variable values depending on the variation the user bucketed into
sortMethod = decision.variables['sort_method'];
pageLimit = decision.variables['page_limit'];
}
// Fetch products using the flag variables, using something like this pseudocode:
let products = productProvider.get(sortMethod, pageLimit);
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagination.
// Define flag variable defaults (if the flag is disabled, you fall back to these values)
var sort_method = "alphabetical"
var page_limit = 10
// Is the flag enabled for the user?
var user = await flutterSDK.createUserContext("user123");
var decisionResponse = await user!.decide(sort_method);
var decision = decisionResponse.decision;
var enabled = decision!.enabled
// In the online Eyeofcloud app, go define flag variations using sort_method and page_limit variables.
// For example, variation_1 might sort by popularity and limit results to 15 pages.
if (enabled) {
// get flag variable values depending on the variation the user bucketed into
var sort_method = decision.variables["sort_method"]
var page_limit = decision.variables["page_limit"]
}
// Fetch products using the flag variables, using something like this pseudocode:
var products = ProductProvider.get(sort_method, page_limit)
有关更详细的示例,请参阅:
- 安卓示例用法
- Go 示例用法
- C# 示例用法
- Flutter示例用法
- Java 示例用法
- Javascript示例用法
- 节点示例用法
- PHP 示例用法
- Python示例用法
- React示例用法
- Ruby示例用法
- Swift示例用法
后续步骤
使用标记变体来构建标帜投放规则或实验规则: