ReactSDKClient

云眼About 4 min

ReactSDKClient

本主题介绍 ReactSDKClient,它是一个包装器,如果不想使用组件与云眼灰度发布(特性标帜)AB实验进行交互,则允许访问标准 SDK 方法。

为了提供对 Eyeofcloud灰度发布(特性标帜) 的惯用 React 访问,React SDK 使用钩子(如 useDecision)和组件(如 EyeofcloudProvider)。这些组件和挂钩类似于大多数其他 SDK 中使用的标准方法,例如 Decide 方法。

ReactSDKClient 是一个包装器,如果不想使用组件或钩子与云眼灰度发布(特性标帜)AB实验进行交互,则可以访问标准 SDK 方法。以下部分介绍 ReactSDKClient 提供的方法。

版本

SDK v1.0.0

描述

ReactSDKClient 的实例是用 创建的,它也被传递给具有高阶组件的 prop 中的子组件。createInstance``eyeofcloud``withEyeofcloud

ReactSDKClient 提供了以下方法。请注意,如果实例由 或 如果被调用,则在调用 Eyeofcloud 客户端对象的方法时,无需传入 userId 或属性参数,除非希望使用与 或 不同的 userId 或属性。withEyeofcloud``setUser``EyeofcloudProvider``setUser

提供的接口

  • onReady(opts?: { timeout?: number }): Promise 返回一个 Promise,该 Promise 通过表示数据文件提取过程的对象来实现。参见 JavaScript: onReady
  • user: 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 将事件跟踪到云眼灰度发布(特性标帜)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 为指定的云眼灰度发布(特性标帜)AB实验决策上下文设置强制决策
  • getForcedDecision(decisionContext: eyeofcloud.EyeofcloudDecisionContext): eyeofcloud.EyeofcloudForcedDecision | null 返回指定的云眼灰度发布(特性标帜)AB实验决策上下文的强制决策
  • removeForcedDecision(decisionContext: eyeofcloud.EyeofcloudDecisionContext): boolean 删除指定的云眼灰度发布(特性标帜)AB实验决策上下文的强制决策
  • removeAllForcedDecisions(): boolean 删除所有强制决策

获取 ReactSDKClient 实例以使用 API 的示例: withEyeofcloud``track

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)

Example of getting an instance of ReactSDKClient from :createInstance

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; }

包含 React 实现的语言/平台源文件是 index.tsopen in new window

Last update:
Contributors: “zhangweixue”,zhangweixue