Funnel 服务端决策指南
在可信后端用 SDK 调用 Remote Funnel,处理等待与重试,并回报采纳。
API v1
安装与配置#
1npm install --save-exact @tinkerlandai/longplay-sdk@latest
在 Console 的 接入 → 接入概览 页面顶部「接入范围」中复制「服务地址」(API origin)与「App ID」(App UUID);Funnel UUID 来自已发布的目标 Funnel。API origin 不带 /api/v1。
1LONGPLAY_API_URL=https://api.longplayai.com2LONGPLAY_SERVER_KEY=<SERVER_KEY>3LONGPLAY_PROJECT_ID=<APP_UUID>4LONGPLAY_ENVIRONMENT=sandbox5LONGPLAY_FUNNEL_ID=<PUBLISHED_FUNNEL_UUID>
开始之前#
- 在 Console 发布目标 Funnel,并把求值模式设为 Remote。服务端决策只支持 Remote 模式。
- Server Key 需要 customers:write, sessions:create, funnels:run 权限。
- Run 使用启动它的 Key 的权限:Paywall 和 Offering 步骤需要 entitlements:read;在 Paywall 购买还需要 billing:write(报价、确认和取消)和 billing:read(读取结算与恢复结果);领取或展示 Coupon 需要 offers:resolve。
- 在 Console 创建 Key 时点击「选择 Funnel 启动权限」,会一次选中这些权限:funnels:run、sessions:create、customers:write、entitlements:read、offers:resolve、billing:read、billing:write。
externalUserId 使用后端已验证的用户 ID。attributes 传入本次判断需要的业务属性,Funnel 中的条件可以读取它们。
后端调用#
1import {2 createFunnelCampaignClient,3 createFunnelDecisionClient,4 type LongplayServerOptions,5} from "@tinkerlandai/longplay-sdk/server";6 7const environment = process.env.LONGPLAY_ENVIRONMENT;8if (environment !== "sandbox" && environment !== "live")9 throw new Error("Set LONGPLAY_ENVIRONMENT to sandbox or live");10const options: LongplayServerOptions = {11 baseUrl: process.env.LONGPLAY_API_URL!,12 apiKey: process.env.LONGPLAY_SERVER_KEY!,13 scope: {14 projectId: process.env.LONGPLAY_PROJECT_ID!,15 environment,16 },17};18const funnelId = process.env.LONGPLAY_FUNNEL_ID!;19const campaign = createFunnelCampaignClient({ ...options, funnelId });20const decisions = createFunnelDecisionClient({ ...options, funnelId });21 22export async function verifyFunnel() {23 const config = await decisions.configuration();24 if (config.evaluationMode !== "remote")25 throw new Error("Publish a Remote Funnel for server-side Decide");26 return config.versionId;27}28 29export async function decideForUser(input: {30 verifiedUserId: string;31 requestId: string;32 attributes: Record<string, string | number | boolean>;33}) {34 return campaign.decide({35 requestId: input.requestId,36 customer: { type: "externalUserId", value: input.verifiedUserId },37 attributes: input.attributes,38 });39}40 41export async function reportAdoption(input: {42 decisionId: string;43 eventId: string;44 occurredAt: string;45 reference: string;46}) {47 return decisions.reportUsed(input);48}
- 启动时调用一次
verifyFunnel,确认 Funnel 已发布且为 Remote 模式。 campaign.decide 返回决策结果;decision.value 的内容由你在 Funnel 中配置的输出决定。- 发送前保存
requestId;结果未知时用同一个 requestId 和输入重试。 - 最多等待约 30 秒。超时会抛出
FunnelDecisionTimeoutError,用其中的 decisionId 调用 decisions.read 读取结果。
回报采纳#
用户按决策结果行动(如完成订阅)后,调用 decisions.reportUsed 上报(示例中封装为 reportAdoption),用于统计效果。保存 eventId,重试时使用同一个值。
相关 SDK 方法| 方法 | 返回与恢复 |
|---|
| campaign.decide(input) | 等待决策完成;返回 FunnelDecision。结果未知时重放原 requestId 和完整输入。 |
| decisions.read(decisionId) | 读取已接受决策的 pending、completed 或 failed 状态。 |
| decisions.reportUsed(input) | 返回 caller_reported 回执;重试沿用原 eventId 和事实。 |
相关:HTTP 报文与响应