40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
/**
|
||
* 客户端上报用户旅程埋点(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 }
|
||
}
|