Skip to content

fix: PostHog events not sending — init in provider, not instrumentation-client#32

Merged
sonpiaz merged 1 commit into
mainfrom
fix/posthog-single-instance-provider
May 28, 2026
Merged

fix: PostHog events not sending — init in provider, not instrumentation-client#32
sonpiaz merged 1 commit into
mainfrom
fix/posthog-single-instance-provider

Conversation

@sonpiaz
Copy link
Copy Markdown
Member

@sonpiaz sonpiaz commented May 28, 2026

This is the real fix for "0 events in PostHog despite traffic".

Root cause: posthog.init() was in instrumentation-client.ts — a separate Next build entry. It got a different posthog-js instance than the app bundle, so init ran (remote config.js loaded → looked wired) but every capture() in the app hit a second, uninitialized instance and silently sent nothing. Hence config loads but no $pageview and Web analytics = 0.

Fix (mirrors the working kyma setup):

  • src/lib/posthog.ts — single shared client (initPostHog + exported posthog).
  • src/components/posthog-provider.tsx ('use client') — inits inside the React tree + captures $pageview on initial load and every route change.
  • layout.tsx wraps the app in <PostHogProvider>; track() routes through the same module.
  • Removes instrumentation-client.ts + posthog-pageview.tsx.

Verified locally: browser now POSTs events — /_ph/e/, /_ph/i/v0/e/ (pageview), /_ph/s/ (recording) all 200. (Temporarily disabled the bot-UA filter to test from headless; reverted before commit.) Build green (878/878). Will mirror to SSOT.

🤖 Generated with Claude Code

…vents now send)

Root cause of the 0-events-in-PostHog problem: posthog.init() lived in
instrumentation-client.ts, which Next builds as a separate entry. That gave it
a different posthog-js module instance than the app bundle, so init ran (the
remote config.js loaded — making it look wired) while every capture() in
PostHogPageView / track() hit a second, uninitialized instance and silently
sent nothing. Web analytics stayed at 0 despite real traffic.

Fix mirrors the working kyma setup: a single shared client in src/lib/posthog.ts
(initPostHog + exported posthog) initialized inside a 'use client'
PostHogProvider mounted in the root layout. init and all captures now share one
instance. $pageview is captured manually on initial load + every route change
(usePathname only, to keep the ~800 static pages static). track() routes its
dual-write through the same module.

Verified locally: with init in the provider, the browser actually POSTs events
— /_ph/e/, /_ph/i/v0/e/ (pageview), and /_ph/s/ (recording) all return 200.
(Confirmed by temporarily disabling the bot-UA filter so the headless test
browser wasn't filtered; that flag was reverted before commit.)

Removes instrumentation-client.ts and components/posthog-pageview.tsx.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
open-affiliate Canceled Canceled May 28, 2026 6:23am

Request Review

@sonpiaz sonpiaz merged commit 5194e09 into main May 28, 2026
2 of 4 checks passed
@sonpiaz sonpiaz deleted the fix/posthog-single-instance-provider branch May 28, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant