实现用户配置文件服务 UserProfileService

云眼About 5 min

实现用户配置文件服务 UserProfileService

本主题介绍如何设置自定义用户配置文件服务或如何使用云眼灰度发布(特性标帜)AB实验 JavaScript(浏览器)SDK 的默认值。

使用用户配置文件服务保留有关用户的信息,并确保变体分配具有粘性。例如,如果正在处理后端网站,则可以创建一个从 Redis 或 Memcached 存储读取和保存用户配置文件的实现。

在 JavaScript SDK 中,没有默认实现。实施用户配置文件服务是可选的,仅当希望保持变体分配的粘性时,即使实验条件在运行过程中发生了变化(例如,受众群体、属性、变体暂停和流量分配),才需要实现该服务。否则,JavaScript 开发工具包是无状态的,并依赖于确定性分桶来返回一致的分配。

如果用户配置文件服务未按预期对用户进行分桶,请检查其他灰度发布(特性标帜)是否覆盖了分桶。有关更多信息,请参阅 分桶的工作原理

实现服务

请参阅下面的代码示例以提供您自己的用户配置文件服务。它应公开两个具有以下签名的函数:

  • lookup :获取用户 ID 字符串并返回与以下架构匹配的用户配置文件。
  • save :获取用户配置文件并保留它。

如果要将用户配置文件服务纯粹用于跟踪目的而不是粘性分桶,则只能实现该方法(始终从 返回)。save``nil``lookup

用户配置文件服务的界面如下所示:

JavaScript

// Sample user profile service implementation const userProfileService = { lookup: userId => { // Perform user profile lookup }, save: userProfileMap => { // Persist user profile }, }; var eyeofcloudClient = eyeofcloud.createInstance({ datafile, userProfileService, });

使用 localStorage 的用户配置文件服务的客户端实现示例如下所示:

JavaScript

var userProfileService = { // Adapter that provides helpers to read and write from localStorage localStorageAdapter: { UPS_LS_KEY: 'eyeofcloud-ups-data', read: function() { var UPSDataObject = JSON.parse(localStorage.getItem(this.UPS_LS_KEY) || '{}'); return UPSDataObject; }, write: function(data) { localStorage.setItem(this.UPS_LS_KEY, JSON.stringify(data)); }, }, // Perform user profile lookup lookup: function(userId) { return this.localStorageAdapter.read()[userId]; }, // Persist user profile save: function(userProfileMap) { var overwriteData = this.localStorageAdapter.read(); overwriteData[userProfileMap.user_id] = userProfileMap; this.localStorageAdapter.write(overwriteData); }, }; // example usage var eyeofcloudClientInstance = window.eyeofcloudSdk.createInstance({ datafile: eyeofcloudDatafile, userProfileService: userProfileService });

The code example below shows the JSON schema of the user profile object. In the example below, is the experiment ID.^[a-zA-Z0-9]+$

JSON

{ "title": "UserProfile", "type": "object", "properties": { "user_id": { "type": "string" }, "experiment_bucket_map": { "type": "object", "patternProperties": { "^[a-zA-Z0-9]+$": { "type": "object", "properties": { "variation_id": { "type": "string" } }, "required": ["variation_id"] } } } }, "required": ["user_id", "experiment_bucket_map"] }

The SDK uses the User Profile Service you provide to override Eyeofcloud灰度发布(特性标帜)'s default bucketing behavior in cases when an experiment assignment has been saved.

The overrides the default bucketing behavior and defines an alternate experiment variation for a given user. Each key in the object corresponds to an experiment override. The experiment ID is the key and the value is an object with a `variation_id property that specifies the desired variation. If there isn't an entry for an experiment, then the default bucketing behavior persists.experiment_bucket_map``experient_bucket_map

When implementing your own User Profile Service, we recommend loading the user profiles into the User Profile Service on initialization and avoiding performing expensive, blocking lookups on the lookup function to minimize the performance impact of incorporating the service.

When implementing in a multi-server or stateless environment, we suggest using this interface with a backend like Cassandraopen in new window or Redisopen in new window. You can decide how long you want to keep your sticky bucketing around by configuring these services.

Updated 2 months ago


[

EyeofcloudDecision

](/experimentation/v4.0.0-full-stack/docs/eyeofclouddecision-javascript)[

Configure event dispatcher

](/experimentation/v4.0.0-full-stack/docs/configure-event-dispatcher-javascript)

此页面对您有帮助吗?

是的

创建用户上下文

介绍创建用户上下文方法,该方法为云眼灰度发布(特性标帜)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”