From 982046e3e816edb77ed5bfafa654d52e40e181e0 Mon Sep 17 00:00:00 2001 From: "wizard-ci-bot[bot]" <254716194+wizard-ci-bot[bot]@users.noreply.github.com> Date: Mon, 6 Jul 2026 22:09:06 +0000 Subject: [PATCH] wizard-ci: react-router/react-router-v7-project --- .../.posthog-wizard | 0 .../SKILL.md | 76 + .../references/1-begin.md | 56 + .../references/2-edit.md | 36 + .../references/3-revise.md | 22 + .../references/4-conclude.md | 57 + .../references/EXAMPLE.md | 1218 +++++++++++++++++ .../references/identify-users.md | 272 ++++ .../react-router-v7-framework-mode.md | 479 +++++++ .../app/context/AuthContext.tsx | 21 +- .../app/entry.client.tsx | 16 +- .../react-router-v7-project/app/root.tsx | 4 + .../app/routes/countries.tsx | 37 +- .../app/routes/login.tsx | 17 + .../app/routes/signup.tsx | 13 + .../react-router-v7-project/package-lock.json | 129 +- .../react-router-v7-project/package.json | 3 + .../posthog-setup-report.md | 38 + .../react-router-v7-project/vite.config.ts | 3 + 19 files changed, 2490 insertions(+), 7 deletions(-) create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/.posthog-wizard create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/SKILL.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/1-begin.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/2-edit.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/3-revise.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/4-conclude.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/EXAMPLE.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/identify-users.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/react-router-v7-framework-mode.md create mode 100644 apps/basic-integration/react-router/react-router-v7-project/posthog-setup-report.md diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/.posthog-wizard b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/.posthog-wizard new file mode 100644 index 000000000..e69de29bb diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/SKILL.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/SKILL.md new file mode 100644 index 000000000..78d7873fe --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/SKILL.md @@ -0,0 +1,76 @@ +--- +name: integration-react-react-router-7-framework +description: PostHog integration for React Router v7 - Framework mode applications +metadata: + author: PostHog + version: dev +--- + +# PostHog integration for React Router v7 - Framework mode + +This skill helps you add PostHog analytics to React Router v7 - Framework mode applications. + +## Workflow + +Follow these steps in order to complete the integration: + +1. `references/1-begin.md` - PostHog Setup - Begin ← **Start here** +2. `references/2-edit.md` - PostHog Setup - Edit +3. `references/3-revise.md` - PostHog Setup - Revise +4. `references/4-conclude.md` - PostHog Setup - Conclusion + +## Reference files + +- `references/EXAMPLE.md` - React Router v7 - Framework mode example project code +- `references/1-begin.md` - Start the event tracking setup process by analyzing the project and creating an event tracking plan +- `references/2-edit.md` - Implement PostHog event tracking in the identified files, following best practices and the example project +- `references/3-revise.md` - Review and fix any errors in the PostHog integration implementation +- `references/4-conclude.md` - Review and fix any errors in the PostHog integration implementation +- `references/react-router-v7-framework-mode.md` - React router v7 framework mode (remix v3) - docs +- `references/identify-users.md` - Identify users - docs + +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 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) +- Remember that source code is available in the node_modules directory +- Check package.json for type checking or build scripts to validate changes +- posthog-node is the Node.js server-side SDK package name – do NOT use posthog-js on the server +- Include enableExceptionAutocapture: true in the PostHog constructor options +- Add posthog.capture() calls in route handlers for meaningful user actions – every route that creates, updates, or deletes data should track an event with contextual properties +- Add posthog.captureException(err, distinctId) in the application's error handler (e.g., Express error middleware, Fastify setErrorHandler, Koa app.on('error')) +- In long-running servers, the SDK batches events automatically – do NOT set flushAt or flushInterval unless you have a specific reason to +- For short-lived processes (scripts, CLIs, serverless), set flushAt to 1 and flushInterval to 0 to send events immediately +- Reverse proxy is NOT needed for server-side Node.js – only client-side JavaScript needs a proxy to avoid ad blockers +- When a reverse proxy is configured, both /static/* AND /array/* must route to the assets origin (us-assets.i.posthog.com or eu-assets.i.posthog.com). +- posthog-js is the JavaScript SDK package name +- posthog.init() MUST be called before any other PostHog methods (capture, identify, etc.) +- posthog-js is browser-only — do NOT import it in Node.js or server-side contexts (use posthog-node instead) +- Autocapture is ON by default with posthog-js (tracks clicks, form submissions, pageviews). Do NOT disable autocapture unless the user explicitly requests it. +- NEVER send PII in posthog.capture() event properties — no emails, full names, phone numbers, physical addresses, IP addresses, or user-generated content +- PII belongs in posthog.identify() person properties (email, name, role), NOT in capture() event properties +- Call posthog.identify(userId, { email, name, role }) on login AND on page refresh if the user is already logged in +- Call posthog.reset() on logout to unlink future events from the current user +- For SPAs without a framework router, capture pageviews with posthog.capture($pageview) or use the capture_pageview history_change option in init for History API routing + +## 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. diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/1-begin.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/1-begin.md new file mode 100644 index 000000000..55f0a8326 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/1-begin.md @@ -0,0 +1,56 @@ +--- +title: PostHog Setup - Begin +description: Start the event tracking setup process by analyzing the project and creating an event tracking plan +--- + +We're making an event tracking plan for this project. + +This is the first of several phases — plan the events, implement them, revise and validate changes, then conclude by creating a dashboard and writing a setup report. + +## Task list + +As soon as you've read this description and have a rough sense of the work, make a single **call `TaskCreate` immediately** before reading any reference file or beginning analysis. The user is watching the task pane and shouldn't see it sit empty. + +It's fine if your first list is incomplete or imprecise. Seed it with whatever high-level items you can infer from the overview above, then call `TaskCreate` again (or `TaskUpdate` to refine existing items) every time your understanding sharpens: after a phase reveals work you didn't anticipate, after planning surfaces concrete sub-items, after you hit something new. Use `TaskUpdate` to mark items `in_progress` when you start them and `completed` when you finish. Keeping the list current matters more than getting it right on the first call. + +Keep task titles broad and job-oriented. Describe the purpose or area of work with wording like "Planning event tracking", "Identifying users", "Installing PostHog", "Capturing events", or "Creating dashboards", not the specific files, paths, or symbols involved. Adjust the task names according to the user's project and context. + +Before proceeding, find any existing `posthog.capture()` code. Make note of event name formatting. + +From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events. Also look for additional files related to login that could be used for identifying users, along with error handling. Read the files. If a file is already well-covered by PostHog events, replace it with another option. Do not spawn subagents. + +Look for opportunities to track client-side events. + +**IMPORTANT: Server-side events are REQUIRED** if the project includes any instrumentable server-side code. If the project has API routes (e.g., `app/api/**/route.ts`) or Server Actions, you MUST include server-side events for critical business operations like: + + - Payment/checkout completion + - Webhook handlers + - Authentication endpoints + +Do not skip server-side events - they capture actions that cannot be tracked client-side. + +Create a new file with a JSON array at the root of the project: .posthog-events.json. It should include one object for each event we want to add with these exact field names: `event_name` (the event name), `event_description` (one sentence), and `file` (the file path the event goes in). The wizard reads this file to surface the plan in the UI. If events already exist, don't duplicate them; supplement them. + +Track actions only, not pageviews. These can be captured automatically. Exceptions can be made for "viewed"-type events that correspond to the top of a conversion funnel. + +As you review files, make an internal note of opportunities to identify users and catch errors. We'll need them for the next step. + +## Status + +Before beginning a phase of the setup, you will send a status message with the exact prefix '[STATUS]', as in: + +[STATUS] Checking project structure. + +Status to report in this phase: + +- Checking project structure +- Verifying PostHog dependencies +- Generating events based on project + +## Abort statuses + +If and only if the instructions have `[ABORT]` states specified, and you clearly match the conditions for an abort, emit the abort message. Do NOT attempt to exit or halt yourself — the wizard's middleware catches `[ABORT]` and terminates the run for you. + +--- + +**Upon completion, continue with:** [2-edit.md](2-edit.md) \ No newline at end of file diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/2-edit.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/2-edit.md new file mode 100644 index 000000000..e5f7ffd16 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/2-edit.md @@ -0,0 +1,36 @@ +--- +title: PostHog Setup - Edit +description: Implement PostHog event tracking in the identified files, following best practices and the example project +--- + +For each of the files and events noted in .posthog-events.json, make edits to capture events using PostHog. Make sure to set up any helper files needed. Carefully examine the included example project code: your implementation should match it as closely as possible. Do not spawn subagents. + +Use environment variables for PostHog keys. Do not hardcode PostHog keys. + +If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it. + +For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach. + +Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference. + +Where possible, add calls for PostHog's identify() function on the client side upon events like logins and signups. Use the contents of login and signup forms to identify users on submit. If there is server-side code, pass the client-side session and distinct ID to the server-side code to identify the user. On the server side, make sure events have a matching distinct ID where relevant. + +It's essential to do this in both client code and server code, so that user behavior from both domains is easy to correlate. + +You should also add PostHog exception capture error tracking to these files where relevant. + +Remember: Do not alter the fundamental architecture of existing files. Make your additions minimal and targeted. + +Remember the documentation and example project resources you were provided at the beginning. Read them now. + +## Status + +Status to report in this phase: + +- Inserting PostHog capture code +- A status message for each file whose edits you are planning, including a high level summary of changes +- A status message for each file you have edited + +--- + +**Upon completion, continue with:** [3-revise.md](3-revise.md) \ No newline at end of file diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/3-revise.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/3-revise.md new file mode 100644 index 000000000..3b07f5069 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/3-revise.md @@ -0,0 +1,22 @@ +--- +title: PostHog Setup - Revise +description: Review and fix any errors in the PostHog integration implementation +--- + +Check the project for errors. Read the package.json file for any type checking or build scripts that may provide input about what to fix. Remember that you can find the source code for any dependency in the node_modules directory. Do not spawn subagents. + +Ensure that any components created were actually used. + +Once all other tasks are complete, run any linter or prettier-like scripts found in the package.json, but ONLY on the files you have edited or created during this session. Do not run formatting or linting across the entire project's codebase. + +## Status + +Status to report in this phase: + +- Finding and correcting errors +- Report details of any errors you fix +- Linting, building and prettying + +--- + +**Upon completion, continue with:** [4-conclude.md](4-conclude.md) \ No newline at end of file diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/4-conclude.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/4-conclude.md new file mode 100644 index 000000000..d876d4353 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/4-conclude.md @@ -0,0 +1,57 @@ +--- +title: PostHog Setup - Conclusion +description: Review and fix any errors in the PostHog integration implementation +--- + +Use the PostHog MCP to create a new dashboard named "Analytics basics (wizard)" based on the events created here. Keep the `(wizard)` tag with that exact casing so anyone browsing PostHog can see the wizard created this dashboard, and so a quick search for `(wizard)` surfaces every wizard-created artifact in one go. Make sure to use the exact same event names as implemented in the code. Populate it with up to five insights, with special emphasis on things like conversion funnels, churn events, and other business critical insights. + +Once the dashboard exists, emit its URL on its own line in your assistant message using this exact marker: `[DASHBOARD_URL] `. The wizard parses this marker from your visible message and surfaces the link in the success summary. Mentioning the URL only in thinking or in prose without the marker means the link is dropped. + +Search for a file called `.posthog-events.json` and read it for available events. + +Do not spawn subagents. + +Create the file posthog-setup-report.md. It should include a summary of the integration edits, a table with the event names, event descriptions, and files where events were added, a list of links for the dashboard and insights created, and a "Verify before merging" checklist (see below). Follow this format: + + +# PostHog post-wizard report + +The wizard has completed a deep integration of your project. [Detailed summary of changes] + +[table of events/descriptions/files] + +## Next steps + +We've built some insights and a dashboard for you to keep an eye on user behavior, based on the events we just instrumented: + +[links] + +## Verify before merging + +[checklist] + +### Agent skill + +We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog. + + + +For the "Verify before merging" checklist, write GitHub-style checkboxes (`- [ ] ...`) covering what the developer (or their coding agent) still needs to do to take this from "wizard finished" to "merged". Include ONLY the items that actually apply to the integration you just performed — judge each against the code you changed in this run, and drop any that don't fit. Phrase each item as a concrete, checkable action. Candidate items, with the condition for including each: + +- Always: "Run a full production build (the wizard only verified the files it touched) and fix any lint or type errors introduced by the generated code." +- Always: "Run the test suite — call sites that were rewritten or instrumented may need updated mocks or fixtures." +- If you added environment variables: "Add the exact PostHog env var names you added to `.env.example` and any monorepo/bootstrap scripts so collaborators know what to set." +- If this integration ships a minified production browser bundle (most SPA/SSR web frameworks — e.g. Next.js, Nuxt, SvelteKit, Astro, Vite-based apps): "Wire source-map upload (`posthog-cli sourcemap` or your bundler's upload step) into CI so production stack traces de-minify." +- If LLM analytics was set up in this run: "Trigger the LLM call path(s) you instrumented and confirm `$ai_generation` events appear in PostHog AI Observability." +- If the app has user auth and an `identify` call was added: "Confirm the returning-visitor path also calls `identify` — a handler that only identifies on fresh login can leave returning sessions on anonymous distinct IDs." + +Do not invent items beyond what applies. If only the two "Always" items apply, the checklist is just those two. + +Upon completion, remove .posthog-events.json. + +## Status + +Status to report in this phase: + +- Configured dashboard: [insert PostHog dashboard URL] +- Created setup report: [insert full local file path] \ No newline at end of file diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/EXAMPLE.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/EXAMPLE.md new file mode 100644 index 000000000..115ca4365 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/EXAMPLE.md @@ -0,0 +1,1218 @@ +# PostHog React Router v7 - Framework mode Example Project + +Repository: https://github.com/PostHog/context-mill +Path: example-apps/react-react-router-7-framework + +--- + +## README.md + +# PostHog React Router 7 Framework example + +This is a [React Router 7](https://reactrouter.com) Framework example demonstrating PostHog integration with product analytics, session replay, feature flags, and error tracking. + +## Features + +- **Product Analytics**: Track user events and behaviors +- **Session Replay**: Record and replay user sessions +- **Error Tracking**: Capture and track errors +- **User Authentication**: Demo login system with PostHog user identification +- **Server-side & Client-side Tracking**: Examples of both tracking methods +- **SSR Support**: Server-side rendering with React Router 7 Framework + +## Getting Started + +### 1. Install Dependencies + +```bash +npm install +# or +pnpm install +``` + +### 2. Configure Environment Variables + +Create a `.env` file in the root directory: + +```bash +VITE_PUBLIC_POSTHOG_PROJECT_TOKEN=your_posthog_project_token +VITE_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com +``` + +Get your PostHog project token from your [PostHog project settings](https://app.posthog.com/project/settings). + +### 3. Run the Development Server + +```bash +npm run dev +# or +pnpm dev +``` + +Open [http://localhost:5173](http://localhost:5173) with your browser to see the app. + +## Project Structure + +``` +app/ +├── components/ +│ └── Header.tsx # Navigation header with auth state +├── contexts/ +│ └── AuthContext.tsx # Authentication context +├── lib/ +│ ├── posthog-middleware.ts # Server-side PostHog middleware +│ └── db.ts # Database utilities +├── routes/ +│ ├── home.tsx # Home/Login page +│ ├── burrito.tsx # Demo feature page with event tracking +│ ├── profile.tsx # User profile with error tracking demo +│ ├── api.auth.login.ts # Login API with server-side tracking +│ └── api.burrito.consider.ts # Burrito API with server-side tracking +├── entry.client.tsx # Client entry with PostHog initialization +├── entry.server.tsx # Server entry +└── root.tsx # Root route with error boundary +``` + +## Key Integration Points + +### Client-side initialization (entry.client.tsx) + +```typescript +import posthog from 'posthog-js'; +import { PostHogProvider } from '@posthog/react' + +posthog.init(import.meta.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN, { + api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + defaults: '2026-01-30', + __add_tracing_headers: [ window.location.host, 'localhost' ], +}); + + + + +``` + +### User identification (home.tsx) + +The user is identified when the user logs in on the **client-side**. + +```typescript +posthog?.identify(username, { + username: username, +}); +posthog?.capture('user_logged_in', { + username: username, +}); +``` + +The session and distinct ID are automatically passed to the backend via the `X-POSTHOG-SESSION-ID` and `X-POSTHOG-DISTINCT-ID` headers because we set the `__add_tracing_headers` option in the PostHog initialization. + +**Important**: do not identify users on the server-side. + +### Server-side middleware (posthog-middleware.ts) + +The PostHog middleware creates a server-side PostHog client for each request and extracts session and user context from request headers: + +```typescript +export const posthogMiddleware: Route.MiddlewareFunction = async ({ request, context }, next) => { + const posthog = new PostHog(process.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN!, { + host: process.env.VITE_PUBLIC_POSTHOG_HOST!, + flushAt: 1, + flushInterval: 0, + }); + + const sessionId = request.headers.get('X-POSTHOG-SESSION-ID'); + const distinctId = request.headers.get('X-POSTHOG-DISTINCT-ID'); + + context.posthog = posthog; + + const response = await posthog.withContext( + { sessionId: sessionId ?? undefined, distinctId: distinctId ?? undefined }, + next + ); + + await posthog.shutdown().catch(() => {}); + return response; +}; +``` + +**Key Points:** +- Creates a new PostHog Node client for each request +- Extracts `sessionId` and `distinctId` from request headers (automatically set by the client-side SDK) +- Sets the PostHog client on the request context for use in route handlers +- Uses `withContext()` to associate server-side events with the correct session/user +- Properly shuts down the client after each request + +### Event tracking (burrito.tsx) + +```typescript +posthog?.capture('burrito_considered', { + total_considerations: count, + username: username, +}); +``` + +### Error tracking (root.tsx, profile.tsx) + +Errors are captured in two ways: + +1. **Error boundary** - The `ErrorBoundary` in `root.tsx` automatically captures unhandled React Router errors: +```typescript +export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) { + const posthog = usePostHog(); + posthog.captureException(error); + // ... error UI +} +``` + +2. **Manual error capture** in components (profile.tsx): +```typescript +posthog.captureException(err); +``` + +### Server-side tracking (api.auth.login.ts, api.burrito.consider.ts) + +Server-side events use the PostHog client from the request context (set by the middleware): + +```typescript +const posthog = (context as any).posthog as PostHog | undefined; +if (posthog) { + posthog.capture({ event: 'server_login' }); +} +``` + +**Key Points:** +- The PostHog client is available via `context.posthog` (set by the middleware) +- Events are automatically associated with the correct user/session via the middleware's `withContext()` call +- The `distinctId` and `sessionId` are extracted from request headers and used to maintain context between client and server + +## Learn More + +- [PostHog Documentation](https://posthog.com/docs) +- [React Router 7 Documentation](https://reactrouter.com) +- [PostHog React Integration Guide](https://posthog.com/docs/libraries/react) + +--- + +## .env.example + +```example +VITE_PUBLIC_POSTHOG_PROJECT_TOKEN= +VITE_PUBLIC_POSTHOG_HOST= +PROJECT_ID= +``` + +--- + +## app/components/Header.tsx + +```tsx +import { Link } from 'react-router'; +import { useAuth } from '../contexts/AuthContext'; +import { usePostHog } from '@posthog/react'; + +export default function Header() { + const { user, logout } = useAuth(); + const posthog = usePostHog(); + + const handleLogout = () => { + posthog?.capture('user_logged_out'); + posthog?.reset(); + logout(); + }; + + return ( +
+
+ +
+ {user ? ( + <> + Welcome, {user.username}! + + + ) : ( + Not logged in + )} +
+
+
+ ); +} + + +``` + +--- + +## app/contexts/AuthContext.tsx + +```tsx +import { createContext, useContext, useState, type ReactNode } from 'react'; + +interface User { + username: string; + burritoConsiderations: number; +} + +interface AuthContextType { + user: User | null; + login: (username: string, password: string) => Promise; + logout: () => void; + incrementBurritoConsiderations: () => void; + setUser: (user: User) => void; +} + +const AuthContext = createContext(undefined); + +const users: Map = new Map(); + +export function AuthProvider({ children }: { children: ReactNode }) { + const [user, setUser] = useState(() => { + if (typeof window === 'undefined') return null; + + const storedUsername = localStorage.getItem('currentUser'); + if (storedUsername) { + const existingUser = users.get(storedUsername); + if (existingUser) { + return existingUser; + } + } + return null; + }); + + const login = async (username: string, password: string): Promise => { + try { + const response = await fetch('/api/auth/login', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username, password }), + }); + + if (response.ok) { + const { user: userData } = await response.json(); + + let localUser = users.get(username); + if (!localUser) { + localUser = userData as User; + users.set(username, localUser); + } + + setUser(localUser); + localStorage.setItem('currentUser', username); + + return true; + } + return false; + } catch (error) { + console.error('Login error:', error); + return false; + } + }; + + const logout = () => { + setUser(null); + localStorage.removeItem('currentUser'); + }; + + const incrementBurritoConsiderations = () => { + if (user) { + user.burritoConsiderations++; + users.set(user.username, user); + setUser({ ...user }); + } + }; + + const setUserState = (newUser: User) => { + setUser(newUser); + users.set(newUser.username, newUser); + }; + + return ( + + {children} + + ); +} + +export function useAuth() { + const context = useContext(AuthContext); + if (context === undefined) { + throw new Error('useAuth must be used within an AuthProvider'); + } + return context; +} + + +``` + +--- + +## app/entry.client.tsx + +```tsx +import { startTransition, StrictMode } from "react"; +import { hydrateRoot } from "react-dom/client"; +import { HydratedRouter } from "react-router/dom"; + +import posthog from 'posthog-js'; +import { PostHogProvider } from '@posthog/react' + +posthog.init(import.meta.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN, { + api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + defaults: '2026-01-30', + __add_tracing_headers: [ window.location.host, 'localhost' ], +}); + + +startTransition(() => { + hydrateRoot( + document, + + + + + , + ); +}); + +``` + +--- + +## app/entry.server.tsx + +```tsx +import { PassThrough } from "node:stream"; + +import type { EntryContext, RouterContextProvider } from "react-router"; +import { createReadableStreamFromReadable } from "@react-router/node"; +import { ServerRouter } from "react-router"; +import { isbot } from "isbot"; +import type { RenderToPipeableStreamOptions } from "react-dom/server"; +import { renderToPipeableStream } from "react-dom/server"; + +export const streamTimeout = 5_000; + +export default function handleRequest( + request: Request, + responseStatusCode: number, + responseHeaders: Headers, + routerContext: EntryContext, + loadContext: RouterContextProvider, +) { + // https://httpwg.org/specs/rfc9110.html#HEAD + if (request.method.toUpperCase() === "HEAD") { + return new Response(null, { + status: responseStatusCode, + headers: responseHeaders, + }); + } + + return new Promise((resolve, reject) => { + let shellRendered = false; + let userAgent = request.headers.get("user-agent"); + + // Ensure requests from bots and SPA Mode renders wait for all content to load before responding + // https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation + let readyOption: keyof RenderToPipeableStreamOptions = + (userAgent && isbot(userAgent)) || routerContext.isSpaMode + ? "onAllReady" + : "onShellReady"; + + // Abort the rendering stream after the `streamTimeout` so it has time to + // flush down the rejected boundaries + let timeoutId: ReturnType | undefined = setTimeout( + () => abort(), + streamTimeout + 1000, + ); + + const { pipe, abort } = renderToPipeableStream( + , + { + [readyOption]() { + shellRendered = true; + const body = new PassThrough({ + final(callback) { + // Clear the timeout to prevent retaining the closure and memory leak + clearTimeout(timeoutId); + timeoutId = undefined; + callback(); + }, + }); + const stream = createReadableStreamFromReadable(body); + + responseHeaders.set("Content-Type", "text/html"); + + pipe(body); + + resolve( + new Response(stream, { + headers: responseHeaders, + status: responseStatusCode, + }), + ); + }, + onShellError(error: unknown) { + reject(error); + }, + onError(error: unknown) { + responseStatusCode = 500; + // Log streaming rendering errors from inside the shell. Don't log + // errors encountered during initial shell rendering since they'll + // reject and get logged in handleDocumentRequest. + if (shellRendered) { + console.error(error); + } + }, + }, + ); + }); +} + +``` + +--- + +## app/lib/db.ts + +```ts +import sqlite3 from "sqlite3"; +import { join } from "node:path"; +import { promisify } from "node:util"; + +const dbPath = join(process.cwd(), "burrito-considerations.db"); + +const db = new sqlite3.Database(dbPath); + +// Initialize schema +db.serialize(() => { + db.run(` + CREATE TABLE IF NOT EXISTS burrito_considerations ( + username TEXT PRIMARY KEY, + count INTEGER NOT NULL DEFAULT 0 + ) + `); +}); + +const dbGet = promisify(db.get.bind(db)); +const dbRun = promisify(db.run.bind(db)); + +export function getBurritoConsiderations(username: string): Promise { + return dbGet("SELECT count FROM burrito_considerations WHERE username = ?", [username]) + .then((row: any) => row?.count ?? 0); +} + +export function incrementBurritoConsiderations(username: string): Promise { + return dbRun(` + INSERT INTO burrito_considerations (username, count) + VALUES (?, 1) + ON CONFLICT(username) DO UPDATE SET count = count + 1 + `, [username]) + .then(() => { + return dbGet("SELECT count FROM burrito_considerations WHERE username = ?", [username]); + }) + .then((row: any) => row.count); +} + +``` + +--- + +## app/lib/posthog-middleware.ts + +```ts +import { PostHog } from "posthog-node"; +import type { RouterContextProvider } from "react-router"; +import type { Route } from "../+types/root"; + +export interface PostHogContext extends RouterContextProvider { + posthog?: PostHog; +} + +export const posthogMiddleware: Route.MiddlewareFunction = async ({ request, context }, next) => { + const posthog = new PostHog(process.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN!, { + host: process.env.VITE_PUBLIC_POSTHOG_HOST!, + flushAt: 1, + flushInterval: 0, + }); + + const sessionId = request.headers.get('X-POSTHOG-SESSION-ID'); + const distinctId = request.headers.get('X-POSTHOG-DISTINCT-ID'); + + (context as PostHogContext).posthog = posthog; + + const response = await posthog.withContext( + { sessionId: sessionId ?? undefined, distinctId: distinctId ?? undefined }, + next + ); + + await posthog.shutdown().catch(() => {}); + + return response; +}; + + +``` + +--- + +## app/root.tsx + +```tsx +import { usePostHog } from '@posthog/react'; +import { + isRouteErrorResponse, + Links, + Meta, + Outlet, + Scripts, + ScrollRestoration, +} from "react-router"; + +import type { Route } from "./+types/root"; +import "./app.css"; +import "./globals.css"; +import Header from "./components/Header"; +import { AuthProvider } from "./contexts/AuthContext"; +import { posthogMiddleware } from "./lib/posthog-middleware"; + +export const middleware: Route.MiddlewareFunction[] = [ + posthogMiddleware, +]; + +export const links: Route.LinksFunction = () => [ + { rel: "preconnect", href: "https://fonts.googleapis.com" }, + { + rel: "preconnect", + href: "https://fonts.gstatic.com", + crossOrigin: "anonymous", + }, + { + rel: "stylesheet", + href: "https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap", + }, +]; + +export function Layout({ children }: { children: React.ReactNode }) { + return ( + + + + + + + + + {children} + + + + + ); +} + +export default function App() { + return ( + +
+
+ +
+ + ); +} + +export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) { + let message = "Oops!"; + let details = "An unexpected error occurred."; + let stack: string | undefined; + + const posthog = usePostHog(); + posthog.captureException(error); + + if (isRouteErrorResponse(error)) { + message = error.status === 404 ? "404" : "Error"; + details = + error.status === 404 + ? "The requested page could not be found." + : error.statusText || details; + } else if (import.meta.env.DEV && error && error instanceof Error) { + details = error.message; + stack = error.stack; + } + + return ( +
+

{message}

+

{details}

+ {stack && ( +
+          {stack}
+        
+ )} +
+ ); +} + +``` + +--- + +## app/routes.ts + +```ts +import { type RouteConfig, index, route } from "@react-router/dev/routes"; + +export default [ + index("routes/home.tsx"), + route("burrito", "routes/burrito.tsx"), + route("profile", "routes/profile.tsx"), + route("error", "routes/error.tsx"), + route("api/auth/login", "routes/api.auth.login.ts"), + route("api/burrito/consider", "routes/api.burrito.consider.ts"), +] satisfies RouteConfig; + +``` + +--- + +## app/routes/api.auth.login.ts + +```ts +import type { Route } from "./+types/api.auth.login"; +import { getBurritoConsiderations } from "../lib/db"; +import type { PostHogContext } from "../lib/posthog-middleware"; + +const users = new Map(); + +export { users }; + +export async function action({ request, context }: Route.ActionArgs) { + const body = await request.json(); + const { username, password } = body; + + if (!username || !password) { + return Response.json({ error: 'Username and password required' }, { status: 400 }); + } + + let user = users.get(username); + + if (!user) { + user = { username }; + users.set(username, user); + } + + const posthog = (context as PostHogContext).posthog; + if (posthog) { + posthog.capture({ event: 'server_login' }); + } + + const burritoConsiderations = await getBurritoConsiderations(username); + + return Response.json({ + success: true, + user: { ...user, burritoConsiderations } + }); +} + +``` + +--- + +## app/routes/api.burrito.consider.ts + +```ts +import type { Route } from "./+types/api.burrito.consider"; +import { users } from "./api.auth.login"; +import { incrementBurritoConsiderations } from "../lib/db"; +import type { PostHogContext } from "../lib/posthog-middleware"; + +export async function action({ request, context }: Route.ActionArgs) { + const body = await request.json(); + const { username } = body; + + if (!username) { + return Response.json({ error: 'Username required' }, { status: 400 }); + } + + const user = users.get(username); + + if (!user) { + return Response.json({ error: 'User not found' }, { status: 404 }); + } + + const burritoConsiderations = await incrementBurritoConsiderations(username); + + const posthog = (context as PostHogContext).posthog; + posthog?.capture({ event: 'burrito_considered' }); + + return Response.json({ + success: true, + user: { ...user, burritoConsiderations } + }); +} + + +``` + +--- + +## app/routes/burrito.tsx + +```tsx +import { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router'; +import type { Route } from "./+types/burrito"; +import { useAuth } from '../contexts/AuthContext'; + +export function meta({}: Route.MetaArgs) { + return [ + { title: "Burrito Consideration - Burrito Consideration App" }, + { name: "description", content: "Consider the potential of burritos" }, + ]; +} + +export default function BurritoPage() { + const { user, setUser } = useAuth(); + const navigate = useNavigate(); + const [hasConsidered, setHasConsidered] = useState(false); + + useEffect(() => { + if (!user) { + navigate('/'); + } + }, [user, navigate]); + + if (!user) { + return null; + } + + const handleConsideration = async () => { + try { + const response = await fetch('/api/burrito/consider', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username: user.username }), + }); + + if (response.ok) { + const { user: updatedUser } = await response.json(); + setUser(updatedUser); + setHasConsidered(true); + setTimeout(() => setHasConsidered(false), 2000); + } else { + console.error('Failed to increment burrito considerations'); + } + } catch (err) { + console.error('Error considering burrito:', err); + } + }; + + return ( +
+

Burrito consideration zone

+

Take a moment to truly consider the potential of burritos.

+ +
+ + + {hasConsidered && ( +

+ Thank you for your consideration! Count: {user.burritoConsiderations} +

+ )} +
+ +
+

Consideration stats

+

Total considerations: {user.burritoConsiderations}

+
+
+ ); +} + + +``` + +--- + +## app/routes/error.tsx + +```tsx +import type { Route } from "./+types/error"; + +export function meta({}: Route.MetaArgs) { + return [ + { title: "Error Test - Burrito Consideration App" }, + { name: "description", content: "Test error boundary" }, + ]; +} + +export default function ErrorPage() { + // This will throw an error during render, which will be caught by ErrorBoundary + throw new Error('Test error for ErrorBoundary - this is a render-time error'); +} + + +``` + +--- + +## app/routes/home.tsx + +```tsx +import { useState } from 'react'; +import type { Route } from "./+types/home"; +import { useAuth } from '../contexts/AuthContext'; +import { usePostHog } from '@posthog/react'; + +export function meta({}: Route.MetaArgs) { + return [ + { title: "Burrito Consideration App" }, + { name: "description", content: "Consider the potential of burritos" }, + ]; +} + +export default function Home() { + const { user, login } = useAuth(); + const posthog = usePostHog(); + const [username, setUsername] = useState(''); + const [password, setPassword] = useState(''); + const [error, setError] = useState(''); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setError(''); + + try { + const success = await login(username, password); + if (success) { + // Identifying the user once on login/sign up is enough. + posthog?.identify(username); + + // Capture login event + posthog?.capture('user_logged_in'); + + setUsername(''); + setPassword(''); + } else { + setError('Please provide both username and password'); + } + } catch (err) { + console.error('Login failed:', err); + setError('An error occurred during login'); + } + }; + + if (user) { + return ( +
+

Welcome back, {user.username}!

+

You are now logged in. Feel free to explore:

+
    +
  • Consider the potential of burritos
  • +
  • View your profile and statistics
  • +
+
+ ); + } + + return ( +
+

Welcome to Burrito Consideration App

+

Please sign in to begin your burrito journey

+ +
+
+ + setUsername(e.target.value)} + placeholder="Enter any username" + /> +
+ +
+ + setPassword(e.target.value)} + placeholder="Enter any password" + /> +
+ + {error &&

{error}

} + + +
+ +

