From 18658b9093355c7c80b02d40053dc05196b4edf7 Mon Sep 17 00:00:00 2001 From: "wizard-ci-bot[bot]" <254716194+wizard-ci-bot[bot]@users.noreply.github.com> Date: Thu, 9 Jul 2026 22:48:48 +0000 Subject: [PATCH] wizard-ci: sveltekit/CMSaasStarter --- .../integration-sveltekit/.posthog-wizard | 0 .../skills/integration-sveltekit/SKILL.md | 70 ++ .../references/1-begin.md | 56 ++ .../references/2-edit.md | 36 + .../references/3-revise.md | 22 + .../references/4-conclude.md | 107 +++ .../references/EXAMPLE.md | 851 ++++++++++++++++++ .../references/identify-users.md | 272 ++++++ .../references/svelte.md | 234 +++++ .../sveltekit/CMSaasStarter/package-lock.json | 129 ++- .../sveltekit/CMSaasStarter/package.json | 2 + .../CMSaasStarter/posthog-setup-report.md | 44 + .../CMSaasStarter/src/hooks.server.ts | 58 +- .../CMSaasStarter/src/lib/posthog.ts | 25 + .../CMSaasStarter/src/lib/server/posthog.ts | 23 + .../(admin)/account/(menu)/+layout.svelte | 15 +- .../(menu)/billing/manage/+page.server.ts | 11 + .../src/routes/(admin)/account/+layout.svelte | 24 +- .../src/routes/(admin)/account/+layout.ts | 2 + .../(admin)/account/api/+page.server.ts | 23 + .../account/subscribe/[slug]/+page.server.ts | 12 + .../(marketing)/auth/callback/+server.js | 18 + .../(marketing)/contact_us/+page.server.ts | 13 + .../src/routes/(marketing)/login/+layout.ts | 7 +- .../(marketing)/login/sign_in/+page.svelte | 17 +- .../(marketing)/login/sign_up/+page.svelte | 8 + .../routes/(marketing)/search/+page.svelte | 18 + .../CMSaasStarter/src/routes/+layout.svelte | 6 + .../sveltekit/CMSaasStarter/svelte.config.js | 3 + 29 files changed, 2082 insertions(+), 24 deletions(-) create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/.posthog-wizard create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/SKILL.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/1-begin.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/2-edit.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/3-revise.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/4-conclude.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/EXAMPLE.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/identify-users.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/svelte.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/posthog-setup-report.md create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/src/lib/posthog.ts create mode 100644 apps/basic-integration/sveltekit/CMSaasStarter/src/lib/server/posthog.ts diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/.posthog-wizard b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/.posthog-wizard new file mode 100644 index 000000000..e69de29bb diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/SKILL.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/SKILL.md new file mode 100644 index 000000000..95218b62b --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/SKILL.md @@ -0,0 +1,70 @@ +--- +name: integration-sveltekit +description: PostHog integration for SvelteKit applications +metadata: + author: PostHog + version: dev +--- + +# PostHog integration for SvelteKit + +This skill helps you add PostHog analytics to SvelteKit 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` - SvelteKit 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/svelte.md` - Svelte - 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 + +- Set paths.relative to false in svelte.config.js — this is required for PostHog session replay to work correctly with SSR and is easy to miss +- Use the Svelte MCP server tools to check Svelte documentation (list-sections, get-documentation) and validate components (svelte-autofixer) — always run svelte-autofixer on new or modified .svelte files before finishing +- Remember that source code is available in the node_modules directory +- Check package.json for type checking or build scripts to validate changes +- When a reverse proxy is configured, both /static/* AND /array/* must route to the assets origin (us-assets.i.posthog.com or eu-assets.i.posthog.com). +- posthog-js is the JavaScript SDK package name +- posthog.init() MUST be called before any other PostHog methods (capture, identify, etc.) +- posthog-js is browser-only — do NOT import it in Node.js or server-side contexts (use posthog-node instead) +- Autocapture is ON by default with posthog-js (tracks clicks, form submissions, pageviews). Do NOT disable autocapture unless the user explicitly requests it. +- NEVER send PII in posthog.capture() event properties — no emails, full names, phone numbers, physical addresses, IP addresses, or user-generated content +- PII belongs in posthog.identify() person properties (email, name, role), NOT in capture() event properties +- Call posthog.identify(userId, { email, name, role }) on login AND on page refresh if the user is already logged in +- Call posthog.reset() on logout to unlink future events from the current user +- For SPAs without a framework router, capture pageviews with posthog.capture($pageview) or use the capture_pageview history_change option in init for History API routing +- posthog-node is the Node.js server-side SDK package name – do NOT use posthog-js on the server +- Include enableExceptionAutocapture: true in the PostHog constructor options +- Add posthog.capture() calls in route handlers for meaningful user actions – every route that creates, updates, or deletes data should track an event with contextual properties +- Add posthog.captureException(err, distinctId) in the application's error handler (e.g., Express error middleware, Fastify setErrorHandler, Koa app.on('error')) +- In long-running servers, the SDK batches events automatically – do NOT set flushAt or flushInterval unless you have a specific reason to +- For short-lived processes (scripts, CLIs, serverless), set flushAt to 1 and flushInterval to 0 to send events immediately +- Reverse proxy is NOT needed for server-side Node.js – only client-side JavaScript needs a proxy to avoid ad blockers + +## Identifying users + +Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation. + +## Error tracking + +Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries. diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/1-begin.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/1-begin.md new file mode 100644 index 000000000..55f0a8326 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/1-begin.md @@ -0,0 +1,56 @@ +--- +title: PostHog Setup - Begin +description: Start the event tracking setup process by analyzing the project and creating an event tracking plan +--- + +We're making an event tracking plan for this project. + +This is the first of several phases — plan the events, implement them, revise and validate changes, then conclude by creating a dashboard and writing a setup report. + +## Task list + +As soon as you've read this description and have a rough sense of the work, make a single **call `TaskCreate` immediately** before reading any reference file or beginning analysis. The user is watching the task pane and shouldn't see it sit empty. + +It's fine if your first list is incomplete or imprecise. Seed it with whatever high-level items you can infer from the overview above, then call `TaskCreate` again (or `TaskUpdate` to refine existing items) every time your understanding sharpens: after a phase reveals work you didn't anticipate, after planning surfaces concrete sub-items, after you hit something new. Use `TaskUpdate` to mark items `in_progress` when you start them and `completed` when you finish. Keeping the list current matters more than getting it right on the first call. + +Keep task titles broad and job-oriented. Describe the purpose or area of work with wording like "Planning event tracking", "Identifying users", "Installing PostHog", "Capturing events", or "Creating dashboards", not the specific files, paths, or symbols involved. Adjust the task names according to the user's project and context. + +Before proceeding, find any existing `posthog.capture()` code. Make note of event name formatting. + +From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events. Also look for additional files related to login that could be used for identifying users, along with error handling. Read the files. If a file is already well-covered by PostHog events, replace it with another option. Do not spawn subagents. + +Look for opportunities to track client-side events. + +**IMPORTANT: Server-side events are REQUIRED** if the project includes any instrumentable server-side code. If the project has API routes (e.g., `app/api/**/route.ts`) or Server Actions, you MUST include server-side events for critical business operations like: + + - Payment/checkout completion + - Webhook handlers + - Authentication endpoints + +Do not skip server-side events - they capture actions that cannot be tracked client-side. + +Create a new file with a JSON array at the root of the project: .posthog-events.json. It should include one object for each event we want to add with these exact field names: `event_name` (the event name), `event_description` (one sentence), and `file` (the file path the event goes in). The wizard reads this file to surface the plan in the UI. If events already exist, don't duplicate them; supplement them. + +Track actions only, not pageviews. These can be captured automatically. Exceptions can be made for "viewed"-type events that correspond to the top of a conversion funnel. + +As you review files, make an internal note of opportunities to identify users and catch errors. We'll need them for the next step. + +## Status + +Before beginning a phase of the setup, you will send a status message with the exact prefix '[STATUS]', as in: + +[STATUS] Checking project structure. + +Status to report in this phase: + +- Checking project structure +- Verifying PostHog dependencies +- Generating events based on project + +## Abort statuses + +If and only if the instructions have `[ABORT]` states specified, and you clearly match the conditions for an abort, emit the abort message. Do NOT attempt to exit or halt yourself — the wizard's middleware catches `[ABORT]` and terminates the run for you. + +--- + +**Upon completion, continue with:** [2-edit.md](2-edit.md) \ No newline at end of file diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/2-edit.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/2-edit.md new file mode 100644 index 000000000..e5f7ffd16 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/2-edit.md @@ -0,0 +1,36 @@ +--- +title: PostHog Setup - Edit +description: Implement PostHog event tracking in the identified files, following best practices and the example project +--- + +For each of the files and events noted in .posthog-events.json, make edits to capture events using PostHog. Make sure to set up any helper files needed. Carefully examine the included example project code: your implementation should match it as closely as possible. Do not spawn subagents. + +Use environment variables for PostHog keys. Do not hardcode PostHog keys. + +If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it. + +For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach. + +Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference. + +Where possible, add calls for PostHog's identify() function on the client side upon events like logins and signups. Use the contents of login and signup forms to identify users on submit. If there is server-side code, pass the client-side session and distinct ID to the server-side code to identify the user. On the server side, make sure events have a matching distinct ID where relevant. + +It's essential to do this in both client code and server code, so that user behavior from both domains is easy to correlate. + +You should also add PostHog exception capture error tracking to these files where relevant. + +Remember: Do not alter the fundamental architecture of existing files. Make your additions minimal and targeted. + +Remember the documentation and example project resources you were provided at the beginning. Read them now. + +## Status + +Status to report in this phase: + +- Inserting PostHog capture code +- A status message for each file whose edits you are planning, including a high level summary of changes +- A status message for each file you have edited + +--- + +**Upon completion, continue with:** [3-revise.md](3-revise.md) \ No newline at end of file diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/3-revise.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/3-revise.md new file mode 100644 index 000000000..3b07f5069 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/3-revise.md @@ -0,0 +1,22 @@ +--- +title: PostHog Setup - Revise +description: Review and fix any errors in the PostHog integration implementation +--- + +Check the project for errors. Read the package.json file for any type checking or build scripts that may provide input about what to fix. Remember that you can find the source code for any dependency in the node_modules directory. Do not spawn subagents. + +Ensure that any components created were actually used. + +Once all other tasks are complete, run any linter or prettier-like scripts found in the package.json, but ONLY on the files you have edited or created during this session. Do not run formatting or linting across the entire project's codebase. + +## Status + +Status to report in this phase: + +- Finding and correcting errors +- Report details of any errors you fix +- Linting, building and prettying + +--- + +**Upon completion, continue with:** [4-conclude.md](4-conclude.md) \ No newline at end of file diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/4-conclude.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/4-conclude.md new file mode 100644 index 000000000..8dc67e10e --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/4-conclude.md @@ -0,0 +1,107 @@ +--- +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. + +When calling `insight-create`, use these known-good query shapes — they are verified against the MCP schema, and the common variations around them are rejected: + +A trends insight with a breakdown (breakdowns go in `breakdownFilter.breakdowns`, an array — there is NO top-level `breakdown` field on `TrendsQuery`): + +```json +{ + "name": "Signups by plan (wizard)", + "dashboards": [], + "query": { + "kind": "InsightVizNode", + "source": { + "kind": "TrendsQuery", + "series": [{ "kind": "EventsNode", "event": "user_signed_up", "math": "total" }], + "interval": "day", + "dateRange": { "date_from": "-30d" }, + "breakdownFilter": { "breakdowns": [{ "type": "event", "property": "plan" }] }, + "trendsFilter": { "display": "ActionsBar" } + } + } +} +``` + +A conversion funnel (the window fields are camelCase and live INSIDE `funnelsFilter` — not at the top level of `FunnelsQuery`, and not snake_case): + +```json +{ + "name": "Signup funnel (wizard)", + "dashboards": [], + "query": { + "kind": "InsightVizNode", + "source": { + "kind": "FunnelsQuery", + "series": [ + { "kind": "EventsNode", "event": "page_viewed" }, + { "kind": "EventsNode", "event": "user_signed_up" } + ], + "dateRange": { "date_from": "-30d" }, + "funnelsFilter": { + "funnelVizType": "steps", + "funnelOrderType": "ordered", + "funnelWindowInterval": 14, + "funnelWindowIntervalUnit": "day" + } + } + } +} +``` + +Valid `trendsFilter.display` values are `ActionsLineGraph`, `ActionsBar`, `ActionsAreaGraph`, `ActionsPie`, `ActionsStackedBar`, `BoldNumber`, and `ActionsTable` — names like `ActionsBarChart` or `ActionsBarGraph` are rejected. If an insight call is rejected anyway, fix the payload against these examples rather than retrying variations. + +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, update `.posthog-events.json` so it matches the events you actually implemented, then remove it with your file tools. If removal is blocked or fails in your environment, leave the file in place and move on — the wizard host cleans it up after the run. Do not retry the removal or reach for shell commands to force it. + +## Status + +Status to report in this phase: + +- Configured dashboard: [insert PostHog dashboard URL] +- Created setup report: [insert full local file path] \ No newline at end of file diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/EXAMPLE.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/EXAMPLE.md new file mode 100644 index 000000000..ad622848d --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/EXAMPLE.md @@ -0,0 +1,851 @@ +# PostHog SvelteKit Example Project + +Repository: https://github.com/PostHog/context-mill +Path: example-apps/sveltekit + +--- + +## README.md + +# SvelteKit PostHog example + +This example demonstrates how to integrate PostHog with a SvelteKit application, including: + +- Client-side PostHog initialization using SvelteKit hooks +- Server-side PostHog tracking with the Node.js SDK +- Reverse proxy to avoid ad blockers +- User identification and event tracking +- Error tracking with `captureException` +- Session replay configuration + +## Getting started + +### 1. Install dependencies + +```bash +npm install +``` + +### 2. Configure environment variables + +Copy the example environment file and add your PostHog credentials: + +```bash +cp .env.example .env +``` + +Edit `.env` with your PostHog project token: + +``` +PUBLIC_POSTHOG_PROJECT_TOKEN=your_posthog_project_token_here +PUBLIC_POSTHOG_HOST=https://us.i.posthog.com +``` + +You can find your project token in your [PostHog project settings](https://app.posthog.com/project/settings). + +### 3. Run the development server + +```bash +npm run dev +``` + +Open [http://localhost:5173](http://localhost:5173) to view the app. + +## Project structure + +``` +src/ +├── lib/ +│ ├── auth.svelte.ts # Auth context with Svelte 5 runes +│ ├── components/ +│ │ └── Header.svelte # Navigation component +│ └── server/ +│ └── posthog.ts # Server-side PostHog singleton +├── routes/ +│ ├── +layout.svelte # Root layout with auth provider +│ ├── +page.svelte # Home/login page +│ ├── burrito/ +│ │ └── +page.svelte # Event tracking demo +│ ├── profile/ +│ │ └── +page.svelte # Error tracking demo +│ └── api/ +│ └── auth/ +│ └── login/ +│ └── +server.ts # Login API with server-side tracking +├── hooks.client.ts # Client-side PostHog init + error handling +├── hooks.server.ts # Server hooks with reverse proxy +├── app.css # Global styles +└── app.html # HTML template +``` + +## Key integration points + +### Client-side initialization (`src/hooks.client.ts`) + +PostHog is initialized in the SvelteKit client hooks `init` function, which runs once when the app starts: + +```typescript +import posthog from 'posthog-js'; + +export async function init() { + posthog.init(PUBLIC_POSTHOG_PROJECT_TOKEN, { + api_host: '/ingest', + ui_host: 'https://us.posthog.com', + defaults: '2026-01-30', + capture_exceptions: true + }); +} +``` + +### Server-side tracking (`src/lib/server/posthog.ts`) + +A singleton pattern ensures one PostHog client instance for server-side tracking: + +```typescript +import { PostHog } from 'posthog-node'; + +let posthogClient: PostHog | null = null; + +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog(PUBLIC_POSTHOG_PROJECT_TOKEN, { + host: PUBLIC_POSTHOG_HOST, + flushAt: 1, + flushInterval: 0 + }); + } + return posthogClient; +} +``` + +### Reverse proxy (`src/hooks.server.ts`) + +The server hooks handle proxies requests through `/ingest` to avoid ad blockers: + +```typescript +export const handle: Handle = async ({ event, resolve }) => { + if (event.url.pathname.startsWith('/ingest')) { + const pathname = event.url.pathname.replace('/ingest', ''); + const host = pathname.startsWith('/static') + ? 'https://us-assets.i.posthog.com' + : 'https://us.i.posthog.com'; + // Proxy to PostHog... + } + return resolve(event); +}; +``` + +### User identification + +When a user logs in, they are identified in PostHog: + +```typescript +import posthog from 'posthog-js'; + +// On login +posthog.identify(userId, { username }); +posthog.capture('user_logged_in', { username }); + +// On logout +posthog.capture('user_logged_out'); +posthog.reset(); +``` + +### Error tracking + +Errors are automatically captured via the `handleError` hook: + +```typescript +export const handleError: HandleClientError = async ({ error }) => { + posthog.captureException(error); + return { message: 'An error occurred' }; +}; +``` + +You can also manually capture errors: + +```typescript +try { + // Some operation +} catch (err) { + posthog.captureException(err); +} +``` + +### Session replay configuration + +For session replay to work correctly, add this to `svelte.config.js`: + +```javascript +export default { + kit: { + paths: { + relative: false + } + } +}; +``` + +## Features demonstrated + +1. **Login page** (`/`) - User authentication with PostHog identification +2. **Burrito page** (`/burrito`) - Custom event tracking with properties +3. **Profile page** (`/profile`) - Error tracking demonstration + +## Learn more + +- [PostHog Svelte documentation](https://posthog.com/docs/libraries/svelte) +- [PostHog SvelteKit proxy setup](https://posthog.com/docs/advanced/proxy/sveltekit) +- [SvelteKit documentation](https://svelte.dev/docs/kit) + +--- + +## .env.example + +```example +# PostHog configuration +# Get your PostHog project token from: https://app.posthog.com/project/settings +PUBLIC_POSTHOG_PROJECT_TOKEN=your_posthog_project_token_here +PUBLIC_POSTHOG_HOST=https://us.i.posthog.com + +``` + +--- + +## .npmrc + +``` +engine-strict=true +min-release-age=7 + +``` + +--- + +## src/app.d.ts + +```ts +// See https://svelte.dev/docs/kit/types#app.d.ts +// for information about these interfaces +declare global { + namespace App { + // interface Error {} + // interface Locals {} + // interface PageData {} + // interface PageState {} + // interface Platform {} + } +} + +export {}; + +``` + +--- + +## src/app.html + +```html + + + + + + %sveltekit.head% + + +
%sveltekit.body%
+ + + +``` + +--- + +## src/hooks.client.ts + +```ts +import posthog from 'posthog-js'; +import { PUBLIC_POSTHOG_PROJECT_TOKEN } from '$env/static/public'; +import type { HandleClientError } from '@sveltejs/kit'; + +// Initialize PostHog when the app starts in the browser +export async function init() { + posthog.init(PUBLIC_POSTHOG_PROJECT_TOKEN, { + api_host: '/ingest', + ui_host: 'https://us.posthog.com', + defaults: '2026-01-30', + capture_exceptions: true + }); +} + +// Capture client-side errors with PostHog +export const handleError: HandleClientError = async ({ error, status, message }) => { + posthog.captureException(error); + + return { + message, + status + }; +}; + +``` + +--- + +## src/hooks.server.ts + +```ts +import type { Handle, HandleServerError } from '@sveltejs/kit'; +import { getPostHogClient } from '$lib/server/posthog'; + +// Handle requests - includes reverse proxy for PostHog +export const handle: Handle = async ({ event, resolve }) => { + const { pathname } = event.url; + + // Reverse proxy for PostHog - route /ingest requests to PostHog servers + if (pathname.startsWith('/ingest')) { + const useAssetHost = pathname.startsWith('/ingest/static/') || pathname.startsWith('/ingest/array/') + const hostname = useAssetHost ? 'us-assets.i.posthog.com' : 'us.i.posthog.com'; + + const url = new URL(event.request.url); + url.protocol = 'https:'; + url.hostname = hostname; + url.port = '443'; + url.pathname = pathname.replace(/^\/ingest/, ''); + + const headers = new Headers(event.request.headers); + headers.set('host', hostname); + headers.set('accept-encoding', ''); + + const clientIp = event.request.headers.get('x-forwarded-for') || event.getClientAddress(); + if (clientIp) { + headers.set('x-forwarded-for', clientIp); + } + + const response = await fetch(url.toString(), { + method: event.request.method, + headers, + body: event.request.body, + // @ts-expect-error - duplex is required for streaming request bodies + duplex: 'half' + }); + + return response; + } + + return resolve(event); +}; + +// Capture server-side errors with PostHog +export const handleError: HandleServerError = async ({ error, status, message }) => { + const posthog = getPostHogClient(); + + posthog.capture({ + distinctId: 'server', + event: 'server_error', + properties: { + error: error instanceof Error ? error.message : String(error), + status, + message + } + }); + + return { + message, + status + }; +}; + +``` + +--- + +## src/lib/auth.svelte.ts + +```ts +import { getContext, setContext } from 'svelte'; +import posthog from 'posthog-js'; +import { browser } from '$app/environment'; + +export interface User { + username: string; + burritoConsiderations: number; +} + +const AUTH_KEY = Symbol('auth'); + +// Class-based auth state using Svelte 5 $state in class fields +// This is the recommended pattern for encapsulating reactive state + behavior +export class AuthState { + user = $state(null); + + constructor() { + // Restore user from localStorage on creation (browser only) + if (browser) { + const storedUsername = localStorage.getItem('currentUser'); + if (storedUsername) { + this.user = { username: storedUsername, burritoConsiderations: 0 }; + } + } + } + + login = async (username: string, password: string): Promise => { + try { + const response = await fetch('/api/auth/login', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username, password }) + }); + + if (response.ok) { + const { user: userData } = await response.json(); + this.user = userData as User; + + if (browser) { + localStorage.setItem('currentUser', username); + posthog.identify(username, { username }); + posthog.capture('user_logged_in', { username }); + } + + return true; + } + return false; + } catch (error) { + console.error('Login error:', error); + return false; + } + }; + + logout = (): void => { + if (browser) { + posthog.capture('user_logged_out'); + posthog.reset(); + localStorage.removeItem('currentUser'); + } + this.user = null; + }; + + incrementBurritoConsiderations = (): void => { + if (this.user) { + this.user = { + ...this.user, + burritoConsiderations: this.user.burritoConsiderations + 1 + }; + } + }; +} + +export function setAuthContext(auth: AuthState) { + setContext(AUTH_KEY, auth); +} + +export function getAuthContext(): AuthState { + return getContext(AUTH_KEY); +} + +``` + +--- + +## src/lib/components/Header.svelte + +```svelte + + +
+
+ +
+ {#if auth.user} + Welcome, {auth.user.username} + + {/if} +
+
+
+ +``` + +--- + +## src/lib/index.ts + +```ts +// place files you want to import through the `$lib` alias in this folder. + +``` + +--- + +## src/lib/server/posthog.ts + +```ts +import { PostHog } from 'posthog-node'; +import { PUBLIC_POSTHOG_PROJECT_TOKEN, PUBLIC_POSTHOG_HOST } from '$env/static/public'; + +let posthogClient: PostHog | null = null; + +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog(PUBLIC_POSTHOG_PROJECT_TOKEN, { + host: PUBLIC_POSTHOG_HOST, + flushAt: 1, + flushInterval: 0 + }); + } + return posthogClient; +} + +export async function shutdownPostHog() { + if (posthogClient) { + await posthogClient.shutdown(); + } +} + +``` + +--- + +## src/routes/+layout.svelte + +```svelte + + + + Burrito consideration app + + + +
+
+ {@render children()} +
+ +``` + +--- + +## src/routes/+page.svelte + +```svelte + + +
+ {#if auth.user} +

Welcome back, {auth.user.username}!

+

You are now logged in. Check out the navigation to explore features.

+ + {:else} +

Welcome to Burrito consideration app

+

Sign in to start considering burritos.

+ +
+
+ + +
+ +
+ + +
+ + {#if error} +

{error}

+ {/if} + + +
+ +

+ Enter any username and password to sign in. This is a demo app. +

+ {/if} +
+ +``` + +--- + +## src/routes/api/auth/login/+server.ts + +```ts +import { json } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; +import { getPostHogClient } from '$lib/server/posthog'; + +const users = new Map(); + +export const POST: RequestHandler = async ({ request }) => { + const { username, password } = await request.json(); + + if (!username || !password) { + return json({ error: 'Username and password required' }, { status: 400 }); + } + + let user = users.get(username); + const isNewUser = !user; + + if (!user) { + user = { username, burritoConsiderations: 0 }; + users.set(username, user); + } + + // Capture server-side login event with user context + const posthog = getPostHogClient(); + posthog.withContext( + { + distinctId: username, + personProperties: { + username, + createdAt: isNewUser ? new Date().toISOString() : undefined + } + }, + () => { + posthog.capture({ + event: 'server_login', + properties: { + isNewUser, + source: 'api' + } + }); + } + ); + + // Flush events to ensure they're sent + await posthog.flush(); + + return json({ success: true, user }); +}; + +``` + +--- + +## src/routes/burrito/+page.svelte + +```svelte + + +
+ {#if auth.user} +

Burrito consideration zone

+

This is where you consider the infinite potential of burritos.

+

Current considerations: {auth.user.burritoConsiderations}

+ + + + {#if hasConsidered} +

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

+ {/if} + +
+

Each consideration is tracked as a PostHog event with custom properties.

+
+ {:else} +

Please log in to consider burritos.

+ {/if} +
+ +``` + +--- + +## src/routes/profile/+page.svelte + +```svelte + + +
+ {#if auth.user} +

User profile

+ +
+

Your information

+

Username: {auth.user.username}

+

Burrito considerations: {auth.user.burritoConsiderations}

+
+ +

Error tracking demo

+

Click the button below to trigger a test error that will be captured by PostHog.

+ + + +
+

This demonstrates PostHog's error tracking capabilities.

+

The error will appear in your PostHog error tracking dashboard.

+
+ {:else} +

Please log in to view your profile.

+ {/if} +
+ +``` + +--- + +## static/robots.txt + +```txt +# allow crawling everything by default +User-agent: * +Disallow: + +``` + +--- + +## svelte.config.js + +```js +import adapter from '@sveltejs/adapter-auto'; +import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; + +/** @type {import('@sveltejs/kit').Config} */ +const config = { + // Consult https://svelte.dev/docs/kit/integrations + // for more information about preprocessors + preprocess: vitePreprocess(), + + kit: { + // adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list. + // If your environment is not supported, or you settled on a specific environment, switch out the adapter. + // See https://svelte.dev/docs/kit/adapters for more information about adapters. + adapter: adapter(), + // Required for PostHog session replay to work correctly with SSR + paths: { + relative: false + } + } +}; + +export default config; + +``` + +--- + +## vite.config.ts + +```ts +import { sveltekit } from '@sveltejs/kit/vite'; +import { defineConfig } from 'vite'; + +export default defineConfig({ + plugins: [sveltekit()] +}); + +``` + +--- + diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/identify-users.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/identify-users.md new file mode 100644 index 000000000..1417e03a8 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/identify-users.md @@ -0,0 +1,272 @@ +# Identify users - Docs + +Linking events to specific users enables you to build a full picture of how they're using your product across different sessions, devices, and platforms. + +This is straightforward to do when [capturing backend events](/docs/product-analytics/capture-events?tab=Node.js.md), as you associate events to a specific user using a `distinct_id`, which is a required argument. + +However, in the frontend of a [web](/docs/libraries/js/features.md#capturing-events) or [mobile app](/docs/libraries/ios.md#capturing-events), a `distinct_id` is not a required argument — PostHog's SDKs will generate an anonymous `distinct_id` for you automatically and you can capture events anonymously, provided you use the appropriate [configuration](/docs/libraries/js/features.md#capturing-anonymous-events). + +To link events to specific users, call `identify`: + +PostHog AI + +### Web + +```javascript +posthog.identify( + 'distinct_id', // Replace 'distinct_id' with your user's unique identifier + { email: 'max@hedgehogmail.com', name: 'Max Hedgehog' } // optional: set additional person properties +); +``` + +### Android + +```kotlin +PostHog.identify( + distinctId = distinctID, // Replace 'distinctID' with your user's unique identifier + // optional: set additional person properties + userProperties = mapOf( + "name" to "Max Hedgehog", + "email" to "max@hedgehogmail.com" + ) +) +``` + +### iOS + +```swift +PostHogSDK.shared.identify("distinct_id", // Replace "distinct_id" with your user's unique identifier + userProperties: ["name": "Max Hedgehog", "email": "max@hedgehogmail.com"]) // optional: set additional person properties +``` + +### React Native + +```jsx +posthog.identify('distinct_id', { // Replace "distinct_id" with your user's unique identifier + email: 'max@hedgehogmail.com', // optional: set additional person properties + name: 'Max Hedgehog' +}) +``` + +### Dart + +```dart +await Posthog().identify( + userId: 'distinct_id', // Replace "distinct_id" with your user's unique identifier + userProperties: { + 'email': 'max@hedgehogmail.com', // optional: set additional person properties + 'name': 'Max Hedgehog', + }, +); +``` + +Events captured after calling `identify` are identified events and this creates a person profile if one doesn't exist already. + +Due to the cost of processing them, anonymous events can be up to 4x cheaper than identified events, so it's recommended you only capture identified events when needed. + +## How identify works + +When a user starts browsing your website or app, PostHog automatically assigns them an **anonymous ID**, which is stored locally. + +Provided you've [configured persistence](/docs/libraries/js/persistence.md) to use cookies or `localStorage`, this enables us to track anonymous users – even across different sessions. + +By calling `identify` with a `distinct_id` of your choice (usually the user's ID in your database, or their email), you link the anonymous ID and distinct ID together. + +Thus, all past and future events made with that anonymous ID are now associated with the distinct ID. + +This enables you to do things like associate events with a user from before they log in for the first time, or associate their events across different devices or platforms. + +Using identify in the backend + +Although you can call `identify` using our backend SDKs, it is used most in frontends. This is because there is no concept of anonymous sessions in the backend SDKs, so calling `identify` only updates person profiles. + +## Best practices when using `identify` + +### 1\. Call `identify` as soon as you're able to + +In your frontend, you should call `identify` as soon as you're able to. + +Typically, this is every time your **app loads** for the first time, and directly after your **users log in**. + +This ensures that events sent during your users' sessions are correctly associated with them. + +You only need to call `identify` once per session, and you should avoid calling it multiple times unnecessarily. + +If you call `identify` multiple times with the same data without reloading the page in between, PostHog will ignore the subsequent calls. + +### 2\. Use unique strings for distinct IDs + +If two users have the same distinct ID, their data is merged and they are considered one user in PostHog. Two common ways this can happen are: + +- Your logic for generating IDs does not generate sufficiently strong IDs and you can end up with a clash where 2 users have the same ID. +- There's a bug, typo, or mistake in your code leading to most or all users being identified with generic IDs like `null`, `true`, or `distinctId`. + +PostHog also has built-in protections to stop the most common distinct ID mistakes. + +### 3\. Reset after logout + +If a user logs out on your frontend, you should call `reset()` to unlink any future events made on that device with that user. + +This is important if your users are sharing a computer, as otherwise all of those users are grouped together into a single user due to shared cookies between sessions. + +**We strongly recommend you call `reset` on logout even if you don't expect users to share a computer.** + +You can do that like so: + +PostHog AI + +### Web + +```javascript +posthog.reset() +``` + +### iOS + +```swift +PostHogSDK.shared.reset() +``` + +### Android + +```kotlin +PostHog.reset() +``` + +### React Native + +```jsx +posthog.reset() +``` + +### Dart + +```dart +await Posthog().reset(); +``` + +If you *also* want to reset the `device_id` so that the device will be considered a new device in future events, you can pass `true` as an argument: + +Web + +PostHog AI + +```javascript +posthog.reset(true) +``` + +### 4\. Person profiles and properties + +You'll notice that one of the parameters in the `identify` method is a `properties` object. + +This enables you to set [person properties](/docs/product-analytics/person-properties.md). + +Whenever possible, we recommend passing in all person properties you have available each time you call identify, as this ensures their person profile on PostHog is up to date. + +Person properties can also be set being adding a `$set` property to a event `capture` call. + +See our [person properties docs](/docs/product-analytics/person-properties.md) for more details on how to work with them and best practices. + +### 5\. Use deep links between platforms + +We recommend you call `identify` [as soon as you're able](#1-call-identify-as-soon-as-youre-able), typically when a user signs up or logs in. + +This doesn't work if one or both platforms are unauthenticated. Some examples of such cases are: + +- Onboarding and signup flows before authentication. +- Unauthenticated web pages redirecting to authenticated mobile apps. +- Authenticated web apps prompting an app download. + +In these cases, you can use a [deep link](https://developer.android.com/training/app-links/deep-linking) on Android and [universal links](https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app) on iOS to identify users. + +1. Use `posthog.get_distinct_id()` to get the current distinct ID. Even if you cannot call identify because the user is unauthenticated, this will return an anonymous distinct ID generated by PostHog. +2. Add the distinct ID to the deep link as query parameters, along with other properties like UTM parameters. +3. When the user is redirected to the app, parse the deep link and handle the following cases: + +- The mobile app is already authenticated. In this case, call [`posthog.alias()`](/docs/libraries/js/features.md#alias) with the distinct ID from the web. This associates the two distinct IDs as a single person. +- The mobile app is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features.md#identifying-users) with the distinct ID from the web so pre-login mobile events stay connected to the web session. When the user later logs in on mobile, call `identify()` again with your canonical user ID. + +As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms. + +Here's an example implementation for handling deep links from web to mobile: + +PostHog AI + +### iOS + +```swift +import PostHog +class DeepLinkIdentityManager { + static let shared = DeepLinkIdentityManager() + // MARK: - Deep Link Received + func handleDeepLink(_ url: URL, isAuthenticatedOnMobile: Bool) { + guard let webDistinctId = URLComponents(url: url, resolvingAgainstBaseURL: true)? + .queryItems?.first(where: { $0.name == "ph_distinct_id" })?.value else { + return + } + if isAuthenticatedOnMobile { + // The mobile app already knows the current user. + // Alias the incoming web distinct ID to that user. + PostHogSDK.shared.alias(webDistinctId) + } else { + // Reuse the web distinct ID until login on mobile. + PostHogSDK.shared.identify(webDistinctId) + } + } + // MARK: - Login/Signup + func handleLogin(canonicalUserId: String) { + // Switch from the web distinct ID (or a mobile anon ID) + // to your canonical user ID. + PostHogSDK.shared.identify(canonicalUserId) + // Set user properties, track signup event, etc. + } + func handleLogout() { + PostHogSDK.shared.reset() + } +} +``` + +### Android + +```kotlin +import android.net.Uri +import com.posthog.PostHog +object DeepLinkIdentityManager { + // Deep Link Received + fun handleDeepLink(uri: Uri, isAuthenticatedOnMobile: Boolean) { + val webDistinctId = uri.getQueryParameter("ph_distinct_id") ?: return + if (isAuthenticatedOnMobile) { + // The mobile app already knows the current user. + // Alias the incoming web distinct ID to that user. + PostHog.alias(webDistinctId) + } else { + // Reuse the web distinct ID until login on mobile. + PostHog.identify(webDistinctId) + } + } + // Login/Signup + fun handleLogin(canonicalUserId: String) { + // Switch from the web distinct ID (or a mobile anon ID) + // to your canonical user ID. + PostHog.identify(canonicalUserId) + // Set user properties, track signup event, etc. + } + fun handleLogout() { + PostHog.reset() + } +} +``` + +## Further reading + +- [Identifying users docs](/docs/product-analytics/identify.md) +- [How person processing works](/docs/how-posthog-works/ingestion-pipeline.md#2-person-processing) +- [An introductory guide to identifying users in PostHog](/tutorials/identifying-users-guide.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/svelte.md b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/svelte.md new file mode 100644 index 000000000..09c2d0d97 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/svelte.md @@ -0,0 +1,234 @@ +# Svelte - Docs + +PostHog makes it easy to get data about traffic and usage of your [Svelte](https://svelte.dev/) app. Integrating PostHog into your site enables analytics about user behavior, custom events capture, session recordings, feature flags, and more. + +This guide walks you through integrating PostHog into your SvelteKit app using the [JavaScript Web](/docs/libraries/js.md) and [Node.js](/docs/libraries/node.md) SDKs. + +## Beta: integration via LLM + +Install PostHog for Svelte in seconds with our wizard by running this prompt with [LLM coding agents](/blog/envoy-wizard-llm-agent.md) like Cursor and Bolt, or by running it in your terminal. + +`npx @posthog/wizard` + +[Learn more](/wizard.md) + +Or, to integrate manually, continue with the rest of this guide. + +## Client-side setup + +Install `posthog-js` using your package manager: + +PostHog AI + +### npm + +```bash +npm install --save posthog-js +``` + +### Yarn + +```bash +yarn add posthog-js +``` + +### pnpm + +```bash +pnpm add posthog-js +``` + +### Bun + +```bash +bun add posthog-js +``` + +Then, if you haven't created a root [layout](https://kit.svelte.dev/docs/routing#layout) already, create a new file called `+layout.js` in your `src/routes` folder In this file, check the environment is the browser, and initialize PostHog if so. You can get both your API key and instance address in your [project settings](https://us.posthog.com/project/settings). + +routes/+layout.js + +PostHog AI + +```javascript +import posthog from 'posthog-js' +import { browser } from '$app/environment'; +export const load = async () => { + if (browser) { + posthog.init('', { + api_host: 'https://us.i.posthog.com', + defaults: '2026-05-30', + }) + } + return +}; +``` + +## 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. + +> ❗️ If you intend on using session replays with a server-side rendered Svelte app ensure that your [asset URLs are configured to be relative](/docs/session-replay/troubleshooting.md#ensure-assets-are-imported-from-the-base-URL-in-Svelte). + +Set up a reverse proxy (recommended) + +We recommend [setting up a reverse proxy](/docs/advanced/proxy.md), so that events are less likely to be intercepted by tracking blockers. + +We have our [own managed reverse proxy service](/docs/advanced/proxy/managed-reverse-proxy.md), which is free for all PostHog Cloud users, routes through our infrastructure, and makes setting up your proxy easy. + +If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using [Cloudflare](/docs/advanced/proxy/cloudflare.md), [AWS Cloudfront](/docs/advanced/proxy/cloudfront.md), and [Vercel](/docs/advanced/proxy/vercel.md). + +Grouping products in one project (recommended) + +If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and [group them in one project](/docs/settings/projects.md). + +This makes it possible to track users across their entire journey (e.g. from visiting your marketing website to signing up for your product), or how they use your product across multiple platforms. + +Add IPs to Firewall/WAF allowlists (recommended) + +For certain features like [heatmaps](/docs/toolbar/heatmaps.md), your Web Application Firewall (WAF) may be blocking PostHog's requests to your site. Add these IP addresses to your WAF allowlist or rules to let PostHog access your site. + +**EU**: `3.75.65.221`, `18.197.246.42`, `3.120.223.253` + +**US**: `44.205.89.55`, `52.4.194.122`, `44.208.188.173` + +These are public, stable IPs used by PostHog services (e.g., Celery tasks for snapshots). + +## Server-side setup + +Install `posthog-node` using your package manager: + +PostHog AI + +### npm + +```bash +npm install posthog-node --save +``` + +### Yarn + +```bash +yarn add posthog-node +``` + +### pnpm + +```bash +pnpm add posthog-node +``` + +### Bun + +```bash +bun add posthog-node +``` + +Then, initialize the PostHog Node client where you'd like to use it on the server side. For example, in a [load function](https://kit.svelte.dev/docs/load#page-data): + +routes/+page.server.js + +PostHog AI + +```javascript +import { PostHog } from 'posthog-node'; +export async function load() { + const posthog = new PostHog('', { host: 'https://us.i.posthog.com' }); + posthog.capture({ + distinctId: 'distinct_id_of_the_user', + event: 'event_name', + }) + await posthog.shutdown() +} +``` + +> **Note:** Make sure to always call `posthog.shutdown()` after capturing events from the server-side. PostHog queues events into larger batches, and this call forces all batched events to be flushed immediately. + +## Feature flags + +To use client-side feature flags, import PostHog into your Svelte component and check if the feature is enabled (while ensuring the code only runs in the browser). + +routes/+page.svelte + +PostHog AI + +```javascript + +{#if coolFeature} +

Welcome to the cool feature!

+{/if} +``` + +To use server-side feature flags, import PostHog into your SvelteKit `load` function and check if the feature is enabled. + +routes/+page.server.js + +PostHog AI + +```javascript +import { PostHog } from 'posthog-node'; +const client = new PostHog( + '', + { host: 'https://us.i.posthog.com' } +); +export async function load() { + const distinctId = 'distinct_id_of_the_user'; + const megaFeature = await client.isFeatureEnabled( + 'mega-feature', + distinctId + ); + return { + megaFeature + }; +} +``` + +See our [JavaScript Web](/docs/libraries/js/features.md#feature-flags) and [Node](/docs/libraries/node.md#feature-flags) docs for more details. + +## Configuring session replay for server-side rendered apps + +By default, [Svelte uses relative asset paths](https://kit.svelte.dev/docs/configuration) during server-side rending. This causes issues with PostHog's ability to record sessions. + +To fix this, set the config to not use relative paths in `svelte.config.js`: + +JavaScript + +PostHog AI + +```javascript +kit: { + paths: { + relative: false, + }, + }, +``` + +## Next steps + +For any technical questions for how to integrate specific PostHog features into Svelte (such as analytics, feature flags, A/B testing, surveys, etc.), have a look at our [JavaScript Web](/docs/libraries/js/features.md) and [Node](/docs/libraries/node.md) SDK docs. + +Alternatively, the following tutorials can help you get started: + +- [How to set up Svelte analytics, feature flags, and more](/tutorials/svelte-analytics.md) +- [How to set up A/B tests in Svelte](/tutorials/svelte-ab-tests.md) +- [How to set up surveys in Svelte](/tutorials/svelte-surveys.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/package-lock.json b/apps/basic-integration/sveltekit/CMSaasStarter/package-lock.json index 04d318971..1157f0c59 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/package-lock.json +++ b/apps/basic-integration/sveltekit/CMSaasStarter/package-lock.json @@ -13,6 +13,8 @@ "@supabase/ssr": "^0.5.2", "@supabase/supabase-js": "^2.45.2", "handlebars": "^4.7.8", + "posthog-js": "^1.396.5", + "posthog-node": "^5.39.4", "resend": "^3.5.0", "stripe": "^13.3.0" }, @@ -800,6 +802,21 @@ "dev": true, "license": "MIT" }, + "node_modules/@posthog/core": { + "version": "1.39.6", + "resolved": "https://registry.npmjs.org/@posthog/core/-/core-1.39.6.tgz", + "integrity": "sha512-o6ajIwN5zXoNP0D4H/QPmOyibNTUkSyOR6ya7AG5U2ywXx4awo72L2KnCoiZPQM5x/bXv6jPBdimH8M18Ax0aw==", + "license": "MIT", + "dependencies": { + "@posthog/types": "^1.392.0" + } + }, + "node_modules/@posthog/types": { + "version": "1.392.0", + "resolved": "https://registry.npmjs.org/@posthog/types/-/types-1.392.0.tgz", + "integrity": "sha512-nctNujXL3FC1v99FktaTMSugSD9ZOZekEpahUSafkU2TSvW+XGKNkQZbokuJtiWvPBK208dwMJva8UfBkChqpw==", + "license": "MIT" + }, "node_modules/@react-email/render": { "version": "0.0.16", "resolved": "https://registry.npmjs.org/@react-email/render/-/render-0.0.16.tgz", @@ -1214,7 +1231,6 @@ "resolved": "https://registry.npmjs.org/@supabase/supabase-js/-/supabase-js-2.90.1.tgz", "integrity": "sha512-U8KaKGLUgTIFHtwEW1dgw1gK7XrdpvvYo7nzzqPx721GqPe8WZbAiLh/hmyKLGBYQ/mmQNr20vU9tWSDZpii3w==", "license": "MIT", - "peer": true, "dependencies": { "@supabase/auth-js": "2.90.1", "@supabase/functions-js": "2.90.1", @@ -1255,7 +1271,6 @@ "integrity": "sha512-dCYqelr2RVnWUuxc+Dk/dB/SjV/8JBndp1UovCyCZdIQezd8TRwFLNZctYkzgHxRJtaNvseCSRsuuHPeUgIN/A==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@standard-schema/spec": "^1.0.0", "@sveltejs/acorn-typescript": "^1.0.5", @@ -1299,7 +1314,6 @@ "integrity": "sha512-MCFS6CrQDu1yGwspm4qtli0e63vaPCehf6V7pIMP15AsWgMKrqDGCPFF/0kn4SP0ii4aySu4Pa62+fIRGFMjgw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@sveltejs/vite-plugin-svelte-inspector": "^4.0.1", "debug": "^4.4.0", @@ -1682,6 +1696,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/trusted-types": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz", + "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", + "license": "MIT", + "optional": true + }, "node_modules/@types/ws": { "version": "8.18.1", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz", @@ -1733,7 +1754,6 @@ "integrity": "sha512-tbsV1jPne5CkFQCgPBcDOt30ItF7aJoZL997JSF7MhGQqOeT3svWRYxiqlfA5RUdlHN6Fi+EI9bxqbdyAUZjYQ==", "dev": true, "license": "BSD-2-Clause", - "peer": true, "dependencies": { "@typescript-eslint/scope-manager": "6.21.0", "@typescript-eslint/types": "6.21.0", @@ -2024,7 +2044,6 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.14.1.tgz", "integrity": "sha512-OvQ/2pUDKmgfCg++xsTX1wGxfTaszcHVcTctW4UJB4hibJx2HXxxO5UmVgyjMa+ZDsiaf5wWLXYpRWMmBI0QHg==", "license": "MIT", - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -2515,6 +2534,17 @@ "node": ">= 0.6" } }, + "node_modules/core-js": { + "version": "3.49.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.49.0.tgz", + "integrity": "sha512-es1U2+YTtzpwkxVLwAFdSpaIMyQaq0PBgm3YD1W3Qpsn1NAmO3KSgZfu+oGSWVu6NvLHoHCV/aYcsE5wiB7ALg==", + "hasInstallScript": true, + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/core-js" + } + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -2798,6 +2828,15 @@ "url": "https://github.com/fb55/domhandler?sponsor=1" } }, + "node_modules/dompurify": { + "version": "3.4.11", + "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.11.tgz", + "integrity": "sha512-zhlUV12GsaRzMsf9q5M254YhA4+VuF0fG+QFqu6aYpoGlKtz+w8//jBcGVYBgQkR5GHjUomejY84AV+/uPbWdw==", + "license": "(MPL-2.0 OR Apache-2.0)", + "optionalDependencies": { + "@types/trusted-types": "^2.0.7" + } + }, "node_modules/domutils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/domutils/-/domutils-3.1.0.tgz", @@ -3011,7 +3050,6 @@ "deprecated": "This version is no longer supported. Please see https://eslint.org/version-support for other options.", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", @@ -3378,6 +3416,12 @@ "reusify": "^1.0.4" } }, + "node_modules/fflate": { + "version": "0.4.8", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.4.8.tgz", + "integrity": "sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==", + "license": "MIT" + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -4042,7 +4086,8 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/js-yaml": { "version": "4.1.1", @@ -4500,6 +4545,7 @@ "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", "license": "MIT", + "peer": true, "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, @@ -4938,7 +4984,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "nanoid": "^3.3.8", "picocolors": "^1.1.1", @@ -5046,6 +5091,52 @@ "node": ">=4" } }, + "node_modules/posthog-js": { + "version": "1.396.5", + "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.396.5.tgz", + "integrity": "sha512-huW/8R151I08i/tkOaAv3Q/yWVqzhqPj3SxyhpgNLB/xysaNeV1ibp6DHzbRWV5D7FRM48Zg6Icxsm4q0irEtg==", + "license": "(Apache-2.0 AND MIT)", + "dependencies": { + "@posthog/core": "^1.39.5", + "@posthog/types": "^1.392.0", + "core-js": "^3.38.1", + "dompurify": "^3.3.2", + "fflate": "^0.4.8", + "preact": "^10.29.2", + "query-selector-shadow-dom": "^1.0.1", + "web-vitals": "^5.3.0" + } + }, + "node_modules/posthog-node": { + "version": "5.39.4", + "resolved": "https://registry.npmjs.org/posthog-node/-/posthog-node-5.39.4.tgz", + "integrity": "sha512-+fCQ7htBFRQQFbIzl1T0TA7bDwYyaB9XP308ZFMCUoB5LzTzOFxBa6TYVrxdH/VQl43WXTp6sf0QsG2Z4XlNBg==", + "license": "MIT", + "dependencies": { + "@posthog/core": "^1.39.5" + }, + "engines": { + "node": "^20.20.0 || >=22.22.0" + }, + "peerDependencies": { + "rxjs": "^7.0.0" + }, + "peerDependenciesMeta": { + "rxjs": { + "optional": true + } + } + }, + "node_modules/preact": { + "version": "10.29.3", + "resolved": "https://registry.npmjs.org/preact/-/preact-10.29.3.tgz", + "integrity": "sha512-D9NL1GAnJZhc3RndVs4gDdxEeU9TcHgywMrhhOsnpdlvFjdbx0gAsLUnH6JEhlJH5giL7Tx5biWPUSEXE/HPzw==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/preact" + } + }, "node_modules/prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", @@ -5062,7 +5153,6 @@ "integrity": "sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==", "dev": true, "license": "MIT", - "peer": true, "bin": { "prettier": "bin/prettier.cjs" }, @@ -5128,6 +5218,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/query-selector-shadow-dom": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/query-selector-shadow-dom/-/query-selector-shadow-dom-1.0.1.tgz", + "integrity": "sha512-lT5yCqEBgfoMYpf3F2xQRK7zEr1rhIIZuceDK6+xRkJQ4NMbHTwXqk4NkwDwQMNqXgG9r9fyHnzwNVs6zV5KRw==", + "license": "MIT" + }, "node_modules/querystringify": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", @@ -5433,6 +5529,7 @@ "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.2.tgz", "integrity": "sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==", "license": "MIT", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" } @@ -5810,7 +5907,6 @@ "resolved": "https://registry.npmjs.org/svelte/-/svelte-5.14.0.tgz", "integrity": "sha512-xHrS9dd2Ci9GJd2sReNFqJztoe515wB4OzsPw4A8L2M6lddLFkREkWDJnM5DAND30Zyvjwc1icQVzH0F+Sdx5A==", "license": "MIT", - "peer": true, "dependencies": { "@ampproject/remapping": "^2.3.0", "@jridgewell/sourcemap-codec": "^1.5.0", @@ -5945,8 +6041,7 @@ "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-4.0.9.tgz", "integrity": "sha512-12laZu+fv1ONDRoNR9ipTOpUD7RN9essRVkX36sjxuRUInpN7hIiHN4lBd/SIFjbISvnXzp8h/hXzmU8SQQYhw==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/tapable": { "version": "2.2.1", @@ -6017,7 +6112,6 @@ "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=12" }, @@ -6145,7 +6239,6 @@ "integrity": "sha512-i5t66RHxDvVN40HfDd1PsEThGNnlMCMT3jMUuoh9/0TaqWevNontacunWyN02LA9/fIbEWlcHZcgTKb9QoaLfg==", "dev": true, "license": "Apache-2.0", - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -6227,7 +6320,6 @@ "integrity": "sha512-+Oxm7q9hDoLMyJOYfUYBuHQo+dkAloi33apOPP56pzj+vsdJDzr+j1NISE5pyaAuKL4A3UD34qd0lx5+kfKp2g==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "esbuild": "^0.25.0", "fdir": "^6.4.4", @@ -6341,7 +6433,6 @@ "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=12" }, @@ -6451,6 +6542,12 @@ "node": ">=18" } }, + "node_modules/web-vitals": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-5.3.0.tgz", + "integrity": "sha512-q6LWsLatGYZp5VGBIOvbTj6JBV2nOmC8KvWztXBmwJcfFAzhwKwbOxhUH306XY3CcaZDUlSmSuNPBsCn0bFu+g==", + "license": "Apache-2.0" + }, "node_modules/webidl-conversions": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/package.json b/apps/basic-integration/sveltekit/CMSaasStarter/package.json index be50d6ac6..3931a5add 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/package.json +++ b/apps/basic-integration/sveltekit/CMSaasStarter/package.json @@ -51,6 +51,8 @@ "@supabase/ssr": "^0.5.2", "@supabase/supabase-js": "^2.45.2", "handlebars": "^4.7.8", + "posthog-js": "^1.396.5", + "posthog-node": "^5.39.4", "resend": "^3.5.0", "stripe": "^13.3.0" }, diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/posthog-setup-report.md b/apps/basic-integration/sveltekit/CMSaasStarter/posthog-setup-report.md new file mode 100644 index 000000000..2819abb93 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/posthog-setup-report.md @@ -0,0 +1,44 @@ + +# PostHog post-wizard report + +The wizard completed a PostHog integration for this SvelteKit app across both browser and server paths. It installed `posthog-js` and `posthog-node`, added client initialization in a shared helper and the root layout, added a SvelteKit `/ingest` reverse proxy plus server-side exception capture in `src/hooks.server.ts`, created a reusable server PostHog client, instrumented authentication, onboarding, contact, billing, and search flows, and configured `PUBLIC_POSTHOG_PROJECT_TOKEN` / `PUBLIC_POSTHOG_HOST` in `.env`. It also updated `svelte.config.js` to set `paths.relative = false` for replay compatibility and created a PostHog dashboard with five insights. + +| Event name | Description | File | +| --- | --- | --- | +| `auth_sign_in_succeeded` | Captures successful sign-in completion from the hosted auth flow. | `src/routes/(marketing)/login/sign_in/+page.svelte` | +| `auth_sign_up_viewed` | Captures when the sign-up experience is shown as a top-of-funnel conversion step. | `src/routes/(marketing)/login/sign_up/+page.svelte` | +| `auth_callback_completed` | Captures successful auth callback exchanges on the server after session creation. | `src/routes/(marketing)/auth/callback/+server.js` | +| `auth_signed_out` | Captures sign-out actions from authenticated account navigation and auth state changes. | `src/routes/(admin)/account/(menu)/+layout.svelte`, `src/routes/(admin)/account/+layout.svelte` | +| `contact_request_submitted` | Captures successful contact form submissions after validation and persistence complete. | `src/routes/(marketing)/contact_us/+page.server.ts` | +| `profile_created` | Captures first-time profile completion during onboarding. | `src/routes/(admin)/account/api/+page.server.ts` | +| `profile_updated` | Captures subsequent profile updates from account settings. | `src/routes/(admin)/account/api/+page.server.ts` | +| `email_subscription_toggled` | Captures when a signed-in user changes email subscription status. | `src/routes/(admin)/account/api/+page.server.ts` | +| `checkout_started` | Captures when a signed-in user starts a Stripe checkout flow for a plan. | `src/routes/(admin)/account/subscribe/[slug]/+page.server.ts` | +| `billing_portal_opened` | Captures when a signed-in customer opens the Stripe billing portal. | `src/routes/(admin)/account/(menu)/billing/manage/+page.server.ts` | +| `search_used` | Captures when a visitor performs a search and sees matching results. | `src/routes/(marketing)/search/+page.svelte` | +| `$exception` | Captures server-side exceptions through the global SvelteKit error handler. | `src/hooks.server.ts` | + +## 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: + +- Dashboard: https://us.posthog.com/project/483112/dashboard/1825446 +- Insight: Auth sign-ins (wizard) — https://us.posthog.com/project/483112/insights/xPuynETH +- Insight: Onboarding conversion (wizard) — https://us.posthog.com/project/483112/insights/POPtLm49 +- Insight: Billing flow starts (wizard) — https://us.posthog.com/project/483112/insights/LVAgvJVF +- Insight: Contact requests (wizard) — https://us.posthog.com/project/483112/insights/N7CJmXEX +- Insight: Search engagement (wizard) — https://us.posthog.com/project/483112/insights/IQovQKA2 + +## 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 the exact PostHog env var names you added to `.env.example` and any monorepo/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` — a handler that only identifies on fresh login can leave returning sessions on anonymous distinct IDs. + +### 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/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.server.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.server.ts index 166bff0df..36f8211e0 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.server.ts +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.server.ts @@ -6,8 +6,9 @@ import { } from "$env/static/public" import { createServerClient } from "@supabase/ssr" import { createClient, type AMREntry } from "@supabase/supabase-js" -import type { Handle } from "@sveltejs/kit" +import type { Handle, HandleServerError } from "@sveltejs/kit" import { sequence } from "@sveltejs/kit/hooks" +import { getPostHogClient } from "$lib/server/posthog" export const supabase: Handle = async ({ event, resolve }) => { event.locals.supabase = createServerClient( @@ -104,4 +105,57 @@ const authGuard: Handle = async ({ event, resolve }) => { return resolve(event) } -export const handle: Handle = sequence(supabase, authGuard) +const posthogProxy: Handle = async ({ event, resolve }) => { + const { pathname } = event.url + + if (pathname.startsWith("/ingest")) { + const useAssetHost = + pathname.startsWith("/ingest/static/") || + pathname.startsWith("/ingest/array/") + const hostname = useAssetHost ? "us-assets.i.posthog.com" : "us.i.posthog.com" + + const url = new URL(event.request.url) + url.protocol = "https:" + url.hostname = hostname + url.port = "443" + url.pathname = pathname.replace(/^\/ingest/, "") + + const headers = new Headers(event.request.headers) + headers.set("host", hostname) + headers.set("accept-encoding", "") + + const clientIp = event.request.headers.get("x-forwarded-for") + if (clientIp) { + headers.set("x-forwarded-for", clientIp) + } + + return fetch(url.toString(), { + method: event.request.method, + headers, + body: event.request.body, + // @ts-expect-error - duplex is required for streaming request bodies + duplex: "half", + }) + } + + return resolve(event) +} + +export const handleError: HandleServerError = async ({ error, event, status, message }) => { + const posthog = getPostHogClient() + + posthog.captureException(error, event.locals.user?.id ?? "server", { + path: event.url.pathname, + status, + message, + }) + + await posthog.flush() + + return { + message, + status, + } +} + +export const handle: Handle = sequence(posthogProxy, supabase, authGuard) diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/posthog.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/posthog.ts new file mode 100644 index 000000000..07fb0ceed --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/posthog.ts @@ -0,0 +1,25 @@ +import posthog from "posthog-js" +import { browser } from "$app/environment" +import { + PUBLIC_POSTHOG_HOST, + PUBLIC_POSTHOG_PROJECT_TOKEN, +} from "$env/static/public" + +let initialized = false + +export function initPostHog() { + if (!browser || initialized || !PUBLIC_POSTHOG_PROJECT_TOKEN) { + return + } + + posthog.init(PUBLIC_POSTHOG_PROJECT_TOKEN, { + api_host: "/ingest", + ui_host: PUBLIC_POSTHOG_HOST, + defaults: "2026-01-30", + capture_exceptions: true, + }) + + initialized = true +} + +export { posthog } diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/server/posthog.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/server/posthog.ts new file mode 100644 index 000000000..19df88c51 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/server/posthog.ts @@ -0,0 +1,23 @@ +import { PUBLIC_POSTHOG_HOST, PUBLIC_POSTHOG_PROJECT_TOKEN } from "$env/static/public" +import { PostHog } from "posthog-node" + +let posthogClient: PostHog | null = null + +export function getPostHogClient() { + if (!posthogClient) { + posthogClient = new PostHog(PUBLIC_POSTHOG_PROJECT_TOKEN, { + host: PUBLIC_POSTHOG_HOST, + flushAt: 1, + flushInterval: 0, + enableExceptionAutocapture: true, + }) + } + + return posthogClient +} + +export async function shutdownPostHog() { + if (posthogClient) { + await posthogClient.shutdown() + } +} diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/(menu)/+layout.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/(menu)/+layout.svelte index aaef7a012..263aa1121 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/(menu)/+layout.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/(menu)/+layout.svelte @@ -1,8 +1,10 @@ diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/+layout.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/+layout.ts index b3e5efc7f..5459b9d5e 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/+layout.ts +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/+layout.ts @@ -1,4 +1,5 @@ import { + PUBLIC_POSTHOG_PROJECT_TOKEN, PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL, } from "$env/static/public" @@ -63,6 +64,7 @@ export const load = async ({ fetch, data, depends, url }) => { profile, user, amr: aal?.currentAuthenticationMethods, + posthogConfigured: Boolean(PUBLIC_POSTHOG_PROJECT_TOKEN), } } diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/api/+page.server.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/api/+page.server.ts index 0ed47a19e..ab854ccf1 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/api/+page.server.ts +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/api/+page.server.ts @@ -1,6 +1,7 @@ import { fail, redirect } from "@sveltejs/kit" import { sendAdminEmail, sendUserEmail } from "$lib/mailer" import { WebsiteBaseUrl } from "../../../../config" +import { getPostHogClient } from "$lib/server/posthog" export const actions = { toggleEmailSubscription: async ({ locals: { supabase, safeGetSession } }) => { @@ -28,6 +29,16 @@ export const actions = { return fail(500, { message: "Failed to update subscription status" }) } + const posthog = getPostHogClient() + posthog.capture({ + distinctId: session.user.id, + event: "email_subscription_toggled", + properties: { + unsubscribed: newUnsubscribedStatus, + }, + }) + await posthog.flush() + return { unsubscribed: newUnsubscribedStatus, } @@ -303,6 +314,18 @@ export const actions = { // If the profile was just created, send an email to the user and admin const newProfile = priorProfile?.updated_at === null && priorProfileError === null + + const posthog = getPostHogClient() + posthog.capture({ + distinctId: user.id, + event: newProfile ? "profile_created" : "profile_updated", + properties: { + has_company_name: Boolean(companyName), + has_website: Boolean(website), + }, + }) + await posthog.flush() + if (newProfile) { await sendAdminEmail({ subject: "Profile Created", diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/subscribe/[slug]/+page.server.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/subscribe/[slug]/+page.server.ts index 93da852ae..f401af231 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/subscribe/[slug]/+page.server.ts +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/subscribe/[slug]/+page.server.ts @@ -6,6 +6,7 @@ import { getOrCreateCustomerId, } from "../../subscription_helpers.server" import type { PageServerLoad } from "./$types" +import { getPostHogClient } from "$lib/server/posthog" const stripe = new Stripe(PRIVATE_STRIPE_API_KEY, { apiVersion: "2023-08-16" }) export const load: PageServerLoad = async ({ @@ -57,6 +58,17 @@ export const load: PageServerLoad = async ({ cancel_url: `${url.origin}/account/billing`, }) checkoutUrl = stripeSession.url + + const posthog = getPostHogClient() + posthog.capture({ + distinctId: user.id, + event: "checkout_started", + properties: { + plan_slug: params.slug, + stripe_customer_id_present: Boolean(customerId), + }, + }) + await posthog.flush() } catch (e) { console.error("Error creating checkout session", e) error(500, "Unknown Error (SSE): If issue persists please contact us.") diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/auth/callback/+server.js b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/auth/callback/+server.js index 92e8b437c..fbf940ce1 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/auth/callback/+server.js +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/auth/callback/+server.js @@ -1,6 +1,7 @@ // src/routes/auth/callback/+server.js import { redirect } from "@sveltejs/kit" import { isAuthApiError } from "@supabase/supabase-js" +import { getPostHogClient } from "$lib/server/posthog" export const GET = async ({ url, locals: { supabase } }) => { const code = url.searchParams.get("code") @@ -18,6 +19,23 @@ export const GET = async ({ url, locals: { supabase } }) => { } } + const { + data: { user }, + } = await supabase.auth.getUser() + + if (user?.id) { + const posthog = getPostHogClient() + posthog.capture({ + distinctId: user.id, + event: "auth_callback_completed", + properties: { + next_present: Boolean(url.searchParams.get("next")), + provider: user.app_metadata?.provider ?? "email", + }, + }) + await posthog.flush() + } + const next = url.searchParams.get("next") if (next) { redirect(303, next) diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/contact_us/+page.server.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/contact_us/+page.server.ts index c7ce5c7e4..16f0ec22a 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/contact_us/+page.server.ts +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/contact_us/+page.server.ts @@ -1,5 +1,6 @@ import { fail } from "@sveltejs/kit" import { sendAdminEmail } from "$lib/mailer.js" +import { getPostHogClient } from "$lib/server/posthog" /** @type {import('./$types').Actions} */ export const actions = { @@ -74,5 +75,17 @@ export const actions = { subject: "New contact request", body: `New contact request from ${firstName} ${lastName}.\n\nEmail: ${email}\n\nPhone: ${phone}\n\nCompany: ${company}\n\nMessage: ${message}`, }) + + const posthog = getPostHogClient() + posthog.capture({ + distinctId: `contact_request:${crypto.randomUUID()}`, + event: "contact_request_submitted", + properties: { + has_company: Boolean(company), + has_phone: Boolean(phone), + message_length: message.length, + }, + }) + await posthog.flush() }, } diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/+layout.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/+layout.ts index 2163909ea..2799dc63e 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/+layout.ts +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/+layout.ts @@ -1,4 +1,5 @@ import { + PUBLIC_POSTHOG_PROJECT_TOKEN, PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL, } from "$env/static/public" @@ -38,5 +39,9 @@ export const load = async ({ fetch, data, depends }) => { const url = data.url - return { supabase, url } + return { + supabase, + url, + posthogConfigured: Boolean(PUBLIC_POSTHOG_PROJECT_TOKEN), + } } diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_in/+page.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_in/+page.svelte index 5ee793ced..1ddcb2b51 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_in/+page.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_in/+page.svelte @@ -4,14 +4,27 @@ import { goto } from "$app/navigation" import { onMount } from "svelte" import { page } from "$app/stores" + import { posthog } from "$lib/posthog" let { data } = $props() let { supabase } = data onMount(() => { - supabase.auth.onAuthStateChange((event) => { + const { + data: { subscription }, + } = supabase.auth.onAuthStateChange((event, session) => { // Redirect to account after successful login if (event == "SIGNED_IN") { + const user = session?.user + if (user?.id) { + posthog.identify(user.id, { + email: user.email, + }) + posthog.capture("auth_sign_in_succeeded", { + method: user.app_metadata.provider ?? "email", + }) + } + // Delay needed because order of callback not guaranteed. // Give the layout callback priority to update state or // we'll just bounch back to login when /account tries to load @@ -20,6 +33,8 @@ }, 1) } }) + + return () => subscription.unsubscribe() }) diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_up/+page.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_up/+page.svelte index 7fe447166..15062fb37 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_up/+page.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/login/sign_up/+page.svelte @@ -1,8 +1,16 @@ diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/search/+page.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/search/+page.svelte index f0bd5eddb..b443274a5 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/search/+page.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/search/+page.svelte @@ -5,6 +5,7 @@ import Fuse from "fuse.js" import { goto } from "$app/navigation" import { dev } from "$app/environment" + import { posthog } from "$lib/posthog" const fuseOptions = { keys: [ @@ -49,6 +50,7 @@ } } let results: Result[] = $state([]) + let lastTrackedQuery = $state("") // searchQuery is $page.url.hash minus the "#" at the beginning if present let searchQuery = $state(decodeURIComponent($page.url.hash.slice(1) ?? "")) @@ -57,6 +59,22 @@ results = fuse.search(searchQuery) } }) + + $effect(() => { + const trimmedQuery = searchQuery.trim() + if ( + browser && + trimmedQuery.length >= 2 && + results.length > 0 && + trimmedQuery !== lastTrackedQuery + ) { + posthog.capture("search_used", { + query_length: trimmedQuery.length, + result_count: results.length, + }) + lastTrackedQuery = trimmedQuery + } + }) // Update the URL hash when searchQuery changes so the browser can bookmark/share the search results $effect(() => { if (browser && window.location.hash.slice(1) !== searchQuery) { diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/+layout.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/+layout.svelte index 45621e4a1..863227b98 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/+layout.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/+layout.svelte @@ -3,11 +3,17 @@ import { navigating } from "$app/stores" import { expoOut } from "svelte/easing" import { slide } from "svelte/transition" + import { onMount } from "svelte" + import { initPostHog } from "$lib/posthog" interface Props { children?: import("svelte").Snippet } let { children }: Props = $props() + + onMount(() => { + initPostHog() + }) {#if $navigating} diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js b/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js index d33de4539..92ee31242 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js +++ b/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js @@ -11,6 +11,9 @@ const config = { // allow up to 150kb of style to be inlined with the HTML // Faster FCP (First Contentful Paint) by reducing the number of requests inlineStyleThreshold: 150000, + paths: { + relative: false, + }, }, preprocess: vitePreprocess(), }