Files
copilotkit__copilotkit/showcase/shell-docs/src/lib/analytics-utils.ts
Sam Julien b355a9aec0 feat(shell-docs): port docs telemetry stack
Brings PostHog, GA4, HubSpot, Reo.dev, Scarf, and RB2B into shell-docs
with parity to docs/. Adds the client-side PostHog provider with
session-stitched bootstrap and pageview capture, the AnalyticsClient
wrapper that mounts RB2B + GA4 hooks behind a single client boundary,
the Scarf pixel for OSS attribution, and the HubSpot and Reo.dev
scripts.

Renames POSTHOG_PROJECT_KEY to POSTHOG_KEY across shell and shell-docs
middlewares so the env names match the upstream pattern, and env-drives
POSTHOG_HOST with eu.i.posthog.com as the fallback.
2026-05-06 15:32:31 -07:00

43 lines
1.4 KiB
TypeScript

import { getIntegrations } from "@/lib/registry";
/**
* Normalizes URLs by converting integration routes from /integrations/{integration}/... to /{integration}/...
* Also normalizes trailing slashes (removes them except for root).
* Only normalizes known integration routes to ensure consistency.
*
* Examples:
* - /integrations/langgraph -> /langgraph
* - /integrations/langgraph/shared-state -> /langgraph/shared-state
* - /langgraph/ -> /langgraph
* - /langgraph -> /langgraph (already canonical)
* - /reference -> /reference (not an integration, unchanged)
*/
export function normalizeUrl(url: string): string {
if (!url) return url;
const segments = url.split("/").filter(Boolean);
if (segments[0] === "integrations" && segments.length > 1) {
const integrationId = segments[1];
const knownSlugs = new Set(getIntegrations().map((i) => i.slug));
if (knownSlugs.has(integrationId)) {
const restOfPath = segments.slice(2).join("/");
const normalized = `/${integrationId}${
restOfPath ? "/" + restOfPath : ""
}`;
return normalized === "/" ? "/" : normalized.replace(/\/$/, "");
}
}
return url === "/" ? "/" : url.replace(/\/$/, "");
}
/**
* Normalizes pathnames for analytics tracking.
* Alias for normalizeUrl for clarity in analytics context.
*/
export function normalizePathnameForAnalytics(pathname: string): string {
return normalizeUrl(pathname);
}