Files
wzdj/lib/analytics/journey-client.ts
卡若 ea3a589f0b
Some checks failed
CI / notice (push) Has been cancelled
chore: sync docs, gitea wiki assets, templates
Made-with: Cursor
2026-04-14 18:44:44 +08:00

40 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 客户端上报用户旅程埋点WM-O1-KR1-02
* 需在浏览器侧生成稳定的 sessionId 与每条 clientEventId建议 UUID
*/
export type JourneySource = "app" | "h5" | "admin"
export type JourneyEventPayload = {
eventName: string
properties?: Record<string, unknown>
sessionId: string
clientEventId: string
occurredAt?: string
source: JourneySource
}
export async function postJourneyEvents(
events: JourneyEventPayload[],
options?: { anonymousId?: string },
): Promise<{ inserted: number; deduped: number; invalid: number }> {
const res = await fetch("/api/analytics/journey-events", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
body: JSON.stringify({
events,
...(options?.anonymousId ? { anonymousId: options.anonymousId } : {}),
}),
})
const json = (await res.json()) as {
success?: boolean
message?: string
data?: { inserted: number; deduped: number; invalid: number }
}
if (!res.ok || !json.success) {
throw new Error(json.message || "journey ingest failed")
}
return json.data ?? { inserted: 0, deduped: 0, invalid: 0 }
}