Files
copilotkit__copilotkit/showcase/shell-docs/src/lib/track-command-copy.ts
Sam Julien e1b3e363fa feat(shell-docs): track CLI command copies via global writeText hook
Mirrors upstream #4643 + #4646 follow-ups. New trackCommandCopy helper
infers install_type from the leading token (covers npx/pnpm/pip/uv/
docker/curl/brew/helm/kubectl/make/bash/sh, falls back to "code"), and
the CopyTracker provider monkey-patches navigator.clipboard.writeText
once at app boot so every programmatic copy fires cli_command_copied
without per-component instrumentation. Preserves upstream's chained-
wrapper pattern so it coexists with Reo's writeText patch.

Final event shape is { install_type, location? } — the command body
and product props from the original draft were dropped upstream
(commit 0b7b3c77c) before merge.
2026-05-06 15:32:33 -07:00

51 lines
1.0 KiB
TypeScript

import type { PostHog } from "posthog-js";
const KNOWN_INSTALL_TYPES = [
"npx",
"npm",
"pnpm",
"yarn",
"bun",
"pip",
"uv",
"poetry",
"cargo",
"go",
"docker",
"curl",
"brew",
"helm",
"kubectl",
"make",
"bash",
"sh",
] as const;
export type InstallType = (typeof KNOWN_INSTALL_TYPES)[number] | "code";
function inferInstallType(command: string): InstallType {
const firstToken = command.trim().split(/\s+/)[0]?.toLowerCase();
if (!firstToken) return "code";
return (KNOWN_INSTALL_TYPES as readonly string[]).includes(firstToken)
? (firstToken as InstallType)
: "code";
}
export type TrackCommandCopyArgs = {
command: string;
location?: string;
};
export function trackCommandCopy(
posthog: PostHog | undefined,
{ command, location }: TrackCommandCopyArgs,
) {
if (!posthog) return;
const trimmed = command.trim();
if (!trimmed) return;
posthog.capture("cli_command_copied", {
install_type: inferInstallType(trimmed),
...(location ? { location } : {}),
});
}