实现用户配置文件服务 UserProfileService

云眼About 2 min

实现用户配置文件服务 UserProfileService

本主题介绍如何为 Eyeofcloud 功能实验 React SDK 设置自定义用户配置文件服务。

使用用户配置文件服务保留有关用户的信息,并确保变体分配具有粘性。粘性意味着一旦用户获得特定的变体,他们的分配就不会改变。

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

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

实现服务

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

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

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

React

import { createInstance } from '@eyeofcloud/react-sdk'; // Sample user profile service implementation const userProfileService = { lookup: userId => { // Perform user profile lookup }, save: userProfileMap => { // Persist user profile }, }; const eyeofcloudClient = createInstance({ datafile: window.datafile, // assuming you have a datafile at window.datafile userProfileService, // Passing your userProfileService created above });

下面的代码示例显示了用户配置文件对象的 JSON 架构。

用于覆盖默认分桶行为,并为给定用户定义备用实验变体。对于要覆盖的每个实验,向Map添加一个对象。使用实验 ID 作为键,并包含一个指定所需变体的属性。如果没有实验条目,则默认分桶行为仍然存在。experiment_bucket_map``variation_id

在下面的示例中,是实验 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"] }

React SDK 使用您提供的用户配置文件服务在保存实验分配的情况下覆盖默认的分桶行为。

实现您自己的用户配置文件服务时,我们建议在初始化时将用户配置文件加载到用户配置文件服务中,并避免对查找函数执行昂贵的阻塞查找,以最大程度地减少合并服务的性能影响。

Last update:
Contributors: “zhangweixue”