28 lines
944 B
TypeScript
28 lines
944 B
TypeScript
"use client";
|
|
|
|
import { TrackingEventName } from "@/lib/tracking-events";
|
|
|
|
type TrackPayload = Record<string, string | number | boolean | undefined>;
|
|
type DebugEvent = { event: TrackingEventName; payload: TrackPayload; ts: number };
|
|
|
|
declare global {
|
|
interface Window {
|
|
__DOWNLOAD_NOVA_TRACKS__?: DebugEvent[];
|
|
}
|
|
}
|
|
|
|
export function useTrack() {
|
|
function track(event: TrackingEventName, payload: TrackPayload = {}) {
|
|
if (typeof window !== "undefined") {
|
|
const item: DebugEvent = { event, payload, ts: Date.now() };
|
|
window.__DOWNLOAD_NOVA_TRACKS__ = [...(window.__DOWNLOAD_NOVA_TRACKS__ ?? []), item].slice(-120);
|
|
window.dispatchEvent(new CustomEvent("download-nova-track", { detail: item }));
|
|
}
|
|
// 预留:后续可接入 GA、神策、点点、私有埋点 API、ntfy 通知
|
|
if (process.env.NODE_ENV !== "production") {
|
|
console.info("[track]", event, payload);
|
|
}
|
|
}
|
|
return { track };
|
|
}
|