运行 Funnel
从后端启动 Run 并在浏览器展示,使用 Campaign、外部信号与 Funnel 命令。
API v1更新于
选择接入方式#
接入方式| 需求 | 使用 |
|---|
| 向用户展示已发布的 Funnel 页面 | createFunnelStartClient + showFunnelRun |
| 后端只需要一个结果(如推荐套餐) | createFunnelCampaignClient().decide |
| 按频率限制展示活动 | createFunnelCampaignClient().start |
| 把外部事件交给等待中的 Funnel | createFunnelOrchestrationClient().signal |
接入方式
向用户展示已发布的 Funnel 页面
- 使用
createFunnelStartClient + showFunnelRun
后端只需要一个结果(如推荐套餐)
- 使用
createFunnelCampaignClient().decide
按频率限制展示活动
- 使用
createFunnelCampaignClient().start
把外部事件交给等待中的 Funnel
- 使用
createFunnelOrchestrationClient().signal
启动并展示 Funnel#
后端启动 Run 并签发 Customer Token,浏览器用这两个值展示 Funnel。后端 Key 需要 funnels:run、sessions:create 和 customers:write。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。
server/funnel.tsTypeScript 1import { createFunnelStartClient, createLongplayServer } from "@tinkerlandai/longplay-sdk/server";2 3const options = {4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" as const },7};8const funnels = createFunnelStartClient(options);9const longplay = createLongplayServer(options);10 11export async function startOnboarding(verifiedUserId: string, requestId: string) {12 const started = await funnels.start({13 funnelId: process.env.LONGPLAY_FUNNEL_ID!,14 requestId, // persisted; reuse it with the same input after an unknown result15 customer: { type: "externalUserId", value: verifiedUserId },16 locale: "en",17 parameters: {18 source: "signup",19 // Required when the Funnel shows a Paywall.20 commerce: { provider: "stripe", currency: "USD" },21 },22 });23 const customerSession = await longplay.createCustomerSession(started.customerId!);24 return { runToken: started.runToken, started, customerSession };25}
Funnel 有 Paywall 时,启动必须传 parameters.commerce,写明 provider 和 currency。缺少时请求返回 400,不会创建 Run。
- 保存
requestId;响应丢失时用同一个值重试,会得到同一个 Run。 - 已暂停的 Funnel 以 409
CONFLICT 拒绝新的启动;显示为暂时不可用。用原 requestId 重放仍返回原 Run。 - Funnel 中的支付和发券在服务端完成,浏览器关闭不影响已经开始的处理。
按频率展示活动#
start 在冷却时间内或已有进行中的 Run 时返回 suppressed: true 和下次可展示的时间 retryAt。
1import { createFunnelCampaignClient } from "@tinkerlandai/longplay-sdk/server";2 3const campaign = createFunnelCampaignClient({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7 funnelId: process.env.LONGPLAY_FUNNEL_ID!,8});9 10export async function maybeShowCampaign(verifiedUserId: string, requestId: string) {11 const result = await campaign.start({12 requestId,13 customer: { type: "externalUserId", value: verifiedUserId },14 locale: "en",15 parameters: { placement: "home" },16 cooldownSeconds: 86400,17 });18 if ("suppressed" in result) return { show: false as const, retryAt: result.retryAt };19 return { show: true as const, runToken: result.runToken };20}
相关:只获取结果:Funnel 服务端决策
发送外部事件#
Funnel 停在等待节点时,后端提交外部事件;eventId 用于去重。
eventName 必须与等待节点里的事件名完全一致(区分大小写)。事件名以字母开头,只含字母、数字、_、. 和 -,最长 64 个字符,例如 kyc_approved 或 kyc.approved。
1import { createFunnelOrchestrationClient } from "@tinkerlandai/longplay-sdk/server";2 3const orchestration = createFunnelOrchestrationClient({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});8 9// eventId is persisted with your business event, so retries are deduplicated.10export async function reportKycApproved(runId: string, eventId: string) {11 const { accepted } = await orchestration.signal({12 runId,13 nodeId: "wait_for_kyc",14 eventName: "kyc_approved",15 eventId,16 data: { level: "full" },17 });18 return accepted ? orchestration.readResults(runId) : null;19}
在浏览器中求值(Local 模式)#
发布为 Local 模式的 Funnel 可以在浏览器中直接启动,用户不必等待服务端计算。规则对所有人可见,涉及资格或价格的判断请使用默认的服务端模式。
local-funnel.tsTypeScript 1import {2 createConfigurationSync,3 createLocalFunnelClient,4 showFunnelRun,5} from "@tinkerlandai/longplay-sdk";6 7const baseUrl = "https://api.longplayai.com";8const configuration = createConfigurationSync({9 origin: baseUrl,10 scope: { projectId: "<APP_UUID>", environment: "sandbox", environmentId: "<ENVIRONMENT_UUID>" },11});12 13export async function showLocalFunnel(14 container: HTMLElement,15 customerToken: string,16 requestId: string,17) {18 const flow = await createLocalFunnelClient({19 baseUrl,20 configuration,21 funnelId: "<PUBLISHED_FUNNEL_UUID>",22 credential: customerToken,23 });24 const started = await flow.start({ requestId, locale: "en", parameters: {} });25 // Keep started.runToken to restore the same Run later with flow.restore(runToken).26 return showFunnelRun(container, { client: started.controller });27}
通过 HTTP 调用#
所有 Funnel 命令都发送到 POST /api/v1/funnels,由请求体的 operation 选择。
相关:全部 Funnel 命令