mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
68561c0cb8
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>
31 lines
877 B
TypeScript
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;
|
|
}
|