-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathanalytics.ts
More file actions
22 lines (19 loc) · 746 Bytes
/
Copy pathanalytics.ts
File metadata and controls
22 lines (19 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"use client";
import { useCallback } from "react";
import { usePostHog } from "@posthog/next";
// Same gate as the provider in layout.tsx: when the key is absent the
// provider isn't mounted, so capture calls must no-op instead of warning.
const ENABLED = Boolean(process.env.NEXT_PUBLIC_POSTHOG_KEY);
/** Returns a stable `track(event, properties)` that no-ops when PostHog
* isn't configured. Event names are kebab-case per the SQLR-36 spec
* (`docs-search-query`, `docs-helpful-vote`). */
export function useTrack() {
const posthog = usePostHog();
return useCallback(
(event: string, properties?: Record<string, unknown>) => {
if (!ENABLED) return;
posthog?.capture(event, properties);
},
[posthog],
);
}