确定方法

云眼About 5 min

确定方法

本主题概述了可用于在云眼全栈中为用户返回标帜决策的 decide 方法。

建议修改

使用 Decide 方法返回用户的标帜决策。标帜决策包括标帜启用/禁用状态和标帜变体。

本页介绍以下决策方法:

决定

版本

SDK 3.7 及更高版本

描述

返回用户的标帜键的决策结果。决策结果在 EyeofcloudDecision 对象中返回,并包含传递标帜规则所需的所有数据。

Decide 是 UserContext 对象的一种方法。有关详细信息,请参阅 EyeofcloudUserContext

有关返回的决策对象的详细信息,请参阅 EyeofcloudDecision

参数

下表描述了 Decide 方法的参数:

参数

类型

描述

旗键

字符串

灰度标帜的键

选项(可选)

数组

Array of EyeofcloudDecideOption enums.请参阅下表。

云眼决策选项

下面的示例演示如何在任何 Decide 方法上单独设置选项,或在实例化 Eyeofcloud 客户端时将其设置为全局默认值。请参见初始化软件开发工具包

Swift

# set global default decide options when initializing the client let eyeofcloud = EyeofcloudClient(sdkKey: sdkKey, defaultDecideOptions: [.disableDecisionEvent]) # set additional options in a decide call let user = eyeofcloud.createUserContext(userId: "user123") let decisions = user.decideAll(options: [.enabledFlagsOnly, .disableDecisionEvent])

下表显示了云眼决策选项的详细信息。

云眼决策选项枚举

如果设置:

EyeofcloudDecideOption.disableDecisionEvent

防止访问者在仍收到变体的同时触发展示open in new window,从而禁止在“结果”页面上open in new window显示 Decide 方法的结果。

此设置可能是为什么决策事件调度枚举在返回的 falseEyeofcloudDecisionopen in new window 对象或 DECIDE 通知侦听器有效负载。

EyeofcloudDecideOption.enabledFlagsOnly

仅返回已启用标帜的决策。此选项仅适用于确定多个标帜的方法,如 Decide All 方法。如果此选项无效,则忽略此选项。如果未设置此选项,SDK 将返回所有决策,无论是否启用该标帜。

EyeofcloudDecideOption.ignoreUserProfileService

When set, the SDK bypasses UPS (both lookup and save) for the decision.

When this option is not set, UPS overrides audience targeting, traffic allocation, and experiment mutual exclusion groups.

EyeofcloudDecideOption.includeReasons

Return log messages in the Reasons field of EyeofcloudDecision object. Note that unlike info or debug messages, critical error messages are always returned, regardless of this setting.

EyeofcloudDecideOption.excludeVariables

Exclude flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables.

Returns

The Decide method returns an EyeofcloudDecision object. For more information, see EyeofcloudDecision.

If the method encounters a critical error (SDK not ready, invalid flag key, etc), then it returns a decision with a null Variation Key field and populates the Reasons field with error messages (regardless of the Include Reasons option).

Example Decision

The following is an example of calling the Decide method and accessing the returned EyeofcloudDecision object:

Swift

// create the user and decide which flag rule & variation they bucket into let user = eyeofcloudClient.createUserContext(userId: "user123", attributes: ["logged_in": true]) let decision = user.decide(key: "product_sort") // Did the decision fail with a critical error? guard let variationKey = decision.variationKey else { print("[decide] error: \(decision.reasons)") return } // flag enabled state: let enabled: Bool = decision.enabled // String variable value: let value1: String? = decision.variables.getValue(jsonPath: "sort_method") // or: let value2: String? = decision.variables.toMap()["sort_method"] as? String // all variable values let allVarValues: EyeofcloudJSON = decision.variables // variation. if null, decision fail with a critical error let variationKey: String = decision.variationKey // flag decision reasons let reasons: [String] = decision.reasons // user for which the decision was made let userContext: EyeofcloudUserContext = decision.userContext

Side Effects

Invokes the notification listener if this listener is enabled.DECISION

Decide All

Returns decisions for all active (unarchived) flags for a user.

See EyeofcloudDecision for details.

Version

SDK v3.6 and higher

Description

Use the Decide All method to return a map of flag decisions for a user.

Parameters

The following table describes parameters for the Decide All method:

Parameter

Type

Description

options (optional)

Array

Array of EyeofcloudDecideOption enums. See EyeofcloudDecideOption.

返回

方法返回云眼决策的映射。有关更多信息,请参阅云眼决策

如果该方法对所有标帜都失败(例如,SDK 未就绪或用户上下文无效),则返回空映射。如果该方法检测到特定标帜的错误,它将在该标帜的决策的“原因”字段中返回错误消息。

例子

下面是使用“全部决定”调用获取用户的标帜的示例:

Swift

// make decisions for all active (unarchived) flags in the project for a user let decisions = user.decideAll() // or only for enabled flags let decisions = user.decideAll(options: [.enabledFlagsOnly]) let flagKeys = decisions.keys let flagDecisions = decisions.values let decisionForFlag1 = decisions["flag_1"]

副作用

如果启用了此侦听器,则为每个决策调用通知侦听器DECISION

决定指定的键

在 Swift SDK 中,如果传递 keys 参数,则 Decide 方法将返回特定标帜键的标帜决策映射。

变体

SDK v3.6 及更高版本

描述

获取特定标帜键的标帜决策图。请注意,在 SDK 中,如果 Decide 方法不是多态的,因此不能接受参数,则有一个 Decide For Keys 方法来实现本部分所述的功能。keys

参数

下表描述了 Decide 方法的参数:

参数

类型

描述

钥匙

数组

字符串标帜键数组。

选项(可选)

数组

Array of EyeofcloudDecideOption enums.请参阅云眼决策选项

返回

返回云眼决策的映射。有关更多信息,请参阅云眼决策

如果该方法对所有标帜都失败(例如,SDK 未就绪或用户上下文无效),则返回空映射。如果该方法检测到特定标帜的错误,它将在该标帜的决策的“原因”字段中返回错误消息。

以下是获取用户指定标帜的示例:

Swift

// make a decisions for specific enabled flags let decisions = user.decide(keys: ["flag_1", "flag_2"], options: [.enabledOnly]) let decisionForFlag1 = decisions["flag_1"] let decisionForFlag2 = decisions["flag_2"]

副作用

如果启用了此侦听器,则为每个决策调用通知侦听器DECISION

源文件

包含 Swift 实现的语言/平台源文件是 EyeofcloudClient.swiftopen in new window

Last update:
Contributors: “zhangweixue”