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 00000000..e69de29b 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 00000000..4406ef76 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/SKILL.md @@ -0,0 +1,52 @@ +--- +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 +- 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/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 00000000..55f0a832 --- /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 00000000..e5f7ffd1 --- /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 00000000..3b07f506 --- /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 00000000..d876d435 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/.claude/skills/integration-sveltekit/references/4-conclude.md @@ -0,0 +1,57 @@ +--- +title: PostHog Setup - Conclusion +description: Review and fix any errors in the PostHog integration implementation +--- + +Use the PostHog MCP to create a new dashboard named "Analytics basics (wizard)" based on the events created here. Keep the `(wizard)` tag with that exact casing so anyone browsing PostHog can see the wizard created this dashboard, and so a quick search for `(wizard)` surfaces every wizard-created artifact in one go. Make sure to use the exact same event names as implemented in the code. Populate it with up to five insights, with special emphasis on things like conversion funnels, churn events, and other business critical insights. + +Once the dashboard exists, emit its URL on its own line in your assistant message using this exact marker: `[DASHBOARD_URL] `. The wizard parses this marker from your visible message and surfaces the link in the success summary. Mentioning the URL only in thinking or in prose without the marker means the link is dropped. + +Search for a file called `.posthog-events.json` and read it for available events. + +Do not spawn subagents. + +Create the file posthog-setup-report.md. It should include a summary of the integration edits, a table with the event names, event descriptions, and files where events were added, a list of links for the dashboard and insights created, and a "Verify before merging" checklist (see below). Follow this format: + + +# PostHog post-wizard report + +The wizard has completed a deep integration of your project. [Detailed summary of changes] + +[table of events/descriptions/files] + +## Next steps + +We've built some insights and a dashboard for you to keep an eye on user behavior, based on the events we just instrumented: + +[links] + +## Verify before merging + +[checklist] + +### Agent skill + +We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog. + + + +For the "Verify before merging" checklist, write GitHub-style checkboxes (`- [ ] ...`) covering what the developer (or their coding agent) still needs to do to take this from "wizard finished" to "merged". Include ONLY the items that actually apply to the integration you just performed — judge each against the code you changed in this run, and drop any that don't fit. Phrase each item as a concrete, checkable action. Candidate items, with the condition for including each: + +- Always: "Run a full production build (the wizard only verified the files it touched) and fix any lint or type errors introduced by the generated code." +- Always: "Run the test suite — call sites that were rewritten or instrumented may need updated mocks or fixtures." +- If you added environment variables: "Add the exact PostHog env var names you added to `.env.example` and any monorepo/bootstrap scripts so collaborators know what to set." +- If this integration ships a minified production browser bundle (most SPA/SSR web frameworks — e.g. Next.js, Nuxt, SvelteKit, Astro, Vite-based apps): "Wire source-map upload (`posthog-cli sourcemap` or your bundler's upload step) into CI so production stack traces de-minify." +- If LLM analytics was set up in this run: "Trigger the LLM call path(s) you instrumented and confirm `$ai_generation` events appear in PostHog AI Observability." +- If the app has user auth and an `identify` call was added: "Confirm the returning-visitor path also calls `identify` — a handler that only identifies on fresh login can leave returning sessions on anonymous distinct IDs." + +Do not invent items beyond what applies. If only the two "Always" items apply, the checklist is just those two. + +Upon completion, remove .posthog-events.json. + +## Status + +Status to report in this phase: + +- Configured dashboard: [insert PostHog dashboard URL] +- Created setup report: [insert full local file path] \ No newline at end of file diff --git a/apps/basic-integration/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 00000000..ad622848 --- /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 00000000..1417e03a --- /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 00000000..555219c6 --- /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@latest` + +[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-01-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)) 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 04d31897..c740c154 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.391.2", + "posthog-node": "^5.38.2", "resend": "^3.5.0", "stripe": "^13.3.0" }, @@ -800,6 +802,21 @@ "dev": true, "license": "MIT" }, + "node_modules/@posthog/core": { + "version": "1.35.3", + "resolved": "https://registry.npmjs.org/@posthog/core/-/core-1.35.3.tgz", + "integrity": "sha512-EsGPbSLl39Jgo2KZ+kI9UAxFnh5nddaN5bNm2rXvUwF+vGmam9eN1EXeNbxhRU7ulEeIiGdm7XjoU7pzavkgIQ==", + "license": "MIT", + "dependencies": { + "@posthog/types": "^1.390.2" + } + }, + "node_modules/@posthog/types": { + "version": "1.390.2", + "resolved": "https://registry.npmjs.org/@posthog/types/-/types-1.390.2.tgz", + "integrity": "sha512-WcfKz2GNn2vfDX8vXmJYbKxegPxVWHuDQ/pHdAn0HoZDXDFnEp/+x3qBQA+fEvtbPjjtjgAt2wIgJMlM7asx7g==", + "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.391.2", + "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.391.2.tgz", + "integrity": "sha512-q0DZN6ljchSnAFJIXf+sQFTPlsLjTlRa+TvrL+QRb6413BGtib/MNiQy1bnwLKt8KR+f6xJYvkqdLyty9s4Aww==", + "license": "SEE LICENSE IN LICENSE", + "dependencies": { + "@posthog/core": "^1.35.3", + "@posthog/types": "^1.390.2", + "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.38.2", + "resolved": "https://registry.npmjs.org/posthog-node/-/posthog-node-5.38.2.tgz", + "integrity": "sha512-eiKpU+vX4hVuHbO/EosvPHsmh2AVIdoVmWss/uUOs1t4b0ViCblw2o8OIFqHxKj3mYRnSOBlX0Dw3wBvcCaYpA==", + "license": "MIT", + "dependencies": { + "@posthog/core": "^1.35.3" + }, + "engines": { + "node": "^20.20.0 || >=22.22.0" + }, + "peerDependencies": { + "rxjs": "^7.0.0" + }, + "peerDependenciesMeta": { + "rxjs": { + "optional": true + } + } + }, + "node_modules/preact": { + "version": "10.29.2", + "resolved": "https://registry.npmjs.org/preact/-/preact-10.29.2.tgz", + "integrity": "sha512-7tNmwg/7mzzAoB/8kSg6Hl37JraAZw3Z3A0JSY7VXlZwo82Xn0G7wKbNNs2qoF4ZEEsQGTwDAroNdqKs1ofJxQ==", + "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", @@ -5869,6 +5965,21 @@ } } }, + "node_modules/svelte-check/node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/svelte-eslint-parser": { "version": "0.43.0", "resolved": "https://registry.npmjs.org/svelte-eslint-parser/-/svelte-eslint-parser-0.43.0.tgz", @@ -5945,8 +6056,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 +6127,6 @@ "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=12" }, @@ -6145,7 +6254,6 @@ "integrity": "sha512-i5t66RHxDvVN40HfDd1PsEThGNnlMCMT3jMUuoh9/0TaqWevNontacunWyN02LA9/fIbEWlcHZcgTKb9QoaLfg==", "dev": true, "license": "Apache-2.0", - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -6227,7 +6335,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 +6448,6 @@ "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">=12" }, @@ -6451,6 +6557,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 be50d6ac..e4380118 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.391.2", + "posthog-node": "^5.38.2", "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 00000000..35d72070 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/posthog-setup-report.md @@ -0,0 +1,55 @@ +# PostHog post-wizard report + +The wizard has completed a deep integration of PostHog into this SvelteKit SaaS starter project. Here is what was added: + +- **`src/hooks.client.ts`** (new): Initializes PostHog on the client side using the `/ingest` reverse proxy path, enables exception capture (`capture_exceptions: true`), and hooks into `handleError` to automatically report client-side errors. +- **`src/hooks.server.ts`** (updated): Added a `posthogProxy` handle that proxies `/ingest` and `/ingest/static|array` requests to PostHog servers (bypasses ad blockers). Also added `handleError` to capture server-side errors. +- **`src/lib/server/posthog.ts`** (new): Singleton factory for the server-side `posthog-node` client with `flushAt: 1` / `flushInterval: 0` to ensure events are sent immediately from serverless-style endpoints. +- **`svelte.config.js`** (updated): Added `paths.relative: false` — required for session replay to work correctly with SSR. +- **`src/routes/(marketing)/login/sign_in/+page.svelte`** (updated): Calls `posthog.identify()` and captures `user_signed_in` on Supabase `SIGNED_IN` auth state change. +- **`src/routes/(marketing)/login/sign_up/+page.svelte`** (updated): Calls `posthog.identify()` and captures `user_signed_up` on Supabase `SIGNED_IN` auth state change. +- **`src/routes/(admin)/account/sign_out/+page.svelte`** (updated): Captures `user_signed_out` and calls `posthog.reset()` on successful sign-out. +- **`src/routes/(marketing)/pricing/pricing_module.svelte`** (updated): Captures `plan_selected` with `plan_id`, `plan_name`, and `stripe_price_id` when a user clicks a plan CTA. +- **`src/routes/(admin)/account/api/+page.server.ts`** (updated): Added server-side events for `profile_created`, `profile_updated`, `password_changed`, `email_change_initiated`, `account_deleted`, and `email_subscription_toggled`. +- **`src/routes/(admin)/account/subscribe/[slug]/+page.server.ts`** (updated): Captures `subscription_checkout_started` server-side when a Stripe checkout session is created. +- **`src/routes/(marketing)/contact_us/+page.server.ts`** (updated): Captures `contact_form_submitted` server-side with anonymized properties after successful form submission. + +## Events + +| Event Name | Description | File | +|---|---|---| +| `user_signed_in` | Fired when a user successfully signs in via the sign-in page. | `src/routes/(marketing)/login/sign_in/+page.svelte` | +| `user_signed_up` | Fired when a new user completes sign-up via the sign-up page. | `src/routes/(marketing)/login/sign_up/+page.svelte` | +| `user_signed_out` | Fired when a user signs out of their account. | `src/routes/(admin)/account/sign_out/+page.svelte` | +| `profile_created` | Fired server-side when a user creates their profile for the first time. | `src/routes/(admin)/account/api/+page.server.ts` | +| `profile_updated` | Fired server-side when a user updates their existing profile. | `src/routes/(admin)/account/api/+page.server.ts` | +| `subscription_checkout_started` | Fired server-side when a Stripe checkout session is created. | `src/routes/(admin)/account/subscribe/[slug]/+page.server.ts` | +| `contact_form_submitted` | Fired server-side when a visitor submits the contact us form. | `src/routes/(marketing)/contact_us/+page.server.ts` | +| `password_changed` | Fired server-side when a user successfully changes their password. | `src/routes/(admin)/account/api/+page.server.ts` | +| `email_change_initiated` | Fired server-side when a user initiates an email address change. | `src/routes/(admin)/account/api/+page.server.ts` | +| `account_deleted` | Fired server-side when a user successfully deletes their account. | `src/routes/(admin)/account/api/+page.server.ts` | +| `email_subscription_toggled` | Fired server-side when a user toggles their email subscription preference. | `src/routes/(admin)/account/api/+page.server.ts` | +| `plan_selected` | Fired client-side when a user clicks to select a pricing plan. | `src/routes/(marketing)/pricing/pricing_module.svelte` | + +## 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**: [Analytics basics (wizard)](https://us.posthog.com/project/2/dashboard/4500) +- **User Sign-ups Over Time**: [https://us.posthog.com/project/2/insights/8a6d52a4](https://us.posthog.com/project/2/insights/8a6d52a4) +- **Sign-up to Subscription Funnel**: [https://us.posthog.com/project/2/insights/fff4d10b](https://us.posthog.com/project/2/insights/fff4d10b) +- **Contact Form Submissions**: [https://us.posthog.com/project/2/insights/77d4bbc1](https://us.posthog.com/project/2/insights/77d4bbc1) +- **Account Deletions (Churn)**: [https://us.posthog.com/project/2/insights/3b558ed3](https://us.posthog.com/project/2/insights/3b558ed3) +- **Plan Selection Breakdown**: [https://us.posthog.com/project/2/insights/3d251c3e](https://us.posthog.com/project/2/insights/3d251c3e) + +## 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 `PUBLIC_POSTHOG_PROJECT_TOKEN` and `PUBLIC_POSTHOG_HOST` 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.client.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.client.ts new file mode 100644 index 00000000..1af9e54a --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.client.ts @@ -0,0 +1,17 @@ +import posthog from "posthog-js" +import { PUBLIC_POSTHOG_PROJECT_TOKEN } from "$env/static/public" +import type { HandleClientError } from "@sveltejs/kit" + +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, + }) +} + +export const handleError: HandleClientError = async ({ error, message }) => { + posthog.captureException(error) + return { message } +} diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.server.ts b/apps/basic-integration/sveltekit/CMSaasStarter/src/hooks.server.ts index 166bff0d..9edc7770 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,65 @@ 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") || 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) +} + +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 } +} + +export const handle: Handle = sequence(supabase, authGuard, posthogProxy) 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 00000000..80092767 --- /dev/null +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/lib/server/posthog.ts @@ -0,0 +1,24 @@ +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() + } +} 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 0ed47a19..df1a5894 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,14 @@ 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, } @@ -71,6 +80,13 @@ export const actions = { }) } + const posthog = getPostHogClient() + posthog.capture({ + distinctId: session.user.id, + event: "email_change_initiated", + }) + await posthog.flush() + return { email, } @@ -172,6 +188,13 @@ export const actions = { }) } + const posthog = getPostHogClient() + posthog.capture({ + distinctId: session.user.id, + event: "password_changed", + }) + await posthog.flush() + return { newPassword1, newPassword2, @@ -221,6 +244,13 @@ export const actions = { }) } + const posthog = getPostHogClient() + posthog.capture({ + distinctId: user.id, + event: "account_deleted", + }) + await posthog.flush() + await supabase.auth.signOut() redirect(303, "/") }, @@ -303,7 +333,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() if (newProfile) { + posthog.capture({ + distinctId: user.id, + event: "profile_created", + properties: { + company_name: companyName, + has_website: !!website, + }, + }) + await sendAdminEmail({ subject: "Profile Created", body: `Profile created by ${session.user.email}\nFull name: ${fullName}\nCompany name: ${companyName}\nWebsite: ${website}`, @@ -320,7 +361,13 @@ export const actions = { WebsiteBaseUrl: WebsiteBaseUrl, }, }) + } else { + posthog.capture({ + distinctId: user.id, + event: "profile_updated", + }) } + await posthog.flush() return { fullName, diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/sign_out/+page.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/sign_out/+page.svelte index 7f8e876e..c2082195 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/sign_out/+page.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(admin)/account/sign_out/+page.svelte @@ -1,18 +1,20 @@ diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/pricing/pricing_module.svelte b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/pricing/pricing_module.svelte index 1c9599e5..97a87bf9 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/pricing/pricing_module.svelte +++ b/apps/basic-integration/sveltekit/CMSaasStarter/src/routes/(marketing)/pricing/pricing_module.svelte @@ -1,5 +1,6 @@
- {#each pricingPlans as plan} + {#each pricingPlans as plan (plan.id)}
Plan Includes:
    - {#each plan.features as feature} + {#each plan.features as feature (feature)}
  • {feature}
  • {/each}
      @@ -57,6 +70,7 @@ href={"/account/subscribe/" + (plan?.stripe_price_id ?? "free_plan")} class="btn btn-primary w-[80%] mx-auto" + onclick={() => handlePlanSelected(plan)} > {callToAction} diff --git a/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js b/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js index d33de453..7015c447 100644 --- a/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js +++ b/apps/basic-integration/sveltekit/CMSaasStarter/svelte.config.js @@ -11,6 +11,10 @@ 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, + // Required for PostHog session replay to work correctly with SSR + paths: { + relative: false, + }, }, preprocess: vitePreprocess(), }