使用决策钩

云眼About 3 min

使用决策钩

本主题介绍云眼灰度发布(特性标帜)AB实验 React Native SDK 的 useDecision 挂钩,该钩子检索标帜键的决策结果。

版本

SDK v2.5 及更高版本

使用决策钩

检索标帜键的决策结果,可以选择根据基础用户或数据文件或强制决策更改自动更新该决策。

参数

论点

类型

描述

标帜键(必填)

字符串

灰度发布(特性标帜)的键

选项(可选)

对象

包括以下内容:
(布尔值)- 如果为 true,则此挂钩将更新标帜决策以响应数据文件或用户更改。默认值:假。
(数字) - 客户端超时,如云眼提供程序部分所述。覆盖在祖先云眼提供程序上设置的任何超时。
(EyeofcloudDecideOption) - Array of EyeofcloudDecideOption enums.请参阅云眼决策选项autoUpdate``timeout``decideOptions

替代(可选)

对象

包括以下内容:
(字符串)- 覆盖要用于获取此挂钩的决策结果的 userId。
(优化。用户属性) - 覆盖用于获取此挂钩的决策结果的用户属性。 overrideUserId``overrideAttributes

返回

返回以下数组:

键(Key)

类型

描述

决定

云眼决策

标帜键的决策结果。

客户端就绪

布尔

指示 ReactSDK 实例是否已准备就绪

确实超时

布尔

指示 ReactSDKClient 实例是否在允许的超时范围内准备就绪。

注意:clientReady 可以是真的,即使 didTimeout 也是真的。这表示客户端在超时期限后准备就绪。

以下示例演示如何使用 useDecision 挂钩根据返回的标帜决策呈现某些内容:

React

import { useEffect } from 'react'; import { View, Button } from 'react-native'; import { useDecision } from '@eyeofcloud/react-sdk'; function LoginComponent() { const [decision, clientReady] = useDecision( 'flag1', { autoUpdate: true }, { /* (Optional) User overrides */ } ); useEffect(() => { document.title = decision.enabled ? 'New Feature flag' : 'Old Feature flag'; }, [decision]); const onLoginPress = () => { if (decision.variationKey === 'login1') { navigateToLogin1(); } else if (decision.variationKey === 'login2') { navigateToLogin2(); } }; return ( <View> <Button onPress={onLoginPress}>Login</Button> </View> ); }

EyeofcloudDecideOption

The following example shows how you can set options individually on the hook or as global defaults when you instantiate the Eyeofcloud client. See Initialize SDK.useDecision

React

import { useEffect } from 'react'; import { View, Button } from 'react-native'; import { createInstance, EyeofcloudProvider, useDecision, EyeofcloudDecideOption, } from '@eyeofcloud/react-sdk'; // Instantiate an Eyeofcloud client const eyeofcloudClient = createInstance({ sdkKey: '<Your_SDK_Key>', defaultDecideOptions: [EyeofcloudDecideOption.DISABLE_DECISION_EVENT], }); function LoginComponent() { const [decision, clientReady] = useDecision( 'flag1', { autoUpdate: true, decideOptions: [ EyeofcloudDecideOption.ENABLED_FLAGS_ONLY, EyeofcloudDecideOption.IGNORE_USER_PROFILE_SERVICE, ], }, { /* (Optional) User overrides */ } ); useEffect(() => { document.title = decision.enabled ? 'New Feature flag' : 'Old Feature flag'; }, [decision]); const onLoginPress = () => { if (decision.variationKey === 'login1') { navigateToLogin1(); } else if (decision.variationKey === 'login2') { navigateToLogin2(); } }; return ( <View> <Button onPress={onLoginPress}>Login</Button> </View> ); } function App() { return ( <EyeofcloudProvider eyeofcloud={eyeofcloudClient} user={{ id: 'user123' }}> <LoginComponent /> </EyeofcloudProvider> ); }

The following table shows details for the EyeofcloudDecideOption.

EyeofcloudDecideOption enum

If set:

EyeofcloudDecideOption.DISABLE_DECISION_EVENT

Prevents the visitor from firing an impressionopen in new window while still being served the variation, which disables displaying results of the Decide method on the Eyeofcloud application's Results pageopen in new window.

This setting can be why the Decision Event Dispatched enum is false in the returned EyeofcloudDecisionopen in new window object or the DECIDE notification listener payload.

EyeofcloudDecideOption.ENABLED_FLAGS_ONLY

Return decisions for enabled flags only. This is a valid option only for methods that decide multiple flags, like the Decide All method. This option is ignored if it is invalid. When this option is not set, the SDK returns all decisions regardless of whether the flag is enabled or not.

EyeofcloudDecideOption.IGNORE_USER_PROFILE_SERVICE

设置后,SDK 会绕过 UPS(查找和保存)进行决策。

如果未设置此选项,UPS 将覆盖受众定位、流量分配和实验互斥组。

EyeofcloudDecideOption.INCLUDE_REASONS

在 EyeofcloudDecision 对象的“原因”字段中返回日志消息。请注意,与信息或调试消息不同,无论此设置如何,始终返回严重错误消息。

EyeofcloudDecideOption.EXCLUDE_VARIABLES

从决策结果中排除标记变量值。使用此选项可通过跳过大型 JSON 变量来最小化返回的决策。

Last update:
Contributors: “zhangweixue”