diff --git a/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/SKILL.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/SKILL.md new file mode 100644 index 0000000..7993371 --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/SKILL.md @@ -0,0 +1,53 @@ +--- +name: 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. `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 + +- `EXAMPLE.md` - React Router v7 - Framework mode example project code +- `react-router-v7-framework-mode.md` - React router v7 framework mode (remix v3) - docs +- `identify-users.md` - Identify users - docs +- `basic-integration-1.0-begin.md` - PostHog setup - begin +- `basic-integration-1.1-edit.md` - PostHog setup - edit +- `basic-integration-1.2-revise.md` - PostHog setup - revise +- `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 + +- Never use useEffect() for analytics capture - it's brittle and causes errors +- Prefer event handlers or routing mechanisms to trigger analytics calls +- Add handlers where user actions occur rather than reacting to state changes +- Remember that source code is available in the node_modules directory + +## Identifying users + +Call `posthog.identify()` on the client side during login and signup events. Use form contents to identify users on submit. If server-side code exists, 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/react-router/shopper/.claude/skills/react-react-router-7-framework/references/EXAMPLE.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/EXAMPLE.md new file mode 100644 index 0000000..a49c96c --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/EXAMPLE.md @@ -0,0 +1,1208 @@ +# PostHog React Router v7 - Framework mode Example Project + +Repository: https://github.com/PostHog/examples +Path: basics/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_KEY=your_posthog_project_api_key +VITE_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com +``` + +Get your PostHog API key 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_KEY, { + api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + defaults: '2025-11-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_KEY!, { + 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_KEY= +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_KEY, { + api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + defaults: '2025-11-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_KEY!, { + 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': { + target: env.VITE_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + }, + }, + }, + }; +}); + +``` + +--- + diff --git a/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.0-begin.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.0-begin.md new file mode 100644 index 0000000..953ead2 --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.0-begin.md @@ -0,0 +1,43 @@ +--- +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. + +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. + +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: event name, event description, and the file path we want to place the event in. 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 + + +--- + +**Upon completion, continue with:** [basic-integration-1.1-edit.md](basic-integration-1.1-edit.md) \ No newline at end of file diff --git a/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.1-edit.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.1-edit.md new file mode 100644 index 0000000..44c1a4e --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.1-edit.md @@ -0,0 +1,37 @@ +--- +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. + +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:** [basic-integration-1.2-revise.md](basic-integration-1.2-revise.md) \ No newline at end of file diff --git a/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.2-revise.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.2-revise.md new file mode 100644 index 0000000..26f3a60 --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.2-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. + +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. + +## 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:** [basic-integration-1.3-conclude.md](basic-integration-1.3-conclude.md) \ No newline at end of file diff --git a/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.3-conclude.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.3-conclude.md new file mode 100644 index 0000000..552118f --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/basic-integration-1.3-conclude.md @@ -0,0 +1,36 @@ +--- +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" based on the events created here. 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. + +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, along with a list of links for the dashboard and insights created. 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] + +### 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. + + + +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/react-router/shopper/.claude/skills/react-react-router-7-framework/references/identify-users.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/identify-users.md new file mode 100644 index 0000000..ce16545 --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/identify-users.md @@ -0,0 +1,202 @@ +# 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#capturing-events.md) or [mobile app](/docs/libraries/ios#capturing-events.md), 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#capturing-anonymous-events.md). + +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 +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 user is already authenticated on the mobile app. In this case, call [`posthog.alias()`](/docs/libraries/js/features#alias.md) with the distinct ID from the web. This associates the two distinct IDs as a single person. +- The user is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features#identifying-users.md) with the distinct ID from the web. Events will be associated with this distinct ID. + +As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms. + +## Further reading + +- [Identifying users docs](/docs/product-analytics/identify.md) +- [How person processing works](/docs/how-posthog-works/ingestion-pipeline#2-person-processing.md) +- [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/react-router/shopper/.claude/skills/react-react-router-7-framework/references/react-router-v7-framework-mode.md b/apps/react-router/shopper/.claude/skills/react-react-router-7-framework/references/react-router-v7-framework-mode.md new file mode 100644 index 0000000..9071e8b --- /dev/null +++ b/apps/react-router/shopper/.claude/skills/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 API key and host in [your project settings](https://us.posthog.com/settings/project). If you're using Vite, including `VITE_PUBLIC_` in their names ensures they are accessible in the frontend. + + .env.local + + PostHog AI + + ```shell + VITE_PUBLIC_POSTHOG_KEY= + VITE_PUBLIC_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_PUBLIC_POSTHOG_KEY, { + api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + defaults: '2025-11-30', + __add_tracing_headers: [ window.location.host, '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 `__add_tracing_headers: ['your-backend-domain1.com', 'your-backend-domain2.com', ...]` option to your PostHog initialization. This adds the `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to your requests, 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 + +