Files
copilotkit__copilotkit/docs/lib/providers/copy-tracker.tsx
Benjamin Taylor 68561c0cb8 feat(docs): track cli command copies via global writeText hook
CopyTracker monkey-patches navigator.clipboard.writeText once at app
boot to fire PostHog cli_command_copied for every programmatic copy —
covering Fumadocs' built-in code-block button, custom-code-block,
code-showcase, framework-overview, and any future copy widget without
per-component instrumentation. Reo's own writeText patch continues to
function alongside this one.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 16:26:17 -05:00

31 lines
877 B
TypeScript

"use client";
import { useEffect } from "react";
import { usePostHog } from "posthog-js/react";
import { trackCommandCopy } from "@/lib/track-command-copy";
export function CopyTracker() {
const posthog = usePostHog();
useEffect(() => {
if (typeof navigator === "undefined" || !navigator.clipboard?.writeText) return;
const original = navigator.clipboard.writeText.bind(navigator.clipboard);
navigator.clipboard.writeText = async function (text: string) {
try {
trackCommandCopy(posthog, {
command: text,
location: typeof window !== "undefined" ? window.location.pathname : undefined,
});
} catch {
// Never let analytics break the underlying copy.
}
return original(text);
};
return () => {
navigator.clipboard.writeText = original;
};
}, [posthog]);
return null;
}