diff --git a/server/.env.example b/server/.env.example index 260db4c..1bf7f48 100644 --- a/server/.env.example +++ b/server/.env.example @@ -1,6 +1,8 @@ JUDGE0_ENDPOINT= JUDGE0_AUTH_KEY= +# configure sentry endpoint here +SENTRY_DSN= # For who uses RapidAPI, also configure the following: # determine whether or not to use rapidapi (case-sensitive - True/False) IS_RAPIDAPI= diff --git a/server/main.py b/server/main.py index b4034cd..a7401f4 100644 --- a/server/main.py +++ b/server/main.py @@ -4,6 +4,8 @@ from fastapi import FastAPI, Request, HTTPException, status from fastapi.middleware.cors import CORSMiddleware from dotenv import load_dotenv +import sentry_sdk +from sentry_sdk import metrics from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.util import get_remote_address from slowapi.errors import RateLimitExceeded @@ -13,6 +15,15 @@ import logging from rich.logging import RichHandler + + +sentry_sdk.init( + dsn=os.getenv('SENTRY_DSN'), + # Add data like request headers and IP for users, + # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info + send_default_pii=True, +) + log = logging.getLogger("rich") log.setLevel(logging.INFO) log.handlers.clear() @@ -42,6 +53,7 @@ + @app.get("/") def read_root(): return {"Hello": "World"} @@ -53,6 +65,11 @@ def read_root(): # todo: prob need question id to do this, testing # code execution for now @app.post('/execute') +def judge0_execution(student_code: StudentCode): + # count to sentry for analytics + metrics.count("code.execution", 1) + # print student code + @limiter.limit("10/minute") async def judge0_execution(student_code: StudentCode, request: Request): # send the code to judge0 diff --git a/server/pyproject.toml b/server/pyproject.toml index cb1472c..fecc22d 100644 --- a/server/pyproject.toml +++ b/server/pyproject.toml @@ -9,6 +9,7 @@ dependencies = [ "python-dotenv>=1.2.2", "requests>=2.34.2", "rich>=15.0.0", + "sentry-sdk>=2.62.0", "httpx>=0.27.0", "slowapi>=0.1.9", ] diff --git a/server/uv.lock b/server/uv.lock index ab3d1c9..f3f6e39 100644 --- a/server/uv.lock +++ b/server/uv.lock @@ -758,6 +758,7 @@ dependencies = [ { name = "python-dotenv" }, { name = "requests" }, { name = "rich" }, + { name = "sentry-sdk" }, { name = "slowapi" }, ] @@ -768,6 +769,7 @@ requires-dist = [ { name = "python-dotenv", specifier = ">=1.2.2" }, { name = "requests", specifier = ">=2.34.2" }, { name = "rich", specifier = ">=15.0.0" }, + { name = "sentry-sdk", specifier = ">=2.62.0" }, { name = "slowapi", specifier = ">=0.1.9" }, ] diff --git a/web/.claude/skills/integration-tanstack-start/.posthog-wizard b/web/.claude/skills/integration-tanstack-start/.posthog-wizard new file mode 100644 index 0000000..e69de29 diff --git a/web/.claude/skills/integration-tanstack-start/SKILL.md b/web/.claude/skills/integration-tanstack-start/SKILL.md new file mode 100644 index 0000000..3904054 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/SKILL.md @@ -0,0 +1,63 @@ +--- +name: integration-tanstack-start +description: PostHog integration for TanStack Start full-stack applications +metadata: + author: PostHog + version: 1.23.2 +--- + +# PostHog integration for TanStack Start + +This skill helps you add PostHog analytics to TanStack Start 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` - TanStack Start 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/tanstack-start.md` - Tanstack start - 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) +- Use PostHogProvider in the root route (__root.tsx) for client-side tracking +- Use posthog-node for server-side event capture in API routes (src/routes/api/) - do NOT use posthog-js on the server +- Create a singleton PostHog server client to avoid re-initialization on every request +- Use TanStack Router's built-in navigation events for pageview tracking instead of useEffect +- Use PostHogProvider in the root component defined in either the file-based convention (__root.tsx) or code-based convention (wherever createRootRoute() is called) so all child routes have access to the PostHog client +- 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). + +## 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/web/.claude/skills/integration-tanstack-start/references/1-begin.md b/web/.claude/skills/integration-tanstack-start/references/1-begin.md new file mode 100644 index 0000000..55f0a83 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/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/web/.claude/skills/integration-tanstack-start/references/2-edit.md b/web/.claude/skills/integration-tanstack-start/references/2-edit.md new file mode 100644 index 0000000..e5f7ffd --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/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/web/.claude/skills/integration-tanstack-start/references/3-revise.md b/web/.claude/skills/integration-tanstack-start/references/3-revise.md new file mode 100644 index 0000000..3b07f50 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/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/web/.claude/skills/integration-tanstack-start/references/4-conclude.md b/web/.claude/skills/integration-tanstack-start/references/4-conclude.md new file mode 100644 index 0000000..d876d43 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/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/web/.claude/skills/integration-tanstack-start/references/EXAMPLE.md b/web/.claude/skills/integration-tanstack-start/references/EXAMPLE.md new file mode 100644 index 0000000..ce7c810 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/references/EXAMPLE.md @@ -0,0 +1,1193 @@ +# PostHog TanStack Start Example Project + +Repository: https://github.com/PostHog/context-mill +Path: example-apps/tanstack-start + +--- + +## README.md + +# PostHog TanStack Start example + +This is a [TanStack Start](https://tanstack.com/start) 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 automatically +- **User authentication**: Demo login system with PostHog user identification +- **Server-side & client-side tracking**: Complete examples of both tracking methods +- **Reverse proxy**: PostHog ingestion through Vite dev server proxy + +## Getting started + +### 1. Install dependencies + +```bash +npm 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 +``` + +Open [http://localhost:3000](http://localhost:3000) with your browser to see the app. + +## Project structure + +``` +src/ +├── components/ +│ └── Header.tsx # Navigation header with auth state +├── contexts/ +│ └── AuthContext.tsx # Authentication context with PostHog integration +├── utils/ +│ └── posthog-server.ts # Server-side PostHog client +├── routes/ +│ ├── __root.tsx # Root route with PostHogProvider +│ ├── index.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 +│ └── burrito/ +│ └── consider.ts # Burrito API with server-side tracking +└── styles.css # Global styles + +vite.config.ts # Vite config with PostHog proxy +.env # Environment variables +``` + +## Key integration points + +### Client-side initialization (routes/__root.tsx) + +PostHog is initialized using `PostHogProvider` from `@posthog/react`. The provider wraps the entire app in the root shell component and handles calling `posthog.init()` automatically: + +```typescript +import { PostHogProvider } from '@posthog/react' + + + {children} + +``` + +### Server-side setup (utils/posthog-server.ts) + +For server-side tracking, we use the `posthog-node` SDK with a singleton pattern: + +```typescript +import { PostHog } from 'posthog-node' + +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog( + process.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN || import.meta.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN!, + { + host: process.env.VITE_PUBLIC_POSTHOG_HOST || import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + flushAt: 1, + flushInterval: 0, + } + ) + } + return posthogClient +} +``` + +This client is used in API routes to track server-side events. + +### Server-side capture (routes/api/*) + +Server-side events include the client's `$session_id` so they appear in the same session in PostHog. The frontend sends it via a header: + +```typescript +// Frontend: include session ID in API requests +await fetch('/api/burrito/consider', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-PostHog-Session-Id': posthog.get_session_id() ?? '', + }, + body: JSON.stringify({ ... }), +}) +``` + +```typescript +// Server: read session ID from header and include in capture +import { getPostHogClient } from '../../utils/posthog-server' + +const sessionId = request.headers.get('X-PostHog-Session-Id') + +const posthog = getPostHogClient() +posthog.capture({ + distinctId: username, + event: 'burrito_considered', + properties: { + $session_id: sessionId || undefined, + username: username, + source: 'api', + }, +}) +``` + +### Reverse proxy configuration + +The Vite dev server is configured to proxy PostHog requests to avoid CORS issues and improve reliability: + +```typescript +server: { + proxy: { + '/ingest': { + target: 'https://us.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + secure: false, + }, + }, +} +``` + +### User identification (contexts/AuthContext.tsx) + +```typescript +import { usePostHog } from '@posthog/react' + +const posthog = usePostHog() + +posthog.identify(username, { + username: username, +}) +``` + +### Event tracking (routes/burrito.tsx) + +```typescript +import { usePostHog } from '@posthog/react' + +const posthog = usePostHog() + +posthog.capture('burrito_considered', { + total_considerations: user.burritoConsiderations + 1, + username: user.username, +}) +``` + +### Error tracking (routes/profile.tsx) + +```typescript +posthog.captureException(error) +``` + +## Learn more + +- [PostHog documentation](https://posthog.com/docs) +- [TanStack Start documentation](https://tanstack.com/start) +- [TanStack Router documentation](https://tanstack.com/router) +- [PostHog React integration](https://posthog.com/docs/libraries/react) +- [PostHog Node.js integration](https://posthog.com/docs/libraries/node) + +--- + +## .env.example + +```example +VITE_PUBLIC_POSTHOG_PROJECT_TOKEN= +VITE_PUBLIC_POSTHOG_HOST= + +``` + +--- + +## .prettierignore + +``` +package-lock.json +pnpm-lock.yaml +yarn.lock +``` + +--- + +## prettier.config.js + +```js +// @ts-check + +/** @type {import('prettier').Config} */ +const config = { + semi: false, + singleQuote: true, + trailingComma: "all", +}; + +export default config; + +``` + +--- + +## public/robots.txt + +```txt +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: + +``` + +--- + +## src/components/Header.tsx + +```tsx +import { Link } from '@tanstack/react-router' +import { useAuth } from '../contexts/AuthContext' + +export default function Header() { + const { user, logout } = useAuth() + + return ( +
+
+ +
+ {user ? ( + <> + Welcome, {user.username}! + + + ) : ( + Not logged in + )} +
+
+
+ ) +} + +``` + +--- + +## src/contexts/AuthContext.tsx + +```tsx +import { + createContext, + useContext, + useState, + ReactNode, +} from 'react' +import { usePostHog } from '@posthog/react' + +interface User { + username: string + burritoConsiderations: number +} + +interface AuthContextType { + user: User | null + login: (username: string, password: string) => Promise + logout: () => void + incrementBurritoConsiderations: () => void +} + +const AuthContext = createContext(undefined) + +const users: Map = new Map() + +export function AuthProvider({ children }: { children: ReactNode }) { + const posthog = usePostHog() + + // Use lazy initializer to read from localStorage only once on mount + 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', + 'X-PostHog-Session-Id': posthog.get_session_id() ?? '', + }, + body: JSON.stringify({ username, password }), + }) + + if (response.ok) { + const { user: userData } = await response.json() + + // Get or create user in local map + let localUser = users.get(username) + if (!localUser) { + localUser = userData as User + users.set(username, localUser) + } + + setUser(localUser) + if (typeof window !== 'undefined') { + localStorage.setItem('currentUser', username) + } + + // Identify user in PostHog using username as distinct ID + posthog.identify(username, { + username: username, + }) + + // Capture login event + posthog.capture('user_logged_in', { + username: username, + }) + + return true + } + return false + } catch (error) { + console.error('Login error:', error) + return false + } + } + + const logout = () => { + // Capture logout event before resetting + posthog.capture('user_logged_out') + posthog.reset() + + setUser(null) + if (typeof window !== 'undefined') { + localStorage.removeItem('currentUser') + } + } + + const incrementBurritoConsiderations = () => { + if (user) { + user.burritoConsiderations++ + users.set(user.username, user) + setUser({ ...user }) + } + } + + return ( + + {children} + + ) +} + +export function useAuth() { + const context = useContext(AuthContext) + if (context === undefined) { + throw new Error('useAuth must be used within an AuthProvider') + } + return context +} + +``` + +--- + +## src/router.tsx + +```tsx +import { createRouter } from '@tanstack/react-router' + +// Import the generated route tree +import { routeTree } from './routeTree.gen' + +// Create a new router instance +export const getRouter = () => { + return createRouter({ + routeTree, + scrollRestoration: true, + defaultPreloadStaleTime: 0, + }) +} + +``` + +--- + +## src/routes/__root.tsx + +```tsx +import { HeadContent, Scripts, createRootRoute } from '@tanstack/react-router' +import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools' +import { TanStackDevtools } from '@tanstack/react-devtools' +import { PostHogProvider } from '@posthog/react' + +import Header from '../components/Header' +import { AuthProvider } from '../contexts/AuthContext' + +import appCss from '../styles.css?url' + +export const Route = createRootRoute({ + head: () => ({ + meta: [ + { + charSet: 'utf-8', + }, + { + name: 'viewport', + content: 'width=device-width, initial-scale=1', + }, + { + title: 'TanStack Start Starter', + }, + ], + links: [ + { + rel: 'stylesheet', + href: appCss, + }, + ], + }), + + shellComponent: RootDocument, +}) + +function RootDocument({ children }: { children: React.ReactNode }) { + return ( + + + + + + + +
+ {children} + , + }, + ]} + /> + + + + + + ) +} + +``` + +--- + +## src/routes/api/auth/login.ts + +```ts +import { createFileRoute } from '@tanstack/react-router' +import { json } from '@tanstack/react-start' +import { getPostHogClient } from '../../../utils/posthog-server' + +export const Route = createFileRoute('/api/auth/login')({ + server: { + handlers: { + POST: async ({ request }) => { + const body = await request.json() + const { username, password } = body + + // Simple validation (in production, you'd verify against a real database) + if (!username || !password) { + return json( + { error: 'Username and password required' }, + { status: 400 }, + ) + } + + // Check if this is a new user (simplified - in production use a database) + const isNewUser = !username + + // Create or get user + const user = { + username, + burritoConsiderations: 0, + } + + const sessionId = request.headers.get('X-PostHog-Session-Id') + + // Capture server-side login event + const posthog = getPostHogClient() + posthog.capture({ + distinctId: username, + event: 'server_login', + properties: { + $session_id: sessionId || undefined, + username: username, + isNewUser: isNewUser, + source: 'api', + }, + }) + + // Identify user on server side + posthog.identify({ + distinctId: username, + properties: { + username: username, + createdAt: isNewUser ? new Date().toISOString() : undefined, + }, + }) + + return json({ success: true, user }) + }, + }, + }, +}) + +``` + +--- + +## src/routes/api/burrito/consider.ts + +```ts +import { createFileRoute } from '@tanstack/react-router' +import { json } from '@tanstack/react-start' +import { getPostHogClient } from '../../../utils/posthog-server' + +export const Route = createFileRoute('/api/burrito/consider')({ + server: { + handlers: { + POST: async ({ request }) => { + const body = await request.json() + const { username, totalConsiderations } = body + + if (!username) { + return json( + { error: 'Username is required' }, + { status: 400 }, + ) + } + + const sessionId = request.headers.get('X-PostHog-Session-Id') + + const posthog = getPostHogClient() + posthog.capture({ + distinctId: username, + event: 'burrito_considered', + properties: { + $session_id: sessionId || undefined, + total_considerations: totalConsiderations, + username: username, + source: 'api', + }, + }) + + return json({ success: true }) + }, + }, + }, +}) + +``` + +--- + +## src/routes/burrito.tsx + +```tsx +import { createFileRoute, useNavigate } from '@tanstack/react-router' +import { useState } from 'react' +import { usePostHog } from '@posthog/react' +import { useAuth } from '../contexts/AuthContext' + +export const Route = createFileRoute('/burrito')({ + component: BurritoPage, + head: () => ({ + meta: [ + { + title: 'Burrito Consideration - Burrito Consideration App', + }, + { + name: 'description', + content: 'Consider the potential of burritos', + }, + ], + }), +}) + +function BurritoPage() { + const { user, incrementBurritoConsiderations } = useAuth() + const navigate = useNavigate() + const posthog = usePostHog() + const [hasConsidered, setHasConsidered] = useState(false) + + // Redirect to home if not logged in + if (!user) { + navigate({ to: '/' }) + return null + } + + const handleClientConsideration = () => { + incrementBurritoConsiderations() + setHasConsidered(true) + setTimeout(() => setHasConsidered(false), 2000) + + posthog.capture('burrito_considered', { + total_considerations: user.burritoConsiderations + 1, + username: user.username, + }) + } + + const handleServerConsideration = async () => { + incrementBurritoConsiderations() + setHasConsidered(true) + setTimeout(() => setHasConsidered(false), 2000) + + await fetch('/api/burrito/consider', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-PostHog-Session-Id': posthog.get_session_id() ?? '', + }, + body: JSON.stringify({ + username: user.username, + totalConsiderations: user.burritoConsiderations + 1, + }), + }) + } + + 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}

+
+
+
+ ) +} + +``` + +--- + +## src/routes/index.tsx + +```tsx +import { createFileRoute } from '@tanstack/react-router' +import { useState } from 'react' +import { useAuth } from '../contexts/AuthContext' + +export const Route = createFileRoute('/')({ + component: Home, + head: () => ({ + meta: [ + { + title: 'Burrito Consideration App', + }, + { + name: 'description', + content: 'Consider the potential of burritos', + }, + ], + }), +}) + +function Home() { + const { user, login } = useAuth() + 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) { + setUsername('') + setPassword('') + } else { + setError('Please provide both username and password') + } + } catch (err) { + console.error('Login failed:', err) + setError('An error occurred during login') + } + } + + return ( +
+ {user ? ( +
+

Welcome back, {user.username}!

+

You are now logged in. Feel free to explore:

+
    +
  • Consider the potential of burritos
  • +
  • View your profile and statistics
  • +
+
+ ) : ( +
+

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. +

+
+ )} +
+ ) +} + +``` + +--- + +## src/routes/profile.tsx + +```tsx +import { createFileRoute, useNavigate } from '@tanstack/react-router' +import { usePostHog } from '@posthog/react' +import { useAuth } from '../contexts/AuthContext' + +export const Route = createFileRoute('/profile')({ + component: ProfilePage, + head: () => ({ + meta: [ + { + title: 'Profile - Burrito Consideration App', + }, + { + name: 'description', + content: 'Your burrito consideration profile', + }, + ], + }), +}) + +function ProfilePage() { + const { user } = useAuth() + const navigate = useNavigate() + const posthog = usePostHog() + + // Redirect to home if not logged in + if (!user) { + navigate({ to: '/' }) + return null + } + + const triggerTestError = () => { + try { + throw new Error('Test error for PostHog error tracking') + } catch (err) { + posthog.captureException(err) + console.error('Captured error:', err) + alert('Error captured and sent to PostHog!') + } + } + + 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! 🌯

+ )} +
+
+
+ ) +} + +``` + +--- + +## src/routeTree.gen.ts + +```ts +/* eslint-disable */ + +// @ts-nocheck + +// noinspection JSUnusedGlobalSymbols + +// This file was automatically generated by TanStack Router. +// You should NOT make any changes in this file as it will be overwritten. +// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. + +import { Route as rootRouteImport } from './routes/__root' +import { Route as ProfileRouteImport } from './routes/profile' +import { Route as BurritoRouteImport } from './routes/burrito' +import { Route as IndexRouteImport } from './routes/index' +import { Route as ApiBurritoConsiderRouteImport } from './routes/api/burrito/consider' +import { Route as ApiAuthLoginRouteImport } from './routes/api/auth/login' + +const ProfileRoute = ProfileRouteImport.update({ + id: '/profile', + path: '/profile', + getParentRoute: () => rootRouteImport, +} as any) +const BurritoRoute = BurritoRouteImport.update({ + id: '/burrito', + path: '/burrito', + getParentRoute: () => rootRouteImport, +} as any) +const IndexRoute = IndexRouteImport.update({ + id: '/', + path: '/', + getParentRoute: () => rootRouteImport, +} as any) +const ApiBurritoConsiderRoute = ApiBurritoConsiderRouteImport.update({ + id: '/api/burrito/consider', + path: '/api/burrito/consider', + getParentRoute: () => rootRouteImport, +} as any) +const ApiAuthLoginRoute = ApiAuthLoginRouteImport.update({ + id: '/api/auth/login', + path: '/api/auth/login', + getParentRoute: () => rootRouteImport, +} as any) + +export interface FileRoutesByFullPath { + '/': typeof IndexRoute + '/burrito': typeof BurritoRoute + '/profile': typeof ProfileRoute + '/api/auth/login': typeof ApiAuthLoginRoute + '/api/burrito/consider': typeof ApiBurritoConsiderRoute +} +export interface FileRoutesByTo { + '/': typeof IndexRoute + '/burrito': typeof BurritoRoute + '/profile': typeof ProfileRoute + '/api/auth/login': typeof ApiAuthLoginRoute + '/api/burrito/consider': typeof ApiBurritoConsiderRoute +} +export interface FileRoutesById { + __root__: typeof rootRouteImport + '/': typeof IndexRoute + '/burrito': typeof BurritoRoute + '/profile': typeof ProfileRoute + '/api/auth/login': typeof ApiAuthLoginRoute + '/api/burrito/consider': typeof ApiBurritoConsiderRoute +} +export interface FileRouteTypes { + fileRoutesByFullPath: FileRoutesByFullPath + fullPaths: + | '/' + | '/burrito' + | '/profile' + | '/api/auth/login' + | '/api/burrito/consider' + fileRoutesByTo: FileRoutesByTo + to: + | '/' + | '/burrito' + | '/profile' + | '/api/auth/login' + | '/api/burrito/consider' + id: + | '__root__' + | '/' + | '/burrito' + | '/profile' + | '/api/auth/login' + | '/api/burrito/consider' + fileRoutesById: FileRoutesById +} +export interface RootRouteChildren { + IndexRoute: typeof IndexRoute + BurritoRoute: typeof BurritoRoute + ProfileRoute: typeof ProfileRoute + ApiAuthLoginRoute: typeof ApiAuthLoginRoute + ApiBurritoConsiderRoute: typeof ApiBurritoConsiderRoute +} + +declare module '@tanstack/react-router' { + interface FileRoutesByPath { + '/profile': { + id: '/profile' + path: '/profile' + fullPath: '/profile' + preLoaderRoute: typeof ProfileRouteImport + parentRoute: typeof rootRouteImport + } + '/burrito': { + id: '/burrito' + path: '/burrito' + fullPath: '/burrito' + preLoaderRoute: typeof BurritoRouteImport + parentRoute: typeof rootRouteImport + } + '/': { + id: '/' + path: '/' + fullPath: '/' + preLoaderRoute: typeof IndexRouteImport + parentRoute: typeof rootRouteImport + } + '/api/burrito/consider': { + id: '/api/burrito/consider' + path: '/api/burrito/consider' + fullPath: '/api/burrito/consider' + preLoaderRoute: typeof ApiBurritoConsiderRouteImport + parentRoute: typeof rootRouteImport + } + '/api/auth/login': { + id: '/api/auth/login' + path: '/api/auth/login' + fullPath: '/api/auth/login' + preLoaderRoute: typeof ApiAuthLoginRouteImport + parentRoute: typeof rootRouteImport + } + } +} + +const rootRouteChildren: RootRouteChildren = { + IndexRoute: IndexRoute, + BurritoRoute: BurritoRoute, + ProfileRoute: ProfileRoute, + ApiAuthLoginRoute: ApiAuthLoginRoute, + ApiBurritoConsiderRoute: ApiBurritoConsiderRoute, +} +export const routeTree = rootRouteImport + ._addFileChildren(rootRouteChildren) + ._addFileTypes() + +import type { getRouter } from './router.tsx' +import type { createStart } from '@tanstack/react-start' +declare module '@tanstack/react-start' { + interface Register { + ssr: true + router: Awaited> + } +} + +``` + +--- + +## src/utils/posthog-server.ts + +```ts +import { PostHog } from 'posthog-node' + +let posthogClient: PostHog | null = null + +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog( + process.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN || import.meta.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN!, + { + host: process.env.VITE_PUBLIC_POSTHOG_HOST || import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + flushAt: 1, + flushInterval: 0, + }, + ) + } + return posthogClient +} + + +``` + +--- + +## vite.config.ts + +```ts +import { defineConfig } from 'vite' +import { tanstackStart } from '@tanstack/react-start/plugin/vite' +import viteReact from '@vitejs/plugin-react' +import viteTsConfigPaths from 'vite-tsconfig-paths' + +const config = defineConfig({ + plugins: [ + // this is the plugin that enables path aliases + viteTsConfigPaths({ + projects: ['./tsconfig.json'], + }), + tanstackStart(), + viteReact(), + ], + server: { + proxy: { + '/ingest/static': { + target: 'https://us-assets.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + secure: false, + }, + '/ingest/array': { + target: 'https://us-assets.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + secure: false, + }, + '/ingest': { + target: 'https://us.i.posthog.com', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ingest/, ''), + secure: false, + }, + }, + }, +}) + +export default config + +``` + +--- + diff --git a/web/.claude/skills/integration-tanstack-start/references/identify-users.md b/web/.claude/skills/integration-tanstack-start/references/identify-users.md new file mode 100644 index 0000000..1417e03 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/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/web/.claude/skills/integration-tanstack-start/references/tanstack-start.md b/web/.claude/skills/integration-tanstack-start/references/tanstack-start.md new file mode 100644 index 0000000..0504850 --- /dev/null +++ b/web/.claude/skills/integration-tanstack-start/references/tanstack-start.md @@ -0,0 +1,193 @@ +# TanStack Start - Docs + +This tutorial shows how to integrate PostHog with a [TanStack Start](https://tanstack.com/start) app for both client-side and server-side analytics. + +## Installation + +Install the required packages: + +Terminal + +PostHog AI + +```bash +npm install @posthog/react posthog-node +``` + +- `@posthog/react` - React package for our [JS Web SDK](/docs/libraries/js.md) for client-side usage +- `posthog-node` - PostHog [Node.js SDK](/docs/libraries/node.md) for server-side event capture + +## Identifying users + +> **Identifying users is required.** Call `posthog.identify('your-user-id')` after login to link events to a known user. This is what connects frontend event captures, [session replays](/docs/session-replay.md), [LLM traces](/docs/ai-engineering.md), and [error tracking](/docs/error-tracking.md) to the same person — and lets backend events link back too. +> +> See our guide on [identifying users](/docs/getting-started/identify-users.md) for how to set this up. + +## Initialize PostHog on the client + +Wrap your app with `PostHogProvider` in your root route with your project token, host, and other options. + +src/routes/\_\_root.tsx + +PostHog AI + +```jsx +// src/routes/__root.tsx +import { HeadContent, Scripts, createRootRoute } from '@tanstack/react-router' +import { PostHogProvider } from '@posthog/react' +export const Route = createRootRoute({ + head: () => ({ + meta: [ + { charSet: 'utf-8' }, + { name: 'viewport', content: 'width=device-width, initial-scale=1' }, + ], + }), + shellComponent: RootDocument, +}) +function RootDocument({ children }: { children: React.ReactNode }) { + return ( + + + + + + + {children} + + + + + ) +} +``` + +Once the provider is in place, PostHog automatically captures pageviews, sessions, and web vitals. + +## Capture events on the client + +Use the `usePostHog` hook from `@posthog/react` in any component to capture custom events: + +src/routes/checkout.tsx + +PostHog AI + +```jsx +import { usePostHog } from '@posthog/react' +function CheckoutButton({ orderId, total }: { orderId: string; total: number }) { + const posthog = usePostHog() + const handleClick = () => { + posthog.capture('checkout_started', { + order_id: orderId, + total: total, + }) + } + return +} +``` + +### Identify users + +Call `posthog.identify()` when a user logs in to link their events to a user ID: + +TSX + +PostHog AI + +```jsx +import { usePostHog } from '@posthog/react' +function LoginForm() { + const posthog = usePostHog() + const handleLogin = async (userId: string, email: string) => { + // ... your login logic + posthog.identify(userId, { + email: email, + }) + posthog.capture('user_logged_in') + } +} +``` + +Call `posthog.reset()` on logout to clear the identified user. + +## Initialize PostHog on the server + +Create a server-side PostHog client using `posthog-node`. Use a singleton pattern so you reuse the same client across requests: + +src/utils/posthog-server.ts + +PostHog AI + +```typescript +// src/utils/posthog-server.ts +import { PostHog } from 'posthog-node' +let posthogClient: PostHog | null = null +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog( + '', + { + host: 'https://us.i.posthog.com', + flushAt: 1, + flushInterval: 0, + }, + ) + } + return posthogClient +} +``` + +## Capture events on the server + +Use the server client in TanStack Start API routes to capture events server-side. Server-side capture is useful for tracking events that shouldn't be spoofable from the client, like purchases or authentication: + +src/routes/api/checkout.ts + +PostHog AI + +```typescript +// src/routes/api/checkout.ts +import { createFileRoute } from '@tanstack/react-router' +import { json } from '@tanstack/react-start' +import { getPostHogClient } from '../../utils/posthog-server' +export const Route = createFileRoute('/api/checkout')({ + server: { + handlers: { + POST: async ({ request }) => { + const body = await request.json() + const posthog = getPostHogClient() + posthog.capture({ + distinctId: body.userId, + event: 'item_purchased', + properties: { + item_id: body.itemId, + price: body.price, + source: 'api', + }, + }) + return json({ success: true }) + }, + }, + }, +}) +``` + +The server-side `capture` call requires a `distinctId` (the user identifier), an `event` name, and optional `properties`. + +## Next steps + +Installing the JS Web SDK and Node SDK means all of their functionality is available in your TanStack Start project. To learn more about this, have a look at our [JS Web SDK docs](/docs/libraries/js/usage.md) and [Node SDK docs](/docs/libraries/node.md). + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/web/package.json b/web/package.json index b566a56..fbebe98 100644 --- a/web/package.json +++ b/web/package.json @@ -17,6 +17,7 @@ }, "dependencies": { "@monaco-editor/react": "^4.7.0", + "@posthog/react": "^1.10.2", "@tailwindcss/vite": "^4.1.18", "@tanstack/react-devtools": "latest", "@tanstack/react-form": "^1.33.0", @@ -35,6 +36,7 @@ "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "lucide-react": "^0.577.0", + "posthog-node": "^5.38.2", "radix-ui": "^1.5.0", "react": "^19.2.0", "react-dom": "^19.2.0", diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index d1ec527..f75a4e9 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -14,12 +14,15 @@ importers: '@monaco-editor/react': specifier: ^4.7.0 version: 4.7.0(monaco-editor@0.55.1)(react-dom@19.2.7(react@19.2.7))(react@19.2.7) + '@posthog/react': + specifier: ^1.10.2 + version: 1.10.2(@types/react@19.2.17)(posthog-js@1.391.2)(react@19.2.7) '@tailwindcss/vite': specifier: ^4.1.18 version: 4.3.0(vite@8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0)) '@tanstack/react-devtools': specifier: latest - version: 0.10.5(@types/react-dom@19.2.3(@types/react@19.2.17))(@types/react@19.2.17)(csstype@3.2.3)(react-dom@19.2.7(react@19.2.7))(react@19.2.7)(solid-js@1.9.13) + version: 0.10.7(@types/react-dom@19.2.3(@types/react@19.2.17))(@types/react@19.2.17)(csstype@3.2.3)(react-dom@19.2.7(react@19.2.7))(react@19.2.7)(solid-js@1.9.13) '@tanstack/react-form': specifier: ^1.33.0 version: 1.33.0(@tanstack/react-start@1.168.26(react-dom@19.2.7(react@19.2.7))(react@19.2.7)(vite@8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0)))(react-dom@19.2.7(react@19.2.7))(react@19.2.7) @@ -65,6 +68,9 @@ importers: lucide-react: specifier: ^0.577.0 version: 0.577.0(react@19.2.7) + posthog-node: + specifier: ^5.38.2 + version: 5.38.2 radix-ui: specifier: ^1.5.0 version: 1.5.0(@types/react-dom@19.2.3(@types/react@19.2.17))(@types/react@19.2.17)(react-dom@19.2.7(react@19.2.7))(react@19.2.7) @@ -95,10 +101,10 @@ importers: version: 0.5.20(tailwindcss@4.3.0) '@tanstack/devtools-event-client': specifier: latest - version: 0.4.3 + version: 0.4.4 '@tanstack/devtools-vite': specifier: latest - version: 0.7.0(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)(vite@8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0)) + version: 0.8.0(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)(vite@8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0)) '@tanstack/router-cli': specifier: ^1.132.0 version: 1.167.17 @@ -798,6 +804,22 @@ packages: '@oxc-project/types@0.133.0': resolution: {integrity: sha512-KzkdCd6Uxqnf6l3HOw1xfatAlUURA0g14cvBYFyJ5SaNOQbOUvBr9PKArcPcrNIeRsBdgcUzOGrhKveVpvOIGA==} + '@posthog/core@1.35.3': + resolution: {integrity: sha512-EsGPbSLl39Jgo2KZ+kI9UAxFnh5nddaN5bNm2rXvUwF+vGmam9eN1EXeNbxhRU7ulEeIiGdm7XjoU7pzavkgIQ==} + + '@posthog/react@1.10.2': + resolution: {integrity: sha512-/KXzUZiSByE9/6fHmnfDVVvld/3pvBuM6KFUuT5NkR4XwOHEAT/EwNwhUUFOOJpsyDa0PaEWWHZS6VY9r2CB3Q==} + peerDependencies: + '@types/react': '>=16.8.0' + posthog-js: '>=1.257.2' + react: '>=16.8.0' + peerDependenciesMeta: + '@types/react': + optional: true + + '@posthog/types@1.390.2': + resolution: {integrity: sha512-WcfKz2GNn2vfDX8vXmJYbKxegPxVWHuDQ/pHdAn0HoZDXDFnEp/+x3qBQA+fEvtbPjjtjgAt2wIgJMlM7asx7g==} + '@radix-ui/number@1.1.2': resolution: {integrity: sha512-ceTwaxc4I5IOi97DgCotl3pqiyRGvffcc0oOsE2dQYaJOFIDsDt4VWG6xEbg1QePv9QWausCEIppud/tJ1wNig==} @@ -1718,12 +1740,12 @@ packages: peerDependencies: vite: ^5.2.0 || ^6 || ^7 || ^8 - '@tanstack/devtools-client@0.0.6': - resolution: {integrity: sha512-f85ZJXJnDIFOoykG/BFIixuAevJovCvJF391LPs6YjBAPhGYC50NWlx1y4iF/UmK5/cCMx+/JqI5SBOz7FanQQ==} + '@tanstack/devtools-client@0.0.7': + resolution: {integrity: sha512-bAqBnXQlg/1PqmIC3XhqzG8jV3YmUQ41fD9VlOVzSilFBD4Kp6WJFJa+7N6TvlYXqMAy8xoeF9gg0d2Lt74OEQ==} engines: {node: '>=18'} - '@tanstack/devtools-event-bus@0.4.1': - resolution: {integrity: sha512-cNnJ89Q021Zf883rlbBTfsaxTfi2r73/qejGtyTa7ksErF3hyDyAq1aTbo5crK9dAL7zSHh9viKY1BtMls1QOA==} + '@tanstack/devtools-event-bus@0.4.2': + resolution: {integrity: sha512-2LHzhwBFlKHCcklsQrGe8TeyjHd4XAF8nuCO6wHmva5fePUkJUULbu6CsCNAlGlCi0KkEsMXZSvRdR4HgMq4yA==} engines: {node: '>=18'} '@tanstack/devtools-event-client@0.4.3': @@ -1731,21 +1753,26 @@ packages: engines: {node: '>=18'} hasBin: true - '@tanstack/devtools-ui@0.5.2': - resolution: {integrity: sha512-GtaMk8kaGZ9ZdR8Pu5RAfcse/ZrxzH/xsAIFtHMapLs2VMqSPFfb1NvIDO1MAAfUcub8Ix8XKQEP0uYSPzoFKw==} + '@tanstack/devtools-event-client@0.4.4': + resolution: {integrity: sha512-6T5Yop/793YI+H+5J8Hsyj4kCih9sl4t3ElLgKioW5hk3ocn+ZdSJ94tT7vL7uabxSugWYBZlOTMPzEw2puvQw==} + engines: {node: '>=18'} + hasBin: true + + '@tanstack/devtools-ui@0.5.3': + resolution: {integrity: sha512-iJjwWtdXhUGpeHyyW9+3NhXhmlVFhh3v3UBNKCouykG9UFXEtneVVNXgSRpd70DeYJFmvKOY19LafKRI5/cM7A==} engines: {node: '>=18'} peerDependencies: solid-js: '>=1.9.7' - '@tanstack/devtools-vite@0.7.0': - resolution: {integrity: sha512-VXki7K+Xwnpo3IKdNSWGe7YOvtZv33YlulGqaQ+YCpeQhYg8JFuxP50BXibDoRLj5EOX4r21Hs7COdxbRHXkTw==} + '@tanstack/devtools-vite@0.8.0': + resolution: {integrity: sha512-Pj4KB6dTK3NGjVxKRym8X0Df8rL/ofojYMVR8sw4jAT7doMCTNSW7VqXrYD5P2cKgyLJiwg3TNOozRm2o+7m3Q==} engines: {node: '>=18'} hasBin: true peerDependencies: vite: ^6.0.0 || ^7.0.0 || ^8.0.0 - '@tanstack/devtools@0.12.2': - resolution: {integrity: sha512-Xdl8pLzoDUvXaclQ0poY36WAPx0jEHk8vqUFd8FYFUm1BMshtB7RnTgD1HE9jCAXODxqw9I0gXBiUZLK3o3+Bw==} + '@tanstack/devtools@0.12.4': + resolution: {integrity: sha512-fYZ0KTEpKq7JyjULDe4kGQBN77aw5jtULs1MaVraWvwtcoIwe4UOKV48UJQ9/mEQMFTytbgKxYk2QaSDgI/Znw==} engines: {node: '>=18'} hasBin: true peerDependencies: @@ -1768,8 +1795,8 @@ packages: '@tanstack/query-devtools@5.101.0': resolution: {integrity: sha512-MVqw17k08RQtGGLEL654+dX/btbX9p/8WjkznO//zusLTMaObxi3Q+MoFwGVkC9K3tqjn8qrrNhJevXx4fJTeQ==} - '@tanstack/react-devtools@0.10.5': - resolution: {integrity: sha512-orVsRJ7oAXFb7oyafQCgx9YuK44jpILh5T/ddYuxAsolNfN5DZBr5/NLrWErD7HCGIzvYzg1TZI4sPxmiKvtvA==} + '@tanstack/react-devtools@0.10.7': + resolution: {integrity: sha512-AYHQH06uuK07Asqq8eASgJjpILlaFBpjnTesxx1JVHGoBl4ijwbyIlKnj3Z8+M8sEOPn5mvtV5o6mielPXKHWg==} engines: {node: '>=18'} peerDependencies: '@types/react': '>=16.8' @@ -2336,6 +2363,9 @@ packages: cookie-es@3.1.1: resolution: {integrity: sha512-UaXxwISYJPTr9hwQxMFYZ7kNhSXboMXP+Z3TRX6f1/NyaGPfuNUZOWP1pUEb75B2HjfklIYLVRfWiFZJyC6Npg==} + core-js@3.49.0: + resolution: {integrity: sha512-es1U2+YTtzpwkxVLwAFdSpaIMyQaq0PBgm3YD1W3Qpsn1NAmO3KSgZfu+oGSWVu6NvLHoHCV/aYcsE5wiB7ALg==} + css-tree@3.2.1: resolution: {integrity: sha512-X7sjQzceUhu1u7Y/ylrRZFU2FS6LRiFVp6rKLPg23y3x3c3DOKAwuXGDp+PAGjh6CSnCjYeAul8pcT8bAl+lSA==} engines: {node: ^10 || ^12.20.0 || ^14.13.0 || >=15.0.0} @@ -2405,6 +2435,9 @@ packages: dompurify@3.2.7: resolution: {integrity: sha512-WhL/YuveyGXJaerVlMYGWhvQswa7myDG17P7Vu65EWC05o8vfeNbvNf4d/BOvH99+ZW+LlQsc1GDKMa1vNK6dw==} + dompurify@3.4.11: + resolution: {integrity: sha512-zhlUV12GsaRzMsf9q5M254YhA4+VuF0fG+QFqu6aYpoGlKtz+w8//jBcGVYBgQkR5GHjUomejY84AV+/uPbWdw==} + dunder-proto@1.0.1: resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==} engines: {node: '>= 0.4'} @@ -2479,6 +2512,9 @@ packages: fetchdts@0.1.7: resolution: {integrity: sha512-YoZjBdafyLIop9lSxXVI33oLD5kN31q4Td+CasofLLYeLXRFeOsuOw0Uo+XNRi9PZlbfdlN2GmRtm4tCEQ9/KA==} + fflate@0.4.8: + resolution: {integrity: sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==} + follow-redirects@1.16.0: resolution: {integrity: sha512-y5rN/uOsadFT/JfYwhxRS5R7Qce+g3zG97+JrtFZlC9klX/W5hD7iiLzScI4nZqUS7DNUdhPgw4xI8W2LuXlUw==} engines: {node: '>=4.0'} @@ -2903,6 +2939,21 @@ packages: resolution: {integrity: sha512-FfR8sjd4em2T6fb3I2MwAJU7HWVMr9zba+enmQeeWFfCbm+UOC/0X4DS8XtpUTMwWMGbjKYP7xjfNekzyGmB3A==} engines: {node: ^10 || ^12 || >=14} + posthog-js@1.391.2: + resolution: {integrity: sha512-q0DZN6ljchSnAFJIXf+sQFTPlsLjTlRa+TvrL+QRb6413BGtib/MNiQy1bnwLKt8KR+f6xJYvkqdLyty9s4Aww==} + + posthog-node@5.38.2: + resolution: {integrity: sha512-eiKpU+vX4hVuHbO/EosvPHsmh2AVIdoVmWss/uUOs1t4b0ViCblw2o8OIFqHxKj3mYRnSOBlX0Dw3wBvcCaYpA==} + engines: {node: ^20.20.0 || >=22.22.0} + peerDependencies: + rxjs: ^7.0.0 + peerDependenciesMeta: + rxjs: + optional: true + + preact@10.29.2: + resolution: {integrity: sha512-7tNmwg/7mzzAoB/8kSg6Hl37JraAZw3Z3A0JSY7VXlZwo82Xn0G7wKbNNs2qoF4ZEEsQGTwDAroNdqKs1ofJxQ==} + prettier@3.8.4: resolution: {integrity: sha512-N2MylSdi48+5N/6S5j+maeHbUSIzzZ5uOcX5Hm4QpV8Dkb1HFjfAKTKX6yNPJQD9AhcT3ifHNB66tWTTJDi11Q==} engines: {node: '>=14'} @@ -2923,6 +2974,9 @@ packages: resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==} engines: {node: '>=6'} + query-selector-shadow-dom@1.0.1: + resolution: {integrity: sha512-lT5yCqEBgfoMYpf3F2xQRK7zEr1rhIIZuceDK6+xRkJQ4NMbHTwXqk4NkwDwQMNqXgG9r9fyHnzwNVs6zV5KRw==} + radix-ui@1.5.0: resolution: {integrity: sha512-Nzh2HNpClgB31FBHRqt2xG8XNUfVfQRpf34hACC5PNrXTd5JdXdqOXwLs3BL+D8CNYiNQiJiT8QGr5Q4vq+00w==} peerDependencies: @@ -3334,6 +3388,9 @@ packages: resolution: {integrity: sha512-o8qghlI8NZHU1lLPrpi2+Uq7abh4GGPpYANlalzWxyWteJOCsr/P+oPBA49TOLu5FTZO4d3F9MnWJfiMo4BkmA==} engines: {node: '>=18'} + web-vitals@5.3.0: + resolution: {integrity: sha512-q6LWsLatGYZp5VGBIOvbTj6JBV2nOmC8KvWztXBmwJcfFAzhwKwbOxhUH306XY3CcaZDUlSmSuNPBsCn0bFu+g==} + webidl-conversions@8.0.1: resolution: {integrity: sha512-BMhLD/Sw+GbJC21C/UgyaZX41nPt8bUTg+jWyDeg7e7YN4xOM05YPSIXceACnXVtqyEw/LMClUQMtMZ+PGGpqQ==} engines: {node: '>=20'} @@ -3922,6 +3979,19 @@ snapshots: '@oxc-project/types@0.133.0': {} + '@posthog/core@1.35.3': + dependencies: + '@posthog/types': 1.390.2 + + '@posthog/react@1.10.2(@types/react@19.2.17)(posthog-js@1.391.2)(react@19.2.7)': + dependencies: + posthog-js: 1.391.2 + react: 19.2.7 + optionalDependencies: + '@types/react': 19.2.17 + + '@posthog/types@1.390.2': {} + '@radix-ui/number@1.1.2': {} '@radix-ui/primitive@1.1.4': {} @@ -4827,11 +4897,11 @@ snapshots: tailwindcss: 4.3.0 vite: 8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0) - '@tanstack/devtools-client@0.0.6': + '@tanstack/devtools-client@0.0.7': dependencies: - '@tanstack/devtools-event-client': 0.4.3 + '@tanstack/devtools-event-client': 0.4.4 - '@tanstack/devtools-event-bus@0.4.1': + '@tanstack/devtools-event-bus@0.4.2': dependencies: ws: 8.21.0 transitivePeerDependencies: @@ -4840,7 +4910,9 @@ snapshots: '@tanstack/devtools-event-client@0.4.3': {} - '@tanstack/devtools-ui@0.5.2(csstype@3.2.3)(solid-js@1.9.13)': + '@tanstack/devtools-event-client@0.4.4': {} + + '@tanstack/devtools-ui@0.5.3(csstype@3.2.3)(solid-js@1.9.13)': dependencies: clsx: 2.1.1 dayjs: 1.11.21 @@ -4849,10 +4921,10 @@ snapshots: transitivePeerDependencies: - csstype - '@tanstack/devtools-vite@0.7.0(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)(vite@8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0))': + '@tanstack/devtools-vite@0.8.0(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)(vite@8.0.16(@types/node@22.19.21)(esbuild@0.27.0)(jiti@2.7.0))': dependencies: - '@tanstack/devtools-client': 0.0.6 - '@tanstack/devtools-event-bus': 0.4.1 + '@tanstack/devtools-client': 0.0.7 + '@tanstack/devtools-event-bus': 0.4.2 chalk: 5.6.2 launch-editor: 2.14.1 magic-string: 0.30.21 @@ -4865,14 +4937,14 @@ snapshots: - bufferutil - utf-8-validate - '@tanstack/devtools@0.12.2(csstype@3.2.3)(solid-js@1.9.13)': + '@tanstack/devtools@0.12.4(csstype@3.2.3)(solid-js@1.9.13)': dependencies: '@solid-primitives/event-listener': 2.4.5(solid-js@1.9.13) '@solid-primitives/keyboard': 1.3.5(solid-js@1.9.13) '@solid-primitives/resize-observer': 2.1.5(solid-js@1.9.13) - '@tanstack/devtools-client': 0.0.6 - '@tanstack/devtools-event-bus': 0.4.1 - '@tanstack/devtools-ui': 0.5.2(csstype@3.2.3)(solid-js@1.9.13) + '@tanstack/devtools-client': 0.0.7 + '@tanstack/devtools-event-bus': 0.4.2 + '@tanstack/devtools-ui': 0.5.3(csstype@3.2.3)(solid-js@1.9.13) clsx: 2.1.1 goober: 2.1.19(csstype@3.2.3) solid-js: 1.9.13 @@ -4895,9 +4967,9 @@ snapshots: '@tanstack/query-devtools@5.101.0': {} - '@tanstack/react-devtools@0.10.5(@types/react-dom@19.2.3(@types/react@19.2.17))(@types/react@19.2.17)(csstype@3.2.3)(react-dom@19.2.7(react@19.2.7))(react@19.2.7)(solid-js@1.9.13)': + '@tanstack/react-devtools@0.10.7(@types/react-dom@19.2.3(@types/react@19.2.17))(@types/react@19.2.17)(csstype@3.2.3)(react-dom@19.2.7(react@19.2.7))(react@19.2.7)(solid-js@1.9.13)': dependencies: - '@tanstack/devtools': 0.12.2(csstype@3.2.3)(solid-js@1.9.13) + '@tanstack/devtools': 0.12.4(csstype@3.2.3)(solid-js@1.9.13) '@types/react': 19.2.17 '@types/react-dom': 19.2.3(@types/react@19.2.17) react: 19.2.7 @@ -5471,6 +5543,8 @@ snapshots: cookie-es@3.1.1: {} + core-js@3.49.0: {} + css-tree@3.2.1: dependencies: mdn-data: 2.27.1 @@ -5528,6 +5602,10 @@ snapshots: optionalDependencies: '@types/trusted-types': 2.0.7 + dompurify@3.4.11: + optionalDependencies: + '@types/trusted-types': 2.0.7 + dunder-proto@1.0.1: dependencies: call-bind-apply-helpers: 1.0.2 @@ -5611,6 +5689,8 @@ snapshots: fetchdts@0.1.7: {} + fflate@0.4.8: {} + follow-redirects@1.16.0: {} form-data@4.0.5: @@ -6170,6 +6250,23 @@ snapshots: picocolors: 1.1.1 source-map-js: 1.2.1 + posthog-js@1.391.2: + dependencies: + '@posthog/core': 1.35.3 + '@posthog/types': 1.390.2 + core-js: 3.49.0 + dompurify: 3.4.11 + fflate: 0.4.8 + preact: 10.29.2 + query-selector-shadow-dom: 1.0.1 + web-vitals: 5.3.0 + + posthog-node@5.38.2: + dependencies: + '@posthog/core': 1.35.3 + + preact@10.29.2: {} + prettier@3.8.4: {} pretty-format@27.5.1: @@ -6184,6 +6281,8 @@ snapshots: punycode@2.3.1: {} + query-selector-shadow-dom@1.0.1: {} + radix-ui@1.5.0(@types/react-dom@19.2.3(@types/react@19.2.17))(@types/react@19.2.17)(react-dom@19.2.7(react@19.2.7))(react@19.2.7): dependencies: '@radix-ui/primitive': 1.1.4 @@ -6596,6 +6695,8 @@ snapshots: dependencies: xml-name-validator: 5.0.0 + web-vitals@5.3.0: {} + webidl-conversions@8.0.1: {} webpack-virtual-modules@0.6.2: {} diff --git a/web/pnpm-workspace.yaml b/web/pnpm-workspace.yaml index b830a26..b5060be 100644 --- a/web/pnpm-workspace.yaml +++ b/web/pnpm-workspace.yaml @@ -1,3 +1,4 @@ allowBuilds: + core-js: false esbuild: false minimumReleaseAge: 600 diff --git a/web/posthog-setup-report.md b/web/posthog-setup-report.md new file mode 100644 index 0000000..4ac728b --- /dev/null +++ b/web/posthog-setup-report.md @@ -0,0 +1,52 @@ + +# PostHog post-wizard report + +The wizard has completed a deep integration of PostHog into ExemplAI, a TanStack Start coding education platform. The integration includes: + +- **Client-side initialization** via `PostHogProvider` in `src/routes/__root.tsx`, wrapping the entire app with session replay, exception capture, and a reverse proxy config. +- **Reverse proxy** in `vite.config.ts` routing `/ingest/*` to the EU PostHog hosts, improving reliability and bypassing ad-blockers. +- **User identification** on email sign-in and sign-up, so returning sessions are correlated to named users. +- **Auth event tracking** across all three sign-in paths (email/password, sign-up with invite code, magic link). +- **Learning engagement tracking** in the course workspace — code runs, submissions, resets, and AI chat opens. +- **Error tracking** (`captureException`) on auth failures and code execution errors. +- **Server-side PostHog client** created as a singleton in `src/utils/posthog-server.ts` for future API-route tracking. + +## Events + +| Event | Description | File | +|---|---|---| +| `user_signed_in` | User successfully signs in with email and password. | `src/components/auth/forms/SignInForm.tsx` | +| `user_signed_up` | User creates a new account using the sign-up form with an invitation code. | `src/components/auth/forms/SignUpForm.tsx` | +| `magic_link_requested` | User requests a passwordless magic link to their email. | `src/components/auth/forms/MagicLinkForm.tsx` | +| `invitation_code_submitted` | User submits an invitation code during the magic link new-user flow. | `src/routes/auth.tsx` | +| `user_signed_out` | User signs out from an already-authenticated session. | `src/routes/auth.tsx` | +| `problem_started` | User clicks Start, Resume, or Review to open a coding problem. | `src/components/home/CourseList.tsx` | +| `code_run` | User runs their code in the workspace editor. | `src/routes/_authenticated.course.tsx` | +| `code_submitted` | User submits their solution for a coding problem. | `src/routes/_authenticated.course.tsx` | +| `code_reset` | User confirms resetting their code back to the default template. | `src/routes/_authenticated.course.tsx` | +| `ai_chat_opened` | User opens the AI assistant chat panel in the coding workspace. | `src/routes/_authenticated.course.tsx` | + +## 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: + +- [Analytics basics (wizard) Dashboard](https://eu.posthog.com/project/206544/dashboard/763020) +- [Auth Funnel: Sign-up to First Problem](https://eu.posthog.com/project/206544/insights/lvQBiemO) +- [New Users Over Time](https://eu.posthog.com/project/206544/insights/2F6Mf6zd) +- [Code Activity: Runs vs Submissions](https://eu.posthog.com/project/206544/insights/Rgyb1edM) +- [AI Chat Adoption](https://eu.posthog.com/project/206544/insights/UKgy4nwH) +- [Problem Engagement Funnel](https://eu.posthog.com/project/206544/insights/Jm78o2sE) + +## Verify before merging + +- [ ] 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. +- [ ] Run the test suite — call sites that were rewritten or instrumented may need updated mocks or fixtures. +- [ ] Add `VITE_PUBLIC_POSTHOG_PROJECT_TOKEN` and `VITE_PUBLIC_POSTHOG_HOST` to `.env.example` and any bootstrap scripts so collaborators know what to set. +- [ ] Wire source-map upload (`posthog-cli sourcemap` or your bundler's upload step) into CI so production stack traces de-minify. +- [ ] Confirm the returning-visitor path also calls `identify` — currently `identify` is called only on fresh sign-in and sign-up; users who return via a persisted session (Better Auth's `useSession()`) are not re-identified on load. Consider calling `posthog.identify(session.user.email, ...)` inside `_authenticated.tsx` when a session is present. + +### 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. + + diff --git a/web/src/components/auth/forms/MagicLinkForm.tsx b/web/src/components/auth/forms/MagicLinkForm.tsx index ce3738f..0891d5b 100644 --- a/web/src/components/auth/forms/MagicLinkForm.tsx +++ b/web/src/components/auth/forms/MagicLinkForm.tsx @@ -1,9 +1,10 @@ -import { Mail, ArrowRight } from "lucide-react"; +import { usePostHog } from "@posthog/react"; import { useForm } from "@tanstack/react-form"; -import { authClient } from "#/lib/auth-client"; +import { ArrowRight, Mail } from "lucide-react"; import { checkUserExists } from "#/lib/auth.functions"; -import { AuthTextField } from "../AuthTextField"; +import { authClient } from "#/lib/auth-client"; import { AuthButton } from "../AuthButton"; +import { AuthTextField } from "../AuthTextField"; interface MagicLinkFormProps { onSuccess: () => void; @@ -18,6 +19,7 @@ export function MagicLinkForm({ onError, redirectUrl, }: MagicLinkFormProps) { + const posthog = usePostHog(); const form = useForm({ defaultValues: { email: "", @@ -31,7 +33,9 @@ export function MagicLinkForm({ return; } - console.log(`[Auth] Magic Link Sign In initiated for email: "${value.email}"`); + console.log( + `[Auth] Magic Link Sign In initiated for email: "${value.email}"`, + ); const { error } = await authClient.signIn.magicLink({ email: value.email, @@ -41,9 +45,11 @@ export function MagicLinkForm({ if (error) { onError(error.message || "Failed to send magic link"); } else { + posthog.capture("magic_link_requested", { email: value.email }); onSuccess(); } } catch (err: any) { + posthog.captureException(err); onError(err.message || "An unexpected error occurred."); } }, @@ -63,7 +69,8 @@ export function MagicLinkForm({ validators={{ onChange: ({ value }) => { if (!value) return "Email is required"; - if (!/\S+@\S+\.\S+/.test(value)) return "Please enter a valid email address"; + if (!/\S+@\S+\.\S+/.test(value)) + return "Please enter a valid email address"; return undefined; }, }} @@ -76,7 +83,11 @@ export function MagicLinkForm({ value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> diff --git a/web/src/components/auth/forms/SignInForm.tsx b/web/src/components/auth/forms/SignInForm.tsx index 9c26627..2e49d5f 100644 --- a/web/src/components/auth/forms/SignInForm.tsx +++ b/web/src/components/auth/forms/SignInForm.tsx @@ -1,8 +1,9 @@ -import { Mail, Lock, ArrowRight } from "lucide-react"; +import { usePostHog } from "@posthog/react"; import { useForm } from "@tanstack/react-form"; +import { ArrowRight, Lock, Mail } from "lucide-react"; import { authClient } from "#/lib/auth-client"; -import { AuthTextField } from "../AuthTextField"; import { AuthButton } from "../AuthButton"; +import { AuthTextField } from "../AuthTextField"; interface SignInFormProps { onSuccess: () => void; @@ -10,6 +11,7 @@ interface SignInFormProps { } export function SignInForm({ onSuccess, onError }: SignInFormProps) { + const posthog = usePostHog(); const form = useForm({ defaultValues: { email: "", @@ -24,9 +26,12 @@ export function SignInForm({ onSuccess, onError }: SignInFormProps) { if (error) { onError(error.message || "Invalid email or password"); } else { + posthog.identify(value.email, { email: value.email }); + posthog.capture("user_signed_in", { method: "email" }); onSuccess(); } } catch (err: any) { + posthog.captureException(err); onError(err.message || "An unexpected error occurred."); } }, @@ -46,7 +51,8 @@ export function SignInForm({ onSuccess, onError }: SignInFormProps) { validators={{ onChange: ({ value }) => { if (!value) return "Email is required"; - if (!/\S+@\S+\.\S+/.test(value)) return "Please enter a valid email address"; + if (!/\S+@\S+\.\S+/.test(value)) + return "Please enter a valid email address"; return undefined; }, }} @@ -59,7 +65,11 @@ export function SignInForm({ onSuccess, onError }: SignInFormProps) { value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> @@ -83,7 +93,11 @@ export function SignInForm({ onSuccess, onError }: SignInFormProps) { value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> @@ -95,7 +109,9 @@ export function SignInForm({ onSuccess, onError }: SignInFormProps) { type="button" className="text-xs font-semibold text-zinc-500 hover:underline outline-none cursor-pointer" onClick={() => { - onError("Password reset is not configured yet. Please contact your administrator."); + onError( + "Password reset is not configured yet. Please contact your administrator.", + ); }} > Forgot Password? diff --git a/web/src/components/auth/forms/SignUpForm.tsx b/web/src/components/auth/forms/SignUpForm.tsx index 2cd8414..5e2d164 100644 --- a/web/src/components/auth/forms/SignUpForm.tsx +++ b/web/src/components/auth/forms/SignUpForm.tsx @@ -1,10 +1,11 @@ -import { Mail, Lock, User, ShieldCheck, ArrowRight } from "lucide-react"; +import { usePostHog } from "@posthog/react"; import { useForm } from "@tanstack/react-form"; import { useConvex, useMutation } from "convex/react"; -import { api } from "../../../../convex/_generated/api"; +import { ArrowRight, Lock, Mail, ShieldCheck, User } from "lucide-react"; import { authClient } from "#/lib/auth-client"; -import { AuthTextField } from "../AuthTextField"; +import { api } from "../../../../convex/_generated/api"; import { AuthButton } from "../AuthButton"; +import { AuthTextField } from "../AuthTextField"; interface SignUpFormProps { onSuccess: () => void; @@ -12,8 +13,11 @@ interface SignUpFormProps { } export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { + const posthog = usePostHog(); const convex = useConvex(); - const createUserAndUseCode = useMutation(api.invitationCodes.createUserAndUseCode); + const createUserAndUseCode = useMutation( + api.invitationCodes.createUserAndUseCode, + ); const form = useForm({ defaultValues: { @@ -25,7 +29,10 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { onSubmit: async ({ value }) => { try { // 1. Validate invitation code in Convex - const validation = await convex.query(api.invitationCodes.validateCode, { code: value.code }); + const validation = await convex.query( + api.invitationCodes.validateCode, + { code: value.code }, + ); if (!validation.isValid) { onError(validation.reason || "Invalid invitation code."); return; @@ -51,10 +58,16 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { code: value.code, tokenIdentifier: user.id, }); + posthog.identify(user.email, { + email: user.email, + name: user.name || undefined, + }); + posthog.capture("user_signed_up", { method: "email" }); } onSuccess(); } } catch (err: any) { + posthog.captureException(err); onError(err.message || "An unexpected error occurred."); } }, @@ -86,7 +99,11 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> @@ -98,7 +115,8 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { validators={{ onChange: ({ value }) => { if (!value) return "Email is required"; - if (!/\S+@\S+\.\S+/.test(value)) return "Please enter a valid email address"; + if (!/\S+@\S+\.\S+/.test(value)) + return "Please enter a valid email address"; return undefined; }, }} @@ -111,7 +129,11 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> @@ -123,7 +145,8 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { validators={{ onChange: ({ value }) => { if (!value) return "Password is required"; - if (value.length < 8) return "Password must be at least 8 characters"; + if (value.length < 8) + return "Password must be at least 8 characters"; return undefined; }, }} @@ -136,7 +159,11 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> @@ -160,7 +187,11 @@ export function SignUpForm({ onSuccess, onError }: SignUpFormProps) { value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} - error={field.state.meta.errors ? field.state.meta.errors.join(", ") : undefined} + error={ + field.state.meta.errors + ? field.state.meta.errors.join(", ") + : undefined + } leadingIcon={} required /> diff --git a/web/src/components/home/CourseList.tsx b/web/src/components/home/CourseList.tsx index 2e93fe5..1d9776a 100644 --- a/web/src/components/home/CourseList.tsx +++ b/web/src/components/home/CourseList.tsx @@ -1,17 +1,18 @@ -import { useState } from "react"; +import { usePostHog } from "@posthog/react"; import { Link } from "@tanstack/react-router"; +import { useQuery } from "convex/react"; import { CheckCircle2, + ChevronDown, + ChevronRight, + Compass, + HelpCircle, Lock, PlayCircle, Search, - HelpCircle, - Compass, - ChevronDown, - ChevronRight, } from "lucide-react"; +import { useState } from "react"; import { cn } from "#/lib/utils.ts"; -import { useQuery } from "convex/react"; import { api } from "../../../convex/_generated/api"; export type ShortProblem = { @@ -84,8 +85,12 @@ export default function CourseList() { const [searchTerm, setSearchTerm] = useState(""); const [selectedWeek, setSelectedWeek] = useState("all"); - const [selectedStatus, setSelectedStatus] = useState("all"); - const [collapsedWeeks, setCollapsedWeeks] = useState>({}); + const [selectedStatus, setSelectedStatus] = useState< + ShortProblem["status"] | "all" + >("all"); + const [collapsedWeeks, setCollapsedWeeks] = useState>( + {}, + ); if (questions === undefined) { return ( @@ -97,7 +102,10 @@ export default function CourseList() {
{[1, 2, 3].map((i) => ( -
+
))}
@@ -123,25 +131,27 @@ export default function CourseList() { const matchesSearch = problem.name.toLowerCase().includes(searchTerm.toLowerCase()) || problem.description.toLowerCase().includes(searchTerm.toLowerCase()) || - problem.tags.some((tag) => tag.toLowerCase().includes(searchTerm.toLowerCase())); + problem.tags.some((tag) => + tag.toLowerCase().includes(searchTerm.toLowerCase()), + ); const matchesWeek = selectedWeek === "all" || problem.week === selectedWeek; - const matchesStatus = selectedStatus === "all" || problem.status === selectedStatus; + const matchesStatus = + selectedStatus === "all" || problem.status === selectedStatus; return matchesSearch && matchesWeek && matchesStatus; }); // Group filtered problems by week - const problemsByWeek = filteredProblems.reduce>( - (groups, problem) => { - if (!groups[problem.week]) { - groups[problem.week] = []; - } - groups[problem.week].push(problem); - return groups; - }, - {} - ); + const problemsByWeek = filteredProblems.reduce< + Record + >((groups, problem) => { + if (!groups[problem.week]) { + groups[problem.week] = []; + } + groups[problem.week].push(problem); + return groups; + }, {}); // Get sorted list of active weeks const sortedWeeks = Object.keys(problemsByWeek) @@ -164,8 +174,8 @@ export default function CourseList() { Python Programming Syllabus

