's'
This commit is contained in:
27
hooks/use-track.ts
Normal file
27
hooks/use-track.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
"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 };
|
||||
}
|
||||
Reference in New Issue
Block a user