Skip to main content
Prompts PostHog Integration for Next.js 15 App Router

developer coding developer risk: low

PostHog Integration for Next.js 15 App Router

Instructs the model to generate code files for integrating PostHog analytics, session replay, feature flags, and error tracking into a Next.js 15 App Router SaaS application with S…

PROMPT

Production-Grade PostHog Integration for Next.js 15 (App Router)
Role
You are a Senior Next.js Architect & Analytics Engineer with deep expertise in Next.js 15, React 19, Supabase Auth, Polar.sh billing, and PostHog.
You design production-grade, privacy-aware systems that handle the strict Server/Client boundaries of Next.js 15 correctly.
Your output must be code-first, deterministic, and suitable for a real SaaS product in 2026.

Goal
Integrate PostHog Analytics, Session Replay, Feature Flags, and Error Tracking into a Next.js 15 App Router SaaS application with:
- Correct Server / Client separation (Providers Pattern)
- Type-safe, centralized analytics
- User identity lifecycle synced with Supabase
- Accurate billing tracking (Polar)
- Suspense-safe SPA navigation tracking

Context
- Framework: Next.js 15 (App Router) & React 19
- Rendering: Server Components (default), Client Components (interaction)
- Auth: Supabase Auth
- Billing: Polar.sh
- State: No existing analytics
- Environment: Web SaaS (production)

Core Architectural Rules (NON-NEGOTIABLE)
1. PostHog must ONLY run in Client Components.
2. No PostHog calls in Server Components, Route Handlers, or API routes.
3. Identity is controlled only by auth state.
4. All analytics must flow through a single abstraction layer (`lib/analytics.ts`).

1. Architecture & Setup (Providers Pattern)
- Create `app/providers.tsx`.
- Mark it as `'use client'`.
- Initialize PostHog inside this component.
- Wrap the application with `PostHogProvider`.
- Configuration:
  - Use `NEXT_PUBLIC_POSTHOG_KEY` and `NEXT_PUBLIC_POSTHOG_HOST`.
  - `capture_pageview`: false (Handled manually to avoid App Router duplicates).
  - `capture_pageleave`: true.
  - Enable Session Replay (`mask_all_text_inputs: true`).

2. User Identity Lifecycle (Supabase Sync)
- Create `hooks/useAnalyticsAuth.ts`.
- Listen to Supabase `onAuthStateChange`.
- Logic:
  - SIGNED_IN: Call `posthog.identify`.
  - SIGNED_OUT: Call `posthog.reset()`.
  - Use appropriate React 19 hooks if applicable for state, but standard `useEffect` is fine for listeners.

3. Billing & Revenue (Polar)
- PostHog `distinct_id` must match Supabase User ID.
- Set `polar_customer_id` as a user property.
- Track events: `CHECKOUT_STARTED`, `SUBSCRIPTION_CREATED`.
- Ensure `SUBSCRIPTION_CREATED` includes `{ revenue: number, currency: string }` for PostHog Revenue dashboards.

4. Type-Safe Analytics Layer
- Create `lib/analytics.ts`.
- Define strict Enum `AnalyticsEvents`.
- Export typed `trackEvent` wrapper.
- Check `if (typeof window === 'undefined')` to prevent SSR errors.

5. SPA Navigation Tracking (Next.js 15 & Suspense Safe)
- Create `components/PostHogPageView.tsx`.
- Use `usePathname` and `useSearchParams`.
- CRITICAL: Because `useSearchParams` causes client-side rendering de-opt in Next.js 15 if not handled, you MUST wrap this component in a `<Suspense>` boundary when mounting it in `app/providers.tsx`.
- Trigger pageviews on route changes.

6. Error Tracking
- Capture errors explicitly: `posthog.capture('$exception', { message, stack })`.

Deliverables (MANDATORY)
Return ONLY the following files:
1. `package.json` (Dependencies: `posthog-js`).
2. `app/providers.tsx` (With Suspense wrapper).
3. `lib/analytics.ts` (Type-safe layer).
4. `hooks/useAnalyticsAuth.ts` (Auth sync).
5. `components/PostHogPageView.tsx` (Navigation tracking).
6. `app/layout.tsx` (Root layout integration example).

🚫 No extra files.
🚫 No prose explanations outside code comments.

OPTIONAL CONTEXT

  • NEXT_PUBLIC_POSTHOG_KEY
  • NEXT_PUBLIC_POSTHOG_HOST

ROLES & RULES

Role assignments

  • You are a Senior Next.js Architect & Analytics Engineer with deep expertise in Next.js 15, React 19, Supabase Auth, Polar.sh billing, and PostHog.
  1. PostHog must ONLY run in Client Components.
  2. No PostHog calls in Server Components, Route Handlers, or API routes.
  3. Identity is controlled only by auth state.
  4. All analytics must flow through a single abstraction layer (`lib/analytics.ts`).
  5. Your output must be code-first, deterministic, and suitable for a real SaaS product in 2026.
  6. Return ONLY the following files.
  7. No extra files.
  8. No prose explanations outside code comments.

EXPECTED OUTPUT

Format
code
Schema
file_list · package.json, app/providers.tsx, lib/analytics.ts, hooks/useAnalyticsAuth.ts, components/PostHogPageView.tsx, app/layout.tsx
Constraints
  • Return ONLY the 6 specified files
  • No extra files
  • No prose explanations outside code comments
  • code-first
  • deterministic
  • type-safe

SUCCESS CRITERIA

  • Create providers pattern with PostHogProvider.
  • Sync Supabase auth state with PostHog identify/reset.
  • Implement type-safe analytics layer in lib/analytics.ts.
  • Track SPA navigation with Suspense-safe PostHogPageView.
  • Integrate into root layout.
  • Add Polar billing event tracking.
  • Enable Session Replay and error tracking.

FAILURE MODES

  • Running PostHog in Server Components.
  • Omitting Suspense boundary for useSearchParams.
  • Including extra files or prose explanations.
  • Failing to sync distinct_id with Supabase User ID.
  • Enabling automatic capture_pageview.
  • Missing revenue properties in billing events.

CAVEATS

Missing context
  • Supabase client import and initialization details.
  • Polar.sh integration details for customer_id and event triggering.
  • Existing app/layout.tsx structure assumptions.
Ambiguities
  • Does not specify integration points for billing events like CHECKOUT_STARTED and SUBSCRIPTION_CREATED.
  • Error tracking mentions explicit capture but no location or example.
  • Source of polar_customer_id not defined (e.g., from Supabase user metadata).

QUALITY

OVERALL
0.85
CLARITY
0.95
SPECIFICITY
0.95
REUSABILITY
0.40
COMPLETENESS
0.90

IMPROVEMENT SUGGESTIONS

  • Introduce placeholders like [POSTHOG_HOST], [SUPABASE_URL] for reusability across projects.
  • Add example usage of trackEvent for billing events in comments.
  • Include a sample ErrorBoundary component or global error handler for error tracking.
  • Clarify polar_customer_id derivation with code snippet.

USAGE

Copy the prompt above and paste it into your AI of choice — Claude, ChatGPT, Gemini, or anywhere else you're working. Replace any placeholder sections with your own context, then ask for the output.

MORE FOR DEVELOPER