- Complete the challenges below sequentially. Use the workspace editor to run and submit - your solutions. + Complete the challenges below sequentially. Use the workspace editor + to run and submit your solutions.

@@ -186,20 +196,22 @@ export default function CourseList() { {/* Status filter tabs */}
- {(["all", "completed", "in-progress", "pending"] as const).map((status) => ( - - ))} + {(["all", "completed", "in-progress", "pending"] as const).map( + (status) => ( + + ), + )}
@@ -211,7 +223,7 @@ export default function CourseList() { "px-3 py-1.5 text-xs font-bold rounded-lg border cursor-pointer transition-colors", selectedWeek === "all" ? "bg-sea-ink text-white border-sea-ink" - : "bg-sand/30 border-line text-sea-ink hover:bg-sand/65" + : "bg-sand/30 border-line text-sea-ink hover:bg-sand/65", )} > All Weeks @@ -224,7 +236,7 @@ export default function CourseList() { "px-3 py-1.5 text-xs font-bold rounded-lg border cursor-pointer transition-colors", selectedWeek === wk ? "bg-sea-ink text-white border-sea-ink" - : "bg-sand/30 border-line text-sea-ink hover:bg-sand/65" + : "bg-sand/30 border-line text-sea-ink hover:bg-sand/65", )} > Week {wk} @@ -271,7 +283,9 @@ export default function CourseList() { ) : (
-

No exercises found

+

+ No exercises found +

Try adjusting your search criteria or selected filters.

@@ -291,6 +305,7 @@ function ShortProblemCard({ difficulty, tags, }: ShortProblem) { + const posthog = usePostHog(); const difficultyStyles = { Easy: "text-emerald-600 bg-emerald-50 border-emerald-200/50", Medium: "text-amber-600 bg-amber-50 border-amber-200/50", @@ -328,7 +343,7 @@ function ShortProblemCard({ {difficulty} @@ -360,16 +375,34 @@ function ShortProblemCard({ + posthog.capture("problem_started", { + problem_id: id, + problem_name: name, + week, + difficulty, + action: + status === "completed" + ? "review" + : status === "in-progress" + ? "resume" + : "start", + }) + } className={cn( "text-xs font-bold px-3 py-1.5 rounded-lg border text-center transition-colors flex items-center gap-1 cursor-pointer", status === "completed" ? "bg-sand/30 border-line text-sea-ink hover:bg-sand/65" : status === "in-progress" ? "bg-palm text-white border-palm hover:bg-palm/90" - : "bg-white border-line text-sea-ink hover:bg-sand/35" + : "bg-white border-line text-sea-ink hover:bg-sand/35", )} > - {status === "completed" ? "Review" : status === "in-progress" ? "Resume" : "Start"} + {status === "completed" + ? "Review" + : status === "in-progress" + ? "Resume" + : "Start"}
diff --git a/web/src/routes/__root.tsx b/web/src/routes/__root.tsx index 99f72b2..08e390a 100644 --- a/web/src/routes/__root.tsx +++ b/web/src/routes/__root.tsx @@ -1,14 +1,16 @@ -import { HeadContent, Scripts, createRootRouteWithContext } from "@tanstack/react-router"; -import { TanStackRouterDevtoolsPanel } from "@tanstack/react-router-devtools"; +import { PostHogProvider } from "@posthog/react"; import { TanStackDevtools } from "@tanstack/react-devtools"; +import type { QueryClient } from "@tanstack/react-query"; +import { + createRootRouteWithContext, + HeadContent, + Scripts, +} from "@tanstack/react-router"; +import { TanStackRouterDevtoolsPanel } from "@tanstack/react-router-devtools"; import { ConvexProvider, ConvexReactClient } from "convex/react"; - import TanStackQueryDevtools from "../integrations/tanstack-query/devtools"; - import appCss from "../styles.css?url"; -import type { QueryClient } from "@tanstack/react-query"; - const convex = new ConvexReactClient(import.meta.env.VITE_CONVEX_URL); interface MyRouterContext { @@ -46,7 +48,21 @@ function RootDocument({ children }: { children: React.ReactNode }) { - {children} + + {children} + {import.meta.env.DEV && ( ) => { - return { - problemId: (search.problemId as string) || undefined, - } - }, -}) +import { usePostHog } from "@posthog/react"; +import { ClientOnly, createFileRoute } from "@tanstack/react-router"; +import axios from "axios"; +import { useQuery } from "convex/react"; +import { BookOpen, ChevronLeft } from "lucide-react"; +import { useRef, useState } from "react"; +import CodeEditor from "#/components/student/CodeEditor"; +import CodingBar from "#/components/student/InteractionBar"; +import Problem from "#/components/student/problem/Problem"; +import ResetCodeForm from "#/components/student/ResetCodeForm"; +import SidePanel from "#/components/student/SidePane"; +import { api } from "../../convex/_generated/api"; + +export const Route = createFileRoute("/_authenticated/course")({ + component: Course, + validateSearch: (search: Record) => { + return { + problemId: (search.problemId as string) || undefined, + }; + }, +}); const CODE_TEMPLATES = { - python: `def main():\n # Write your Python code here\n print("Hello, World!")\n\nif __name__ == "__main__":\n main()`, -} + python: `def main():\n # Write your Python code here\n print("Hello, World!")\n\nif __name__ == "__main__":\n main()`, +}; function Course() { - const editorRef = useRef(null) - const url = import.meta.env.VITE_BACKEND_URL || 'http://localhost:8000' - - const { problemId } = Route.useSearch() - - const questions = useQuery(api.courses.getAllCourses) - const fetchedQuestion = useQuery( - api.courses.getQuestionById, - problemId ? { id: problemId } : "skip" - ) - - const [language, setLanguage] = useState('python') - const [fontSize, setFontSize] = useState(14) - const [codeTemplates, setCodeTemplates] = useState(CODE_TEMPLATES) - const [isRunning, setIsRunning] = useState(false) - const [isSubmitting, setIsSubmitting] = useState(false) - const [executionResult, setExecutionResult] = useState(null) - const [isConsoleOpen, setIsConsoleOpen] = useState(false) - const [showResetModal, setShowResetModal] = useState(false) - const [isProblemCollapsed, setIsProblemCollapsed] = useState(false) - const [isChatCollapsed, setIsChatCollapsed] = useState(true) - - const activeQuestion = problemId - ? fetchedQuestion - : (questions && questions.length > 0 ? questions[0] : null) - - if (activeQuestion === undefined) { - return ( -
-
-

Loading workspace...

-
-
- ) - } - - if (activeQuestion === null) { - return ( -
-

No problems available in the database.

-
- ) - } - - const mappedProblem = { - id: activeQuestion._id, - title: activeQuestion.problem_name, - description: activeQuestion.problem_description, - tags: ["Python"], + const editorRef = useRef(null); + const posthog = usePostHog(); + const url = import.meta.env.VITE_BACKEND_URL || "http://localhost:8000"; + + const { problemId } = Route.useSearch(); + + const questions = useQuery(api.courses.getAllCourses); + const fetchedQuestion = useQuery( + api.courses.getQuestionById, + problemId ? { id: problemId } : "skip", + ); + + const [language, setLanguage] = useState("python"); + const [fontSize, setFontSize] = useState(14); + const [codeTemplates, setCodeTemplates] = useState(CODE_TEMPLATES); + const [isRunning, setIsRunning] = useState(false); + const [isSubmitting, setIsSubmitting] = useState(false); + const [executionResult, setExecutionResult] = useState(null); + const [isConsoleOpen, setIsConsoleOpen] = useState(false); + const [showResetModal, setShowResetModal] = useState(false); + const [isProblemCollapsed, setIsProblemCollapsed] = useState(false); + const [isChatCollapsed, setIsChatCollapsed] = useState(true); + + const activeQuestion = problemId + ? fetchedQuestion + : questions && questions.length > 0 + ? questions[0] + : null; + + if (activeQuestion === undefined) { + return ( +
+
+

+ Loading workspace... +

+
+
+ ); + } + + if (activeQuestion === null) { + return ( +
+

No problems available in the database.

+
+ ); + } + + const mappedProblem = { + id: activeQuestion._id, + title: activeQuestion.problem_name, + description: activeQuestion.problem_description, + tags: ["Python"], + }; + + function handleEditorMount(editor: any) { + editorRef.current = editor; + } + + function handleCodeChange(value: string | undefined) { + if (value !== undefined) { + setCodeTemplates((prev) => ({ + ...prev, + [language]: value, + })); } - - function handleEditorMount(editor: any) { - editorRef.current = editor + } + + function handleReset() { + posthog.capture("code_reset", { problem_id: problemId, language }); + setCodeTemplates((prev) => ({ + ...prev, + [language]: CODE_TEMPLATES[language as keyof typeof CODE_TEMPLATES], + })); + if (editorRef.current) { + editorRef.current.setValue( + CODE_TEMPLATES[language as keyof typeof CODE_TEMPLATES], + ); } + } - function handleCodeChange(value: string | undefined) { - if (value !== undefined) { - setCodeTemplates((prev) => ({ - ...prev, - [language]: value, - })) - } - } + async function handleExecute(actionType: "run" | "submit") { + if (!editorRef.current) return; - function handleReset() { - setCodeTemplates((prev) => ({ - ...prev, - [language]: CODE_TEMPLATES[language as keyof typeof CODE_TEMPLATES], - })) - if (editorRef.current) { - editorRef.current.setValue(CODE_TEMPLATES[language as keyof typeof CODE_TEMPLATES]) - } + if (actionType === "run") { + setIsRunning(true); + } else { + setIsSubmitting(true); } - async function handleExecute(actionType: 'run' | 'submit') { - if (!editorRef.current) return - - if (actionType === 'run') { - setIsRunning(true) - } else { - setIsSubmitting(true) - } - - setExecutionResult(null) - setIsConsoleOpen(true) // Auto-open console drawer when running - - const submissionCode = editorRef.current.getValue() - - try { - const response = await axios.post(`${url}/execute`, { - code: submissionCode, - }) - setExecutionResult(response.data) - } catch (error: any) { - setExecutionResult({ - error: true, - message: error.message || 'Execution failed', - stderr: error.response?.data?.detail || error.response?.data?.message || error.message, - }) - } finally { - setIsRunning(false) - setIsSubmitting(false) - } + setExecutionResult(null); + setIsConsoleOpen(true); // Auto-open console drawer when running + + const submissionCode = editorRef.current.getValue(); + + try { + const response = await axios.post(`${url}/execute`, { + code: submissionCode, + }); + setExecutionResult(response.data); + posthog.capture(actionType === "run" ? "code_run" : "code_submitted", { + problem_id: problemId, + language, + success: !response.data?.error, + }); + } catch (error: any) { + posthog.captureException(error); + setExecutionResult({ + error: true, + message: error.message || "Execution failed", + stderr: + error.response?.data?.detail || + error.response?.data?.message || + error.message, + }); + } finally { + setIsRunning(false); + setIsSubmitting(false); } - - const currentCode = codeTemplates[language as keyof typeof codeTemplates] || '' - - return ( - -
- {/* Workspace Container */} -
- {/* Problem Description Container */} - {!isProblemCollapsed && ( -
- {/* Header */} -
-
- - Problem Description -
- -
- {/* Problem Content */} -
- -
-
- )} - - {/* Code Editor Container */} -
- {/* Top Toolbar */} - setShowResetModal(true)} - isProblemCollapsed={isProblemCollapsed} - setIsProblemCollapsed={setIsProblemCollapsed} - isChatCollapsed={isChatCollapsed} - setIsChatCollapsed={setIsChatCollapsed} - /> - - {/* Editor Container */} -
- handleExecute('run')} - onSubmit={() => handleExecute('submit')} - /> -
-
- - {/* Chat Panel */} - {!isChatCollapsed && ( -
- setIsChatCollapsed(true)} /> -
- )} - + } + + const currentCode = + codeTemplates[language as keyof typeof codeTemplates] || ""; + + return ( + +
+ {/* Workspace Container */} +
+ {/* Problem Description Container */} + {!isProblemCollapsed && ( +
+ {/* Header */} +
+
+ + Problem Description
+ +
+ {/* Problem Content */} +
+ +
+
+ )} + + {/* Code Editor Container */} +
+ {/* Top Toolbar */} + setShowResetModal(true)} + isProblemCollapsed={isProblemCollapsed} + setIsProblemCollapsed={setIsProblemCollapsed} + isChatCollapsed={isChatCollapsed} + setIsChatCollapsed={(collapsed) => { + if (!collapsed) + posthog.capture("ai_chat_opened", { problem_id: problemId }); + setIsChatCollapsed(collapsed); + }} + /> + + {/* Editor Container */} +
+ handleExecute("run")} + onSubmit={() => handleExecute("submit")} + /> +
+
- {/* Solid Reset Confirmation Modal */} - {showResetModal && ( - - )} + {/* Chat Panel */} + {!isChatCollapsed && ( +
+ setIsChatCollapsed(true)} />
- - ) + )} +
+ + {/* Solid Reset Confirmation Modal */} + {showResetModal && ( + + )} +
+
+ ); } diff --git a/web/src/routes/auth.tsx b/web/src/routes/auth.tsx index 2f6ca9e..56fb484 100644 --- a/web/src/routes/auth.tsx +++ b/web/src/routes/auth.tsx @@ -1,16 +1,17 @@ -import * as React from "react"; +import { usePostHog } from "@posthog/react"; import { createFileRoute, useNavigate } from "@tanstack/react-router"; -import { AlertCircle, CheckCircle2 } from "lucide-react"; import { useConvex } from "convex/react"; -import { api } from "../../convex/_generated/api"; -import { authClient } from "#/lib/auth-client"; +import { AlertCircle, CheckCircle2 } from "lucide-react"; +import * as React from "react"; +import { AlreadySignedIn } from "#/components/auth/AlreadySignedIn"; import { AuthCard } from "#/components/auth/AuthCard"; import { AuthTabs } from "#/components/auth/AuthTabs"; -import { AlreadySignedIn } from "#/components/auth/AlreadySignedIn"; -import { InvitationCodeModal } from "#/components/auth/InvitationCodeModal"; +import { MagicLinkForm } from "#/components/auth/forms/MagicLinkForm"; import { SignInForm } from "#/components/auth/forms/SignInForm"; import { SignUpForm } from "#/components/auth/forms/SignUpForm"; -import { MagicLinkForm } from "#/components/auth/forms/MagicLinkForm"; +import { InvitationCodeModal } from "#/components/auth/InvitationCodeModal"; +import { authClient } from "#/lib/auth-client"; +import { api } from "../../convex/_generated/api"; interface AuthSearch { redirect?: string; @@ -48,7 +49,8 @@ export const Route = createFileRoute("/auth")({ component: AuthPage, validateSearch: (search: Record): AuthSearch => { return { - redirect: typeof search.redirect === "string" ? search.redirect : undefined, + redirect: + typeof search.redirect === "string" ? search.redirect : undefined, }; }, }); @@ -56,10 +58,14 @@ export const Route = createFileRoute("/auth")({ function AuthPage() { const navigate = useNavigate(); const { redirect } = Route.useSearch(); - const { data: session, isPending: isSessionLoading } = authClient.useSession(); + const { data: session, isPending: isSessionLoading } = + authClient.useSession(); const convex = useConvex(); + const posthog = usePostHog(); - const [activeTab, setActiveTab] = React.useState<"signin" | "signup" | "magiclink">("signin"); + const [activeTab, setActiveTab] = React.useState< + "signin" | "signup" | "magiclink" + >("signin"); const [isSigningOut, setIsSigningOut] = React.useState(false); const [successMessage, setSuccessMessage] = React.useState(""); const [globalError, setGlobalError] = React.useState(""); @@ -98,9 +104,10 @@ function AuthPage() { if (error) { setModalError(error.message || "Failed to send magic link."); } else { + posthog.capture("invitation_code_submitted", { email: magicEmail }); setShowMagicCodeModal(false); setSuccessMessage( - "Magic link sent! Once you click it in your email, your account will be created." + "Magic link sent! Once you click it in your email, your account will be created.", ); } } catch (err: any) { @@ -119,6 +126,8 @@ function AuthPage() { const handleSignOut = async () => { setIsSigningOut(true); try { + posthog.capture("user_signed_out"); + posthog.reset(); await authClient.signOut(); navigate({ to: "/auth" }); } catch (err: any) { @@ -147,7 +156,9 @@ function AuthPage() { return ( navigate({ to: sanitizeRedirect(redirect || "/") })} + onGoToDashboard={() => + navigate({ to: sanitizeRedirect(redirect || "/") }) + } onSignOut={handleSignOut} isSigningOut={isSigningOut} /> @@ -195,21 +206,29 @@ function AuthPage() { {/* Render appropriate form component */} {activeTab === "signin" && ( navigate({ to: sanitizeRedirect(redirect || "/") })} + onSuccess={() => + navigate({ to: sanitizeRedirect(redirect || "/") }) + } onError={setGlobalError} /> )} {activeTab === "signup" && ( navigate({ to: sanitizeRedirect(redirect || "/") })} + onSuccess={() => + navigate({ to: sanitizeRedirect(redirect || "/") }) + } onError={setGlobalError} /> )} {activeTab === "magiclink" && ( setSuccessMessage("Magic link sent! Please check your email inbox.")} + onSuccess={() => + setSuccessMessage( + "Magic link sent! Please check your email inbox.", + ) + } onUserNotFound={(email) => { setMagicEmail(email); setModalError(""); diff --git a/web/src/utils/posthog-server.ts b/web/src/utils/posthog-server.ts new file mode 100644 index 0000000..32b0420 --- /dev/null +++ b/web/src/utils/posthog-server.ts @@ -0,0 +1,20 @@ +import { PostHog } from "posthog-node"; + +let posthogClient: PostHog | null = null; + +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog( + process.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN || + import.meta.env.VITE_PUBLIC_POSTHOG_PROJECT_TOKEN!, + { + host: + process.env.VITE_PUBLIC_POSTHOG_HOST || + import.meta.env.VITE_PUBLIC_POSTHOG_HOST, + flushAt: 1, + flushInterval: 0, + }, + ); + } + return posthogClient; +} diff --git a/web/vite.config.ts b/web/vite.config.ts index b0f53d3..66fb422 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -1,14 +1,45 @@ -import { defineConfig } from 'vite' -import { devtools } from '@tanstack/devtools-vite' +import tailwindcss from "@tailwindcss/vite"; +import { devtools } from "@tanstack/devtools-vite"; -import { tanstackStart } from '@tanstack/react-start/plugin/vite' +import { tanstackStart } from "@tanstack/react-start/plugin/vite"; -import viteReact from '@vitejs/plugin-react' -import tailwindcss from '@tailwindcss/vite' +import viteReact from "@vitejs/plugin-react"; +import { defineConfig } from "vite"; const config = defineConfig({ resolve: { tsconfigPaths: true }, - plugins: [devtools(), tailwindcss(), tanstackStart(), viteReact()], -}) + plugins: [ + devtools({ + consolePiping: { + enabled: false, + }, + }), + tailwindcss(), + tanstackStart(), + viteReact(), + ], + server: { + proxy: { + "^/api/v1/static": { + target: "https://eu-assets.i.posthog.com", + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api\/v1/, ""), + secure: false, + }, + "^/api/v1/array": { + target: "https://eu-assets.i.posthog.com", + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api\/v1/, ""), + secure: false, + }, + "^/api/v1": { + target: "https://eu.i.posthog.com", + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api\/v1/, ""), + secure: false, + }, + }, + }, +}); -export default config +export default config;