初始化开发工具包

云眼About 8 min

初始化开发工具包

如何在应用程序中初始化云眼灰度发布(特性标帜)AB实验 React SDK。

使用该方法初始化 React SDK 并实例化 ReactSDKClient 类的实例。每个客户端对应于表示特定环境的项目状态的数据文件。createInstance

版本

SDK v1.0.0 及更高版本

描述

该方法接受配置对象来配置云眼灰度发布(特性标帜)AB实验。createInstance

某些参数是可选的,因为 SDK 提供默认实现,但开发者可能希望为生产环境覆盖这些参数。例如,开发者可能希望覆盖这些内容,以设置错误处理程序记录器来捕获问题,设置事件调度程序来管理网络调用,以及设置用户配置文件服务以确保粘性分桶。

参数

该表列出了 React 中的必需参数和可选参数(通过传入 Config 对象创建)。类由这些字段组成)。

参数

类型

描述

数据文件
可选

字符串

表示项目的 JSON 字符串。必须提供 SDK 密钥或数据文件(或两者)。

sdk密钥
可选

字符串

与项目中的环境关联的键。必须至少提供一个 sdkKey 或数据文件。

事件调度程序
可选

对象

用于管理网络调用的事件处理程序。

记录器可选

对象

用于记录问题的记录器实现。

错误处理程序
可选

对象

用于处理错误的错误处理程序对象。

用户配置文件服务
可选

对象

用户配置文件服务。

数据文件选项
可选

对象

具有用于自动数据文件管理的配置的对象。可以具有(布尔值)、(字符串)和(数字)属性。autoUpdate``urlTemplate``updateInterval

默认决定选项
可选

数组

Array of EyeofcloudDecideOption enums.使用此参数构造 Eyeofcloud 客户端时,它会设置默认的 decide 选项,这些选项将应用于在 Eyeofcloud 客户端生存期内进行的所有 Decision(决策)调用。还可以将选项传递给各个 Decide 方法(不覆盖默认值)。有关决定选项的详细信息,请参阅云眼决策选项

返回

实例化云眼灰度发布(特性标帜)AB实验类的实例。

在 React SDK 中,你可以提供其中之一或两者兼而有之。 sdkKey``datafile

  • 仅使用数据文件初始化时,SDK 将使用给定的数据文件。
  • 仅使用 SDK 密钥初始化时,将自动下载数据文件。
  • 同时使用 SDK 密钥和数据文件进行初始化时,SDK 将使用给定的数据文件启动,然后在后台下载最新版本的数据文件。

使用数据文件实例化

首先,通过在 Web 应用程序中包含以下脚本,从我们的服务器获取数据文件的副本。此脚本在窗口变量 上添加数据文件。window.eyeofcloudDatafile

.HTML

<script src="https://cdn.eyeofcloud.com/datafiles/<Your_SDK_Key>.json/tag.js"></script>

拥有数据文件后,可以实例化云眼客户端。

React

import * as eyeofcloudReactSDK from '@eyeofcloud/react-sdk'; // Instantiate an Eyeofcloud client const eyeofcloud = eyeofcloudReactSDK.createInstance({ datafile: window.eyeofcloudDatafile, })

使用开发工具包密钥实例化

若要使用 SDK 密钥进行实例化,请从项目的设置页获取 SDK 密钥,然后在传递给该方法的选项对象中作为字符串属性传递。sdkKey``createInstance

React

import * as eyeofcloudReactSDK from '@eyeofcloud/react-sdk'; // Instantiate an Eyeofcloud client const eyeofcloud = eyeofcloudReactSDK.createInstance({ sdkKey: '<Your_SDK_Key>', });

当您提供 时,SDK 实例将下载与该关联的数据文件。下载完成后,SDK 实例会自行更新以使用下载的数据文件。此初始化方法依赖于持续的互联网连接(相比之下,在 React Native SDK 中,即使应用程序处于离线状态,您也可以使用 SDK 密钥进行初始化。sdkKey``sdkKey

与客户端和用户一起呈现EyeofcloudProvider

要在应用程序中使用 React SDK 组件,请将 an 呈现为根应用程序组件的父级。提供 Eyeofcloud 实例作为 prop 和对象:EyeofcloudProvider``eyeofcloud``user

React

import { EyeofcloudProvider, createInstance } from '@eyeofcloud/react-sdk' const eyeofcloud = createInstance({ datafile: window.datafile, }); class AppWrapper extends React.Component { render() { return ( <EyeofcloudProvider eyeofcloud={eyeofcloud} user={{id: '<Your_User_Id>'}}> <App /> </EyeofcloudProvider> ); } }

Notes

Customize datafile management behavior

To customize datafile management behavior, provide a object property inside the object passed to . The table lists the supported customizable options.datafileOptions``options``createInstance

Option

Type

Explanation

autoUpdate

boolean

When true, and was provided in options, automatic updates are enabled on this instance. The default value is .sdkKey``createInstance``false

updateInterval

number

When automatic updates are enabled, this controls the update interval. The unit is milliseconds. The minimum allowed value is 1000 (1 second). The default value is 300000 milliseconds (5 minutes).

urlTemplate

string

A format string used to build the URL from which the SDK will request datafiles. Instances of will be replaced with the . When not provided, the SDK will request datafiles from the Eyeofcloud CDN.%s``sdkKey

The following example shows how to customize datafile management behavior.

React

import { createInstance } from '@eyeofcloud/react-sdk'; const eyeofcloud = createInstance({ sdkKey: '<Your_SDK_Key>', datafileOptions: { autoUpdate: true, updateInterval: 600000, // 10 minutes in milliseconds urlTemplate: 'http://localhost:5000/datafiles/%s.json', }, });

Considerations when using development mode

If you have created your React application using Create React Appopen in new window or are using a different type of development environment that uses hot reloading, there is a possibility to experience unusual behavior.

When running your application in development mode with hot reloading, for example, when using npm startopen in new window , you may see that the Eyeofcloud 灰度发布(特性标帜) SDK is initialized more than once. This may cause inconsistent behavior, such as the page re-rendering multiple times or users being bucketed into incorrect variations.

This is not a problem with the Eyeofcloud灰度发布(特性标帜) React SDK. Instead, this happens when you wrap your application in , which purposely renders the application twice in development mode to detect potential problems. To avoid this, please disable the strict mode for the top-level component and move it to the lower-level components.React.StrictMode

When building your app for production use and hosting it on a web server, the Eyeofcloud灰度发布(特性标帜) React SDK is not initialized more than once.npm run build

Source files

The language/platform source files containing the implementation for React SDK are at index.tsopen in new window.

Updated 2 months ago


[

Install SDK

](/experimentation/v4.0.0-full-stack/docs/install-sdk-react)[

Example usage

](/experimentation/v4.0.0-full-stack/docs/example-usage-react)

Did this page help you?

Yes

No

创建用户上下文

介绍创建用户上下文方法,该方法为云眼灰度发布(特性标帜)AB实验中的标帜决策和事件创建用户上下文。

此方法的目的是创建用户并设置用户上下文一次,因此不必在每次做出标帜决策或跟踪事件时都指定用户。可以定义多个用户上下文。系统将用户上下文作为运行时对象返回,否则不会持久化。

版本

1.0.0-beta 或更高版本

描述

此调用为标帜决策和事件创建用户上下文。可以在 Eyeofcloud 客户端实例上成功调用此方法,甚至在完全配置实例之前也是如此。

参数

下表列出了必需参数和可选参数:

参数

类型

描述

(必选)用户 ID

字符串

用户的 ID。

属性
可选

Map

自定义键值字符串对的映射,指定系统用户用于受众群体定位的用户的属性。有关更多详细信息,请参阅以下部分。

受众群体属性

为用户设置自定义受众群体属性,可以使用这些属性来定位受众群体。可以将字符串、数字、布尔值和 nil 作为自定义用户属性值传递。如果要根据他们使用的应用程序变体定位访问群体,还可以传入格式为语义变体的open in new window字符串,然后在 Eyeofcloud 应用中定义受众条件。version

🚧 重要

在访问群体评估期间,如果没有为给定的访问群体条件传递有效的属性值(例如,如果在受众群体条件需要布尔值时传递字符串,或者忘记传递值),则系统会跳过该条件。发生这种情况时,SDK 日志会包含警告。

返回

返回一个 EyeofcloudUserContext 对象。有关详细信息,请参阅云眼用户上下文

Dart

// option 1: create a user, then set attributes var user = await flutterSDK.createUserContext("user123"); var attributes = <String, dynamic>{}; attributes["is_logged_in"] = false; attributes["app_version"] = "1.3.2"; user!.setAttributes(attributes); // option 2: pass attributes when creating the user var attributes = <String, dynamic>{}; attributes["is_logged_in"] = false; attributes["app_version"] = "1.3.2"; var eyeofcloudUserContext = await eyeofcloudClient.createUserContext("user123", attributes);

参见

云眼用户上下文

源文件

包含 Flutter SDK for Android Eyeofcloudopen in new window 和 Swift 实现的语言/平台源文件.java为 EyeofcloudClient.swiftopen in new window

Last update:
Contributors: “zhangweixue”