|
| 1 | +--- |
| 2 | +name: nextjs-app-router |
| 3 | +description: PostHog integration for Next.js App Router applications |
| 4 | +metadata: |
| 5 | + author: PostHog |
| 6 | + version: dev |
| 7 | +--- |
| 8 | + |
| 9 | +# PostHog integration for Next.js App Router |
| 10 | + |
| 11 | +This skill helps you add PostHog analytics to Next.js App Router applications. |
| 12 | + |
| 13 | +## Workflow |
| 14 | + |
| 15 | +Follow these steps in order to complete the integration: |
| 16 | + |
| 17 | +1. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here** |
| 18 | +2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit |
| 19 | +3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise |
| 20 | +4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion |
| 21 | + |
| 22 | +## Reference files |
| 23 | + |
| 24 | +- `EXAMPLE.md` - Next.js App Router example project code |
| 25 | +- `next-js.md` - Next.js - docs |
| 26 | +- `identify-users.md` - Identify users - docs |
| 27 | +- `basic-integration-1.0-begin.md` - PostHog setup - begin |
| 28 | +- `basic-integration-1.1-edit.md` - PostHog setup - edit |
| 29 | +- `basic-integration-1.2-revise.md` - PostHog setup - revise |
| 30 | +- `basic-integration-1.3-conclude.md` - PostHog setup - conclusion |
| 31 | + |
| 32 | +The example project shows the target implementation pattern. Consult the documentation for API details. |
| 33 | + |
| 34 | +## Key principles |
| 35 | + |
| 36 | +- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them. |
| 37 | +- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code. |
| 38 | +- **Match the example**: Your implementation should follow the example project's patterns as closely as possible. |
| 39 | + |
| 40 | +## Framework guidelines |
| 41 | + |
| 42 | +- Never use useEffect() for analytics capture - it's brittle and causes errors |
| 43 | +- Prefer event handlers or routing mechanisms to trigger analytics calls |
| 44 | +- Add handlers where user actions occur rather than reacting to state changes |
| 45 | +- Remember that source code is available in the node_modules directory |
| 46 | +- Check package.json for type checking or build scripts to validate changes |
| 47 | +- posthog-js is the JavaScript SDK package name |
| 48 | + |
| 49 | +## Identifying users |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +## Error tracking |
| 54 | + |
| 55 | +Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries. |
0 commit comments