openclaw/apps/web/app/components/posthog-provider.tsx
kumarabhirup 38b062a71e
chore: fix test mocks, suppress cron pageview tracking, and minor docs
Add workspace mock to agent-runner tests, skip PostHog pageviews on cron paths to reduce noise, document web-chat storage path in CRM skill, and include cursor plans and build info.
2026-03-05 21:20:35 -08:00

70 lines
1.7 KiB
TypeScript

"use client";
import posthog from "posthog-js";
import { PostHogProvider as PHProvider } from "posthog-js/react";
import { useEffect, useRef } from "react";
import { usePathname, useSearchParams } from "next/navigation";
const POSTHOG_KEY = process.env.NEXT_PUBLIC_POSTHOG_KEY || "";
const POSTHOG_HOST = "https://us.i.posthog.com";
let initialized = false;
function initPostHog(anonymousId?: string) {
if (initialized || !POSTHOG_KEY || typeof window === "undefined") return;
posthog.init(POSTHOG_KEY, {
api_host: POSTHOG_HOST,
capture_pageview: false,
capture_pageleave: true,
persistence: "memory",
autocapture: false,
disable_session_recording: true,
person_profiles: "identified_only",
bootstrap: anonymousId
? { distinctID: anonymousId, isIdentifiedID: false }
: undefined,
});
initialized = true;
}
function PageviewTracker() {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
if (!initialized) return;
const wsPath = searchParams?.get("path") ?? "";
if (wsPath.startsWith("~cron")) return;
const url = pathname + (searchParams?.toString() ? `?${searchParams.toString()}` : "");
posthog.capture("$pageview", { $current_url: url });
}, [pathname, searchParams]);
return null;
}
export function PostHogProvider({
children,
anonymousId,
}: {
children: React.ReactNode;
anonymousId?: string;
}) {
const initRef = useRef(false);
useEffect(() => {
if (initRef.current) return;
initRef.current = true;
initPostHog(anonymousId);
}, [anonymousId]);
if (!POSTHOG_KEY) return <>{children}</>;
return (
<PHProvider client={posthog}>
<PageviewTracker />
{children}
</PHProvider>
);
}