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
npx skills add PostHog/skills --skill integration-nextjs-pages-router -a claude-codeWhat 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, andbasic-integration-1.3-conclude.md. - Compare the result with
references/EXAMPLE.mdand keep the current integrations intact. - Add identification to login and signup, then pass
X-POSTHOG-DISTINCT-IDandX-POSTHOG-SESSION-IDbetween 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-code2Manually: put the files in place
~/.claude/skills/integration-nextjs-pages-router/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/integration-nextjs-pages-router/
1Skills CLI (Vercel)
npx skills add PostHog/skills --skill integration-nextjs-pages-router -a codex2Manually: put the files in place
.agents/skills/integration-nextjs-pages-router/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add PostHog/skills --skill integration-nextjs-pages-router -a cursor2Manually: put the files in place
.cursor/skills/integration-nextjs-pages-router/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install PostHog/skills skills/posthog/all/skills/integration-nextjs-pages-router2Manually: put the files in place
.github/skills/integration-nextjs-pages-router/SKILL.md1Skills CLI (Vercel)
npx skills add PostHog/skills --skill integration-nextjs-pages-router -a gemini-cli2Platform CLI
gemini skills install https://github.com/PostHog/skills --path skills/posthog/all/skills/integration-nextjs-pages-routerManually: put the files in place
.devin/skills/integration-nextjs-pages-router/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add PostHog/skills --skill integration-nextjs-pages-router -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
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