+ Note: This is a demo app. Use any username and password to sign in. +

+
+ ); +} + +``` + +--- + +## app/routes/profile.tsx + +```tsx +import { useEffect } from 'react'; +import { useNavigate } from 'react-router'; +import type { Route } from "./+types/profile"; +import { useAuth } from '../contexts/AuthContext'; +import posthog from 'posthog-js'; +import { usePostHog } from '@posthog/react'; + +export function meta({}: Route.MetaArgs) { + return [ + { title: "User Profile - Burrito Consideration App" }, + { name: "description", content: "View your profile and burrito consideration stats" }, + ]; +} + +export default function ProfilePage() { + const { user } = useAuth(); + const navigate = useNavigate(); + const posthog = usePostHog(); + + + useEffect(() => { + if (!user) { + navigate('/'); + } + }, [user, navigate]); + + if (!user) { + return null; + } + + const triggerTestError = () => { + try { + throw new Error('Test error for PostHog error tracking'); + } catch (err) { + console.error('Captured error:', err); + posthog.captureException(err); + } + }; + + return ( +
+

User Profile

+ +
+

Your Information

+

Username: {user.username}

+

Burrito Considerations: {user.burritoConsiderations}

+
+ +
+ +
+ +
+

Your Burrito Journey

+ {user.burritoConsiderations === 0 ? ( +

You haven't considered any burritos yet. Visit the Burrito Consideration page to start!

+ ) : user.burritoConsiderations === 1 ? ( +

You've considered the burrito potential once. Keep going!

+ ) : user.burritoConsiderations < 5 ? ( +

You're getting the hang of burrito consideration!

+ ) : user.burritoConsiderations < 10 ? ( +

You're becoming a burrito consideration expert!

+ ) : ( +

You are a true burrito consideration master! 🌯

+ )} +
+
+ ); +} + + +``` + +--- + +## app/welcome/welcome.tsx + +```tsx +import logoDark from "./logo-dark.svg"; +import logoLight from "./logo-light.svg"; + +export function Welcome() { + return ( +
+
+
+
+ React Router + React Router +
+
+
+ +
+
+
+ ); +} + +const resources = [ + { + href: "https://reactrouter.com/docs", + text: "React Router Docs", + icon: ( + + + + ), + }, + { + href: "https://rmx.as/discord", + text: "Join Discord", + icon: ( + + + + ), + }, +]; + +``` + +--- + +## react-router.config.ts + +```ts +import type { Config } from "@react-router/dev/config"; + +export default { + // Config options... + // Server-side render by default, to enable SPA mode set this to `false` + ssr: true, + future: { + v8_middleware: true, + }, +} satisfies Config; + +``` + +--- + +## vite.config.ts + +```ts +import { reactRouter } from "@react-router/dev/vite"; +import tailwindcss from "@tailwindcss/vite"; +import { defineConfig, loadEnv } from "vite"; +import tsconfigPaths from "vite-tsconfig-paths"; + +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), ''); + + return { + plugins: [tailwindcss(), reactRouter(), tsconfigPaths()], + ssr: { + noExternal: ['posthog-js', '@posthog/react'], + }, + server: { + proxy: { + '/ingest/static': { + target: 'https://us-assets.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + }, + '/ingest/array': { + target: 'https://us-assets.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + }, + '/ingest': { + target: env.VITE_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + }, + }, + }, + }; +}); + +``` + +--- + diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/identify-users.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/identify-users.md new file mode 100644 index 000000000..1417e03a8 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/identify-users.md @@ -0,0 +1,272 @@ +# Identify users - Docs + +Linking events to specific users enables you to build a full picture of how they're using your product across different sessions, devices, and platforms. + +This is straightforward to do when [capturing backend events](/docs/product-analytics/capture-events?tab=Node.js.md), as you associate events to a specific user using a `distinct_id`, which is a required argument. + +However, in the frontend of a [web](/docs/libraries/js/features.md#capturing-events) or [mobile app](/docs/libraries/ios.md#capturing-events), a `distinct_id` is not a required argument — PostHog's SDKs will generate an anonymous `distinct_id` for you automatically and you can capture events anonymously, provided you use the appropriate [configuration](/docs/libraries/js/features.md#capturing-anonymous-events). + +To link events to specific users, call `identify`: + +PostHog AI + +### Web + +```javascript +posthog.identify( + 'distinct_id', // Replace 'distinct_id' with your user's unique identifier + { email: 'max@hedgehogmail.com', name: 'Max Hedgehog' } // optional: set additional person properties +); +``` + +### Android + +```kotlin +PostHog.identify( + distinctId = distinctID, // Replace 'distinctID' with your user's unique identifier + // optional: set additional person properties + userProperties = mapOf( + "name" to "Max Hedgehog", + "email" to "max@hedgehogmail.com" + ) +) +``` + +### iOS + +```swift +PostHogSDK.shared.identify("distinct_id", // Replace "distinct_id" with your user's unique identifier + userProperties: ["name": "Max Hedgehog", "email": "max@hedgehogmail.com"]) // optional: set additional person properties +``` + +### React Native + +```jsx +posthog.identify('distinct_id', { // Replace "distinct_id" with your user's unique identifier + email: 'max@hedgehogmail.com', // optional: set additional person properties + name: 'Max Hedgehog' +}) +``` + +### Dart + +```dart +await Posthog().identify( + userId: 'distinct_id', // Replace "distinct_id" with your user's unique identifier + userProperties: { + 'email': 'max@hedgehogmail.com', // optional: set additional person properties + 'name': 'Max Hedgehog', + }, +); +``` + +Events captured after calling `identify` are identified events and this creates a person profile if one doesn't exist already. + +Due to the cost of processing them, anonymous events can be up to 4x cheaper than identified events, so it's recommended you only capture identified events when needed. + +## How identify works + +When a user starts browsing your website or app, PostHog automatically assigns them an **anonymous ID**, which is stored locally. + +Provided you've [configured persistence](/docs/libraries/js/persistence.md) to use cookies or `localStorage`, this enables us to track anonymous users – even across different sessions. + +By calling `identify` with a `distinct_id` of your choice (usually the user's ID in your database, or their email), you link the anonymous ID and distinct ID together. + +Thus, all past and future events made with that anonymous ID are now associated with the distinct ID. + +This enables you to do things like associate events with a user from before they log in for the first time, or associate their events across different devices or platforms. + +Using identify in the backend + +Although you can call `identify` using our backend SDKs, it is used most in frontends. This is because there is no concept of anonymous sessions in the backend SDKs, so calling `identify` only updates person profiles. + +## Best practices when using `identify` + +### 1\. Call `identify` as soon as you're able to + +In your frontend, you should call `identify` as soon as you're able to. + +Typically, this is every time your **app loads** for the first time, and directly after your **users log in**. + +This ensures that events sent during your users' sessions are correctly associated with them. + +You only need to call `identify` once per session, and you should avoid calling it multiple times unnecessarily. + +If you call `identify` multiple times with the same data without reloading the page in between, PostHog will ignore the subsequent calls. + +### 2\. Use unique strings for distinct IDs + +If two users have the same distinct ID, their data is merged and they are considered one user in PostHog. Two common ways this can happen are: + +- Your logic for generating IDs does not generate sufficiently strong IDs and you can end up with a clash where 2 users have the same ID. +- There's a bug, typo, or mistake in your code leading to most or all users being identified with generic IDs like `null`, `true`, or `distinctId`. + +PostHog also has built-in protections to stop the most common distinct ID mistakes. + +### 3\. Reset after logout + +If a user logs out on your frontend, you should call `reset()` to unlink any future events made on that device with that user. + +This is important if your users are sharing a computer, as otherwise all of those users are grouped together into a single user due to shared cookies between sessions. + +**We strongly recommend you call `reset` on logout even if you don't expect users to share a computer.** + +You can do that like so: + +PostHog AI + +### Web + +```javascript +posthog.reset() +``` + +### iOS + +```swift +PostHogSDK.shared.reset() +``` + +### Android + +```kotlin +PostHog.reset() +``` + +### React Native + +```jsx +posthog.reset() +``` + +### Dart + +```dart +await Posthog().reset(); +``` + +If you *also* want to reset the `device_id` so that the device will be considered a new device in future events, you can pass `true` as an argument: + +Web + +PostHog AI + +```javascript +posthog.reset(true) +``` + +### 4\. Person profiles and properties + +You'll notice that one of the parameters in the `identify` method is a `properties` object. + +This enables you to set [person properties](/docs/product-analytics/person-properties.md). + +Whenever possible, we recommend passing in all person properties you have available each time you call identify, as this ensures their person profile on PostHog is up to date. + +Person properties can also be set being adding a `$set` property to a event `capture` call. + +See our [person properties docs](/docs/product-analytics/person-properties.md) for more details on how to work with them and best practices. + +### 5\. Use deep links between platforms + +We recommend you call `identify` [as soon as you're able](#1-call-identify-as-soon-as-youre-able), typically when a user signs up or logs in. + +This doesn't work if one or both platforms are unauthenticated. Some examples of such cases are: + +- Onboarding and signup flows before authentication. +- Unauthenticated web pages redirecting to authenticated mobile apps. +- Authenticated web apps prompting an app download. + +In these cases, you can use a [deep link](https://developer.android.com/training/app-links/deep-linking) on Android and [universal links](https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app) on iOS to identify users. + +1. Use `posthog.get_distinct_id()` to get the current distinct ID. Even if you cannot call identify because the user is unauthenticated, this will return an anonymous distinct ID generated by PostHog. +2. Add the distinct ID to the deep link as query parameters, along with other properties like UTM parameters. +3. When the user is redirected to the app, parse the deep link and handle the following cases: + +- The mobile app is already authenticated. In this case, call [`posthog.alias()`](/docs/libraries/js/features.md#alias) with the distinct ID from the web. This associates the two distinct IDs as a single person. +- The mobile app is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features.md#identifying-users) with the distinct ID from the web so pre-login mobile events stay connected to the web session. When the user later logs in on mobile, call `identify()` again with your canonical user ID. + +As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms. + +Here's an example implementation for handling deep links from web to mobile: + +PostHog AI + +### iOS + +```swift +import PostHog +class DeepLinkIdentityManager { + static let shared = DeepLinkIdentityManager() + // MARK: - Deep Link Received + func handleDeepLink(_ url: URL, isAuthenticatedOnMobile: Bool) { + guard let webDistinctId = URLComponents(url: url, resolvingAgainstBaseURL: true)? + .queryItems?.first(where: { $0.name == "ph_distinct_id" })?.value else { + return + } + if isAuthenticatedOnMobile { + // The mobile app already knows the current user. + // Alias the incoming web distinct ID to that user. + PostHogSDK.shared.alias(webDistinctId) + } else { + // Reuse the web distinct ID until login on mobile. + PostHogSDK.shared.identify(webDistinctId) + } + } + // MARK: - Login/Signup + func handleLogin(canonicalUserId: String) { + // Switch from the web distinct ID (or a mobile anon ID) + // to your canonical user ID. + PostHogSDK.shared.identify(canonicalUserId) + // Set user properties, track signup event, etc. + } + func handleLogout() { + PostHogSDK.shared.reset() + } +} +``` + +### Android + +```kotlin +import android.net.Uri +import com.posthog.PostHog +object DeepLinkIdentityManager { + // Deep Link Received + fun handleDeepLink(uri: Uri, isAuthenticatedOnMobile: Boolean) { + val webDistinctId = uri.getQueryParameter("ph_distinct_id") ?: return + if (isAuthenticatedOnMobile) { + // The mobile app already knows the current user. + // Alias the incoming web distinct ID to that user. + PostHog.alias(webDistinctId) + } else { + // Reuse the web distinct ID until login on mobile. + PostHog.identify(webDistinctId) + } + } + // Login/Signup + fun handleLogin(canonicalUserId: String) { + // Switch from the web distinct ID (or a mobile anon ID) + // to your canonical user ID. + PostHog.identify(canonicalUserId) + // Set user properties, track signup event, etc. + } + fun handleLogout() { + PostHog.reset() + } +} +``` + +## Further reading + +- [Identifying users docs](/docs/product-analytics/identify.md) +- [How person processing works](/docs/how-posthog-works/ingestion-pipeline.md#2-person-processing) +- [An introductory guide to identifying users in PostHog](/tutorials/identifying-users-guide.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/react-router-v7-framework-mode.md b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/react-router-v7-framework-mode.md new file mode 100644 index 000000000..64412f4f8 --- /dev/null +++ b/apps/basic-integration/react-router/react-router-v7-project/.claude/skills/integration-react-react-router-7-framework/references/react-router-v7-framework-mode.md @@ -0,0 +1,479 @@ +# React Router V7 framework mode (Remix V3) - Docs + +This guide walks you through setting up PostHog for React Router V7 in framework mode. If you're using React Router in another mode, find the guide for that mode in the [React Router page](/docs/libraries/react-router.md). If you're using React with another framework, go to the [React integration guide](/docs/libraries/react.md). + +1. 1 + + ## Install client-side SDKs + + Required + + First, you'll need to install [`posthog-js`](https://github.com/posthog/posthog-js) and `@posthog/react` using your package manager. These packages allow you to capture **client-side** events. + + PostHog AI + + ### npm + + ```bash + npm install --save posthog-js @posthog/react + ``` + + ### Yarn + + ```bash + yarn add posthog-js @posthog/react + ``` + + ### pnpm + + ```bash + pnpm add posthog-js @posthog/react + ``` + + ### Bun + + ```bash + bun add posthog-js @posthog/react + ``` + + In framework mode, you'll also need to set `posthog-js` and `@posthog/react` as external packages in your `vite.config.ts` file to avoid SSR errors. + + vite.config.ts + + PostHog AI + + ```typescript + // ... imports + export default defineConfig({ + plugins: [tailwindcss(), reactRouter(), tsconfigPaths()], + ssr: { + noExternal: ['posthog-js', '@posthog/react'] + } + }); + ``` + +2. 2 + + ## Add your environment variables + + Required + + Add your environment variables to your `.env.local` file and to your hosting provider (e.g. Vercel, Netlify, AWS). You can find your project token and host in [your project settings](https://us.posthog.com/settings/project). If you're using Vite, prefixing variable names with `VITE_` ensures they are accessible in the frontend. + + .env.local + + PostHog AI + + ```shell + VITE_POSTHOG_PROJECT_TOKEN= + VITE_POSTHOG_HOST=https://us.i.posthog.com + ``` + +3. 3 + + ## Add the PostHogProvider to your app + + Required + + In framework mode, your app enters from the `app/entry.client.tsx` file. In this file, you'll need to initialize the PostHog SDK and pass it to your app through the `PostHogProvider` context. + + app/entry.client.tsx + + PostHog AI + + ```jsx + import { startTransition, StrictMode } from "react"; + import { hydrateRoot } from "react-dom/client"; + import { HydratedRouter } from "react-router/dom"; + import posthog from 'posthog-js'; + import { PostHogProvider } from '@posthog/react' + posthog.init(import.meta.env.VITE_POSTHOG_PROJECT_TOKEN, { + api_host: import.meta.env.VITE_POSTHOG_HOST, + defaults: '2026-05-30', + tracing_headers: [ window.location.hostname, 'localhost' ], + }); + startTransition(() => { + hydrateRoot( + document, + {/* Pass PostHog client through PostHogProvider */} + + + + + , + ); + }); + ``` + + To help PostHog track your user sessions across the client and server, you'll need to add the `tracing_headers: ['your-backend-hostname1.com', 'your-backend-hostname2.com', ...]` option to your PostHog initialization. This adds the `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to requests sent to the configured hostnames, which we'll later use on the server-side. + + TypeError: Cannot read properties of undefined + + If you see the error `TypeError: Cannot read properties of undefined (reading '...')` this is likely because you tried to call a posthog function when posthog was not initialized (such as during the initial render). On purpose, we still render the children even if PostHog is not initialized so that your app still loads even if PostHog can't load. + + To fix this error, add a check that posthog has been initialized such as: + + React + + PostHog AI + + ```jsx + useEffect(() => { + posthog?.capture('test') // using optional chaining (recommended) + if (posthog) { + posthog.capture('test') // using an if statement + } + }, [posthog]) + ``` + + Typescript helps protect against these errors. + +4. ## Verify client-side events are captured + + Checkpoint + + *Confirm that you can capture client-side events and see them in your PostHog project* + + At this point, you should be able to capture client-side events and see them in your PostHog project. This includes basic events like page views and button clicks that are [autocaptured](/docs/product-analytics/autocapture.md). + + You can also try to capture a custom event to verify it's working. You can access PostHog in any component using the `usePostHog` hook. + + TSX + + PostHog AI + + ```jsx + import { usePostHog } from '@posthog/react' + function App() { + const posthog = usePostHog() + return + } + ``` + + You should see these events in a minute or two in the [activity tab](https://app.posthog.com/activity/explore). + +5. 4 + + ## Access PostHog methods + + Required + + On the client-side, you can access the PostHog client using the `usePostHog` hook. This hook returns the initialized PostHog client, which you can use to call PostHog methods. For example: + + TSX + + PostHog AI + + ```jsx + import { usePostHog } from '@posthog/react' + function App() { + const posthog = usePostHog() + return + } + ``` + + For a complete list of available methods, see the [posthog-js documentation](/docs/libraries/js.md). + +6. 5 + + ## Identify your user + + Recommended + + Now that you can capture basic client-side events, you'll want to identify your user so you can associate users with captured events. + + Generally, you identify users when they log in or when they input some identifiable information (e.g. email, name, etc.). You can identify users by calling the `identify` method on the PostHog client: + + TSX + + PostHog AI + + ```jsx + export default function Login() { + const { user, login } = useAuth(); + const posthog = usePostHog(); + const handleLogin = async (e: React.FormEvent) => { + // existing code to handle login... + const user = await login({ email, password }); + posthog?.identify(user.email, + { + email: user.email, + name: user.name, + } + ); + posthog?.capture('user_logged_in'); + }; + return ( +
+ {/* ... existing code ... */} + +
+ ); + } + ``` + + PostHog automatically generates anonymous IDs for users before they're identified. When you call identify, a new identified person is created. All previous events tracked with the anonymous ID link to the new identified distinct ID, and all future captures on the same browser associate with the identified person. + +7. 6 + + ## Create an error boundary + + Recommended + + PostHog can capture exceptions thrown in your app through an error boundary. React Router in framework mode has a built-in error boundary that you can use to capture exceptions. You can create an error boundary by exporting `ErrorBoundary` from your `app/root.tsx` file. + + app/root.tsx + + PostHog AI + + ```jsx + import { usePostHog } from '@posthog/react' + export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) { + const posthog = usePostHog(); + posthog?.captureException(error); + // other error handling code... + return ( +
+

Something went wrong

+

{error.message}

+
+ ); + } + ``` + + This automatically captures exceptions thrown in your React Router app using the `posthog.captureException()` method. + +8. 7 + + ## Tracking element visibility + + Recommended + + The `PostHogCaptureOnViewed` component enables you to automatically capture events when elements scroll into view in the browser. This is useful for tracking impressions of important content, monitoring user engagement with specific sections, or understanding which parts of your page users are actually seeing. + + The component wraps your content and sends a `$element_viewed` event to PostHog when the wrapped element becomes visible in the viewport. It only fires once per component instance. + + **Basic usage:** + + React + + PostHog AI + + ```jsx + import { PostHogCaptureOnViewed } from '@posthog/react' + function App() { + return ( + +
Your important content here
+
+ ) + } + ``` + + **With custom properties:** + + You can include additional properties with the event to provide more context: + + React + + PostHog AI + + ```jsx + + + + ``` + + **Tracking multiple children:** + + Use `trackAllChildren` to track each child element separately. This is useful for galleries or lists where you want to know which specific items were viewed: + + React + + PostHog AI + + ```jsx + + + + + + ``` + + When `trackAllChildren` is enabled, each child element sends its own event with a `child_index` property indicating its position. + + **Custom intersection observer options:** + + You can customize when elements are considered "viewed" by passing options to the `IntersectionObserver`: + + React + + PostHog AI + + ```jsx + +