ReactSDKClient
2023年5月11日大约 4 分钟
ReactSDKClient
本主题介绍 ReactSDKClient,它是一个包装器,如果不想使用组件与云眼灰度标帜(Feature Flag)AB实验进行交互,则允许访问标准 SDK 方法。
为了提供对 Eyeofcloud灰度标帜(Feature Flag) 的惯用 React 访问,React SDK 使用钩子(如 useDecision)和组件(如 EyeofcloudProvider)。这些组件和挂钩类似于大多数其他 SDK 中使用的标准方法,例如 Decide 方法。
ReactSDKClient 是一个包装器,如果不想使用组件或钩子与云眼灰度标帜(Feature Flag)AB实验进行交互,则可以访问标准 SDK 方法。以下部分介绍 ReactSDKClient 提供的方法。
版本
SDK v1.0.0
描述
ReactSDKClient 的实例是用createInstance创建的,它也被传递给具有withEyeofcloud高阶组件的eyeofcloudprop 中的子组件。
ReactSDKClient 提供了以下方法。请注意,如果实例由withEyeofcloud或如果setUser被调用,则在调用 Eyeofcloud 客户端对象的方法时,无需传入 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 实例以使用track API 的示例:
React
import React from 'react';
import { withEyeofcloud } from '@eyeofcloud/react-sdk'
class SignupButton extends React.Component {
onClick = () => {
// 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 onClick={this.onClick}>
Signup
</button>
}
}
const WrappedSignupButton = withEyeofcloud(SignupButton)从createInstance中获取ReactSDKClient实例的例子
React
import React from 'react';
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}>
<div className="App">Application</div>
</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; }