ReactSDKClient
2023年5月11日大约 4 分钟
ReactSDKClient
本主题介绍 ReactSDKClient,这是一个包装器,如果不想使用组件与云眼灰度标帜(Feature Flag)AB实验进行交互,则允许访问标准 SDK 方法。
ReactSDKClient 是一个包装器,如果不想使用 React 组件或钩子(例如 useDecision)与 Eyeofcloud灰度标帜(Feature Flag) 进行交互,则可以访问标准 SDK 方法,例如 Decide 方法。与其他 SDK 不同,不需要在每次调用方法时都向它们提供用户信息,只要在实例化时传递用户信息即可。以下部分介绍 ReactSDKClient 提供的方法。
版本
SDK v2.2.0
描述
ReactSDKClient 的实例是用createInstance创建的,它也被传递给具有高阶withEyeofcloud组件的eyeofcloudprop 中的子组件。
ReactSDKClient 在下面提供了以下 API 接口。请注意,如果实例是由withEyeofcloud或如果被setUser调用,则在调用云眼客户端对象的方法时,不需要传入 userId 或属性参数,除非希望使用与 EyeofcloudProvider或setUser不同的 userId 或属性。
提供的接口
onReady(opts?: { timeout?: number }): Promise返回一个 Promise,该 Promise 通过表示数据文件提取过程的对象来实现。参见 JavaScript: onReadyuser: User与此客户端实例关联的当前用户setUser(userInfo: User): void调用此函数以更新当前用户onUserUpdate(handler: (userInfo: User) => void): () => void订阅要在此实例的当前用户更改时调用的回调。返回将取消订阅回调的函数。decide(key: string, options?: eyeofcloud.EyeofcloudDecideOption[], overrideUserId?: string, overrideAttributes?: eyeofcloud.UserAttributes): EyeofcloudDecision返回用户的标帜键的决策结果。决策结果在 EyeofcloudDecision 对象中返回,并包含传递标帜规则所需的所有数据。decideAll(options?: eyeofcloud.EyeofcloudDecideOption[], overrideUserId?: string, overrideAttributes?: eyeofcloud.UserAttributes): { [key: string]: EyeofcloudDecision }返回用户的所有活动(未存档)标帜的决策。decideForKeys(keys: string[], options?: eyeofcloud.EyeofcloudDecideOption[], overrideUserId?: string, overrideAttributes?: eyeofcloud.UserAttributes): { [key: string]: EyeofcloudDecision }返回由标帜键映射的决策结果的对象。activate(experimentKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): string | null激活实验,并返回给定用户的变体。getVariation(experimentKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): string | null返回给定实验和用户的变体。getFeatureVariables(featureKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): VariableValuesObject:决定并返回给定标帜和用户的变量值getFeatureVariableString(featureKey: string, variableKey: string, overrideUserId?: string, overrideAttributes?: eyeofcloud.UserAttributes): string | null:确定并返回给定特征、变量和用户的变量值getFeatureVariableInteger(featureKey: string, variableKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): number | null确定并返回给定功能、变量和用户的变量值getFeatureVariableBoolean(featureKey: string, variableKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): boolean | null确定并返回给定功能、变量和用户的变量值getFeatureVariableDouble(featureKey: string, variableKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): number | null确定并返回给定功能、变量和用户的变量值isFeatureEnabled(featureKey: string, overrideUserId?: string, overrideAttributes?: UserAttributes): boolean返回给定标帜和用户的启用状态getEnabledFeatures(overrideUserId?: string, overrideAttributes?: UserAttributes): Array<string>:返回为给定用户启用的所有功能的键track(eventKey: string, overrideUserId?: string | EventTags, overrideAttributes?: UserAttributes, eventTags?: EventTags): void将事件跟踪到云眼灰度标帜(Feature Flag)AB实验结果后端setForcedVariation(experiment: string, overrideUserIdOrVariationKey: string, variationKey?: string | null): boolean为给定实验、变体和用户设置强制变体getForcedVariation(experiment: string, overrideUserId?: string): string | null获取给定实验、变体和用户的强制变体setForcedDecision(decisionContext: eyeofcloud.EyeofcloudDecisionContext, decision: eyeofcloud.EyeofcloudForcedDecision): void为指定的云眼灰度标帜(Feature Flag)AB实验决策上下文设置强制决策getForcedDecision(decisionContext: eyeofcloud.EyeofcloudDecisionContext): eyeofcloud.EyeofcloudForcedDecision | null返回指定的云眼灰度标帜(Feature Flag)AB实验决策上下文的强制决策removeForcedDecision(decisionContext: eyeofcloud.EyeofcloudDecisionContext): boolean删除指定的云眼灰度标帜(Feature Flag)AB实验决策上下文的强制决策removeAllForcedDecisions(): boolean删除所有强制决策
例
获取withEyeofcloud ReactSDKClient 实例以使用trackAPI 的示例:
React
import React from 'react';
import { Button } from 'react-native';
import { withEyeofcloud } from '@eyeofcloud/react-sdk'
class SignupButton extends React.Component {
onPress = () => {
// eyeofcloud is an instance of ReactSDKClient provided by withEyeofcloud
const { eyeofcloud } = this.props
// track API provided by the ReactSDKClient instance
// Notice, no userId or attributes are necessary since
// eyeofcloud has been provided by withEyeofcloud
eyeofcloud.track('signup-clicked')
// rest of click handler
}
render() {
<Button onPress={this.onPress}>
Signup
</Button>
}
}
const WrappedSignupButton = withEyeofcloud(SignupButton)从createInstance中获取ReactSDKClient实例的例子
React
import React from 'react';
import { View, Button } from 'react-native';
import {
createInstance,
EyeofcloudProvider,
} from '@eyeofcloud/react-sdk'
const eyeofcloud = createInstance({
sdkKey: 'CxpSJttFVpz8LiLg8jnZwq',
})
eyeofcloud.setUser({
id: 'user123',
attributes: {
device: 'iPhone',
lifetime: 24738388,
is_logged_in: true,
}
});
eyeofcloud.track('Application Loaded')
function App() {
return (
<EyeofcloudProvider eyeofcloud={eyeofcloud}>
<View>
<Text>Application</Text>
</View>
</EyeofcloudProvider>
);
}
export default App;打字稿类型
ReactSDKClient接口中使用以下类型定义:
UserAttributes : { [name: string]: any }User : { id: string | null, attributes: userAttributes }VariableValuesObject : { [key: string]: boolean | number | string | null }EventTags : { [key: string]: string | number | boolean; }
源
包含 React Native SDK 实现的语言/平台源文件是 index.ts。