Skip to main content
skill CX Codex

integration-nextjs-pages-router — Claude Code skill: setup

The integration-nextjs-pages-router skill guides PostHog setup in Next.js Pages Router apps. It covers analytics events, user identity, feature flags, and error tracking.

MIT 103 installs ★ 80 updated 2026-10-24

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add PostHog/skills --skill integration-nextjs-pages-router -a claude-code
Open on GitHub

What integration-nextjs-pages-router does

The skill gives an agent a four-stage path for adding PostHog to a Next.js Pages Router project: begin, edit, revise, and conclude. It includes an example implementation plus reference material for Next.js, user identification, and each setup stage.

The integration should be added beside existing integrations rather than restructuring them. PostHog keys belong in environment variables, never in source code. For Next.js 15.3+, the skill recommends instrumentation-client.ts; feature flags use useFeatureFlagEnabled() or useFeatureFlagPayload().

Capture analytics in the handlers where user actions happen, and add error tracking around critical flows and API boundaries. When both frontend and backend code are present, pass X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID to correlate the client session with the distinct ID. Hardcoded PostHog keys conflict with the skill’s required setup.

What it helps with

  • add PostHog to a Next.js Pages Router app
  • identify users during login and signup
  • wire feature flags through PostHog hooks
  • capture events at user-action handlers
  • track failures around API boundaries

Example prompts for the agent

  • Add PostHog to this Next.js Pages Router app by following basic-integration-1.0-begin.md, basic-integration-1.1-edit.md, basic-integration-1.2-revise.md, and basic-integration-1.3-conclude.md.
  • Compare the result with references/EXAMPLE.md and keep the current integrations intact.
  • Add identification to login and signup, then pass X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID between the frontend and backend.

How to install integration-nextjs-pages-router

1Skills CLI (Vercel)

npx skills add PostHog/skills --skill integration-nextjs-pages-router -a claude-code

2Manually: put the files in place

~/.claude/skills/integration-nextjs-pages-router/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/integration-nextjs-pages-router/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about integration-nextjs-pages-router

How do I install integration-nextjs-pages-router in Claude Code?

Run npx skills add PostHog/skills --skill integration-nextjs-pages-router -a claude-code, then open ~/.claude/skills/integration-nextjs-pages-router/SKILL.md.

What does integration-nextjs-pages-router do?

It guides an agent through adding PostHog to a Next.js Pages Router app. The workflow covers analytics, feature flags, user identification, and error tracking.

How does the skill correlate frontend and backend activity?

When both sides exist, it passes X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID so the client session and distinct ID remain linked.

Source text

SKILL.md · MIT
---
name: integration-nextjs-pages-router
description: PostHog integration for Next.js Pages Router applications
metadata:
  author: PostHog
  version: 1.9.4
---

# PostHog integration for Next.js Pages Router

This skill helps you add PostHog analytics to Next.js Pages Router applications.

## Workflow

Follow these steps in order to complete the integration:

1. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here**
2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit
3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise
4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion

## Reference files

- `references/EXAMPLE.md` - Next.js Pages Router example project code
- `references/next-js.md` - Next.js - docs
- `references/identify-users.md` - Identify users - docs
- `references/basic-integration-1.0-begin.md` - PostHog setup - begin
- `references/basic-integration-1.1-edit.md` - PostHog setup - edit
- `references/basic-integration-1.2-revise.md` - PostHog setup - revise
- `references/basic-integration-1.3-conclude.md` - PostHog setup - conclusion

The example project shows the target implementation pattern. Consult the documentation for API details.

## Key principles

- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them.
- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
- **Match the example**: Your implementation should follow the example project's patterns as closely as possible.

## Framework guidelines

- For Next.js 15.3+, initialize PostHog in instrumentation-client.ts for the simplest setup
- For feature flags, use useFeatureFlagEnabled() or useFeatureFlagPayload() hooks - they handle loading states and external sync automatically
- Add analytics capture in event handlers where user actions occur, NOT in useEffect reacting to state changes
- Do NOT use useEffect for data transformation - calculate derived values during render instead
- Do NOT use useEffect to respond to user events - put that logic in the event handler itself
- Do NOT use useEffect to chain state updates - calculate all related updates together in the event handler
- Do NOT use useEffect to notify parent components - call the parent callback alongside setState in the event handler
- To reset component state when a prop changes, pass the prop as the component's key instead of using useEffect
- useEffect is ONLY for synchronizing with external systems (non-React widgets, browser APIs, network subscriptions)

## Identifying users

Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation.

## Error tracking

Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.

© PostHog · MIT · PostHog/skills

Similar skills

All skills for Frontend and UI →

From the same set112

The whole set →
feature-flags-api The feature-flags-api skill guides agents through adding PostHog feature flag checks to API applications. Use it when you want to introduce flags without restructuring existing code. CX Codex +1 111installs integration-react-native The integration-react-native skill guides an agent through adding PostHog analytics to a React Native app, including user identification and error tracking. CX Codex +1 109installs feature-flags-react-native feature-flags-react-native integrates PostHog feature flags into React Native apps following best practices for hooks, environment variables, and navigation. CX Codex +1 109installs integration-astro-ssr integration-astro-ssr adds PostHog analytics to server-rendered Astro setups with API endpoints. Connects frontend tracking with backend routes. CX Codex +1 109installs integration-swift integration-swift guides PostHog analytics integration into Swift applications across iOS and macOS targets. CX Codex +1 108installs integration-react-vite integration-react-vite adds PostHog analytics to a React app built with Vite and no router: setup in four steps, user identification, feature flags and error tracking. CX Codex +1 107installs omnibus-instrument-llm-analytics omnibus-instrument-llm-analytics adds PostHog LLM analytics to your code so each generation reports tokens, latency and cost. Installs the SDK too when the project has none. CX Codex +1 106installs integration-react-react-router-7-declarative integration-react-react-router-7-declarative integrates PostHog analytics into React Router v7 declarative mode projects. CX Codex +1 102installs

Project sponsors

Companies that keep this analytics open