From 00c04f89d379e32bd23c712f5cc3a904262e65d1 Mon Sep 17 00:00:00 2001 From: kumarabhirup Date: Thu, 5 Mar 2026 16:09:06 -0800 Subject: [PATCH] refactor(telemetry): remove server-side anonymous ID in favor of PostHog native tracking Let PostHog generate its own distinct ID client-side instead of deriving one from hostname/username. --- apps/web/app/components/posthog-provider.tsx | 12 +++--------- apps/web/app/layout.tsx | 3 +-- apps/web/lib/telemetry.ts | 20 +++++++------------- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/apps/web/app/components/posthog-provider.tsx b/apps/web/app/components/posthog-provider.tsx index 7bf47f97465..582d79ba7cb 100644 --- a/apps/web/app/components/posthog-provider.tsx +++ b/apps/web/app/components/posthog-provider.tsx @@ -10,15 +10,11 @@ const POSTHOG_HOST = "https://us.i.posthog.com"; let initialized = false; -function initPostHog(anonymousId: string) { +function initPostHog() { if (initialized || !POSTHOG_KEY || typeof window === "undefined") return; posthog.init(POSTHOG_KEY, { api_host: POSTHOG_HOST, - bootstrap: { - distinctID: anonymousId, - isIdentifiedID: false, - }, capture_pageview: false, capture_pageleave: true, persistence: "memory", @@ -43,15 +39,13 @@ function PageviewTracker() { } export function PostHogProvider({ - anonymousId, children, }: { - anonymousId: string; children: React.ReactNode; }) { useEffect(() => { - initPostHog(anonymousId); - }, [anonymousId]); + initPostHog(); + }, []); if (!POSTHOG_KEY) return <>{children}; diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsx index c077d1f4510..13edae493b3 100644 --- a/apps/web/app/layout.tsx +++ b/apps/web/app/layout.tsx @@ -1,6 +1,5 @@ import type { Metadata, Viewport } from "next"; import { Suspense } from "react"; -import { getAnonymousId } from "@/lib/telemetry"; import { PostHogProvider } from "./components/posthog-provider"; import "./globals.css"; @@ -43,7 +42,7 @@ export default function RootLayout({ - + {children} diff --git a/apps/web/lib/telemetry.ts b/apps/web/lib/telemetry.ts index 0323a5b1adc..8c9de8b37b4 100644 --- a/apps/web/lib/telemetry.ts +++ b/apps/web/lib/telemetry.ts @@ -1,5 +1,4 @@ -import { createHash } from "node:crypto"; -import os from "node:os"; +import { randomUUID } from "node:crypto"; import { PostHog } from "posthog-node"; const POSTHOG_KEY = process.env.NEXT_PUBLIC_POSTHOG_KEY || ""; @@ -7,15 +6,6 @@ const POSTHOG_HOST = "https://us.i.posthog.com"; let client: PostHog | null = null; -export function getAnonymousId(): string { - try { - const raw = `${os.hostname()}:${os.userInfo().username}`; - return createHash("sha256").update(raw).digest("hex").slice(0, 16); - } catch { - return "unknown"; - } -} - function ensureClient(): PostHog | null { if (!POSTHOG_KEY) return null; if (!client) { @@ -28,12 +18,16 @@ function ensureClient(): PostHog | null { return client; } -export function trackServer(event: string, properties?: Record): void { +export function trackServer( + event: string, + properties?: Record, + distinctId?: string, +): void { const ph = ensureClient(); if (!ph) return; ph.capture({ - distinctId: getAnonymousId(), + distinctId: distinctId || randomUUID(), event, properties: { ...properties,