integration-nextjs-pages-router

Configure PostHog analytics for Next.js Pages Router apps across client and server.

58|5|Updated Jan 19, 2026
One-click install
npx skills add https://github.com/PostHog/skills --skill integration-nextjs-pages-router-posthog
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: integration-nextjs-pages-router
Source: https://github.com/PostHog/skills/tree/main/skills/posthog/integration/skills/nextjs-pages-router
Command: npx skills add https://github.com/PostHog/skills --skill integration-nextjs-pages-router-posthog

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

PostHog integration for Next.js Pages Router solves the challenge of instrumenting both client-side and server-side events in Next.js Pages Router apps, enabling cohesive product analytics, error tracking, and user identification.

Core Features & Use Cases

  • Client-side initialization via instrumentation-client.ts for Next.js 15.3+ to bootstrap analytics.
  • Server-side analytics using posthog-node to capture events from API routes and server functions, enabling end-to-end tracing and user identification.
  • Identify and track users during login to link events across sessions and devices.

Quick Start

Initialize PostHog in instrumentation-client.ts, add server-side event captures in API routes, and call posthog.identify() on user login to bind events to a user.

Frequently Asked Questions about integration-nextjs-pages-router

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up PostHog analytics in a Next.js Pages Router app?

To set up PostHog analytics in a Next.js Pages Router app, initialize the client via instrumentation-client.ts and use posthog-node for server-side API routes. You must configure NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN and NEXT_PUBLIC_POSTHOG_HOST environment variables.

Can I track server-side events from Next.js API routes using PostHog?

Yes, you can track server-side events from Next.js API routes using the posthog-node library. This enables end-to-end tracing and allows you to capture product usage and errors directly from your server functions.

How do I link PostHog events to a specific user after login in Next.js?

To link PostHog events to a specific user after login in Next.js, call posthog.identify() during the login flow. This binds subsequent events to the user, linking their activity across different sessions and devices.

Does PostHog server-side tracking work with the Next.js Pages Router?

Yes, PostHog server-side tracking works with the Next.js Pages Router by using posthog-node within API routes. This approach supports cohesive product analytics and error tracking across both client and server environments.

What environment variables are required for PostHog Next.js instrumentation?

The required environment variables for PostHog Next.js instrumentation are NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN and NEXT_PUBLIC_POSTHOG_HOST. These must be set to initialize the client and connect to your PostHog project.