mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
5fd94e1365
Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: Benjamin Canac <canacb1@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
28 lines
977 B
TypeScript
28 lines
977 B
TypeScript
import { track } from '@vercel/analytics'
|
|
|
|
// Sliders and curve drags stream through the theme setters at drag frequency.
|
|
// One clock per event and setting, shared by every caller, so a drag is one
|
|
// event no matter which component reports it, and a different interaction
|
|
// inside the same two seconds is still its own event. Client only: the
|
|
// module scope would otherwise be shared across SSR requests.
|
|
const trackedAt = new Map<string, number>()
|
|
|
|
/** `track`, at most once per two-second burst of the same event and setting. */
|
|
function trackThrottled(...args: Parameters<typeof track>) {
|
|
if (import.meta.server) return
|
|
const [event, properties] = args
|
|
const key = `${event}:${(properties as Record<string, unknown> | undefined)?.setting ?? ''}`
|
|
const last = trackedAt.get(key)
|
|
if (!last || Date.now() - last > 2000) {
|
|
trackedAt.set(key, Date.now())
|
|
track(...args)
|
|
}
|
|
}
|
|
|
|
export function useAnalytics() {
|
|
return {
|
|
track,
|
|
trackThrottled
|
|
}
|
|
}
|