Feature、实验与事件
Feature 开关与实验、事件采集、网页跳转与可视化实验。
API v1更新于
读取 Feature 开关
1import { createIndiana } from "@tinkerlandai/indiana-sdk";2 3const indiana = createIndiana({4 baseUrl: "https://api.tinker.cash",5 scope: { projectId: "<APP_UUID>", environment: "sandbox" },6});7 8export async function loadFlags(9 customerToken: string,10 customer: { id: string; externalUserId: string | null },11) {12 indiana.identify(customerToken);13 const identity = { id: customer.id, externalUserId: customer.externalUserId };14 const flags = await indiana.features({ attributes: { ...identity, country: "CA" } });15 const enabled = flags.getFeatureValue("new-checkout", false);16 const unsubscribe = flags.subscribe(() => console.log(flags.isOn("new-checkout")));17 indiana.track("Checkout Viewed", { source: "pricing" });18 await indiana.flush();19 return { enabled, stop: () => (unsubscribe(), flags.destroy()) };20}
- 传入
id(Customer UUID)和 externalUserId,同一用户在各处得到一致的实验分组。 setAttributes() 会替换全部属性,更新时保留这两个字段。- 后端可以用
getFeatures 或 evaluateFeatures 读取和求值(需要 features:read)。
上报事件
track(name, properties) 记录行为事件,flush() 立即发送。实验曝光和 Feature 使用会自动记录;可在 Console 中按环境开关每类采集。
网页实验
在页面中加载 URL 跳转实验和可视化实验,不需要凭据。环境 UUID 可从 Console 的实验页面复制。
experiments.tsTypeScript 1import { createPageExperiments } from "@tinkerlandai/indiana-sdk/page";2import { createVisualExperiments } from "@tinkerlandai/indiana-sdk/visual";3 4// The environment UUID is public; it selects published experiments and authenticates nothing.5const redirects = createPageExperiments({ environmentId: "<ENVIRONMENT_UUID>" });6const visual = createVisualExperiments({ environmentId: "<ENVIRONMENT_UUID>" });7 8export async function startExperiments() {9 await Promise.all([redirects.start(), visual.start()]);10}
相关接口
- Feature、求值与事件 (3)
getFeatures, evaluateFeatures, trackEvents
相关:路径、凭据与权限见全部接口