跳到正文
Indiana
控制台
开发文档
章节目录 / Feature、实验与事件

Feature、实验与事件

Feature 开关与实验、事件采集、网页跳转与可视化实验。

API v1更新于

读取 Feature 开关

flags.tsTypeScript
import { createIndiana } from "@tinkerlandai/indiana-sdk"; const indiana = createIndiana({  baseUrl: "https://api.tinker.cash",  scope: { projectId: "<APP_UUID>", environment: "sandbox" },}); export async function loadFlags(  customerToken: string,  customer: { id: string; externalUserId: string | null },) {  indiana.identify(customerToken);  const identity = { id: customer.id, externalUserId: customer.externalUserId };  const flags = await indiana.features({ attributes: { ...identity, country: "CA" } });  const enabled = flags.getFeatureValue("new-checkout", false);  const unsubscribe = flags.subscribe(() => console.log(flags.isOn("new-checkout")));  indiana.track("Checkout Viewed", { source: "pricing" });  await indiana.flush();  return { enabled, stop: () => (unsubscribe(), flags.destroy()) };}
  • 传入 id(Customer UUID)和 externalUserId,同一用户在各处得到一致的实验分组。
  • setAttributes() 会替换全部属性,更新时保留这两个字段。
  • 后端可以用 getFeatures 或 evaluateFeatures 读取和求值(需要 features:read)。

上报事件

track(name, properties) 记录行为事件,flush() 立即发送。实验曝光和 Feature 使用会自动记录;可在 Console 中按环境开关每类采集。

网页实验

在页面中加载 URL 跳转实验和可视化实验,不需要凭据。环境 UUID 可从 Console 的实验页面复制。

experiments.tsTypeScript
import { createPageExperiments } from "@tinkerlandai/indiana-sdk/page";import { createVisualExperiments } from "@tinkerlandai/indiana-sdk/visual"; // The environment UUID is public; it selects published experiments and authenticates nothing.const redirects = createPageExperiments({ environmentId: "<ENVIRONMENT_UUID>" });const visual = createVisualExperiments({ environmentId: "<ENVIRONMENT_UUID>" }); export async function startExperiments() {  await Promise.all([redirects.start(), visual.start()]);}

相关接口

Feature、求值与事件 (3)
getFeatures, evaluateFeatures, trackEvents

相关:路径、凭据与权限见全部接口