Observability (PostHog + Sentry)

ShipSaaS ships an opt-in shim for analytics and error tracking. By default nothing is sent anywhere — the shim is a no-op. You turn it on by installing the package you want and setting the matching environment variable.

This keeps the default install clean (no large SDK trees, no extra audit surface) and gives you a one-line upgrade path.

What you get out of the box

  • src/lib/observability.ts — PostHog + Sentry facade with trackEventServer, trackEventClient, identifyUserClient, resetClient, and captureError.
  • src/providers/ObservabilityProvider.tsx — a client provider that captures pageviews on route change and identifies signed-in users.
  • src/app/error.tsx and src/app/global-error.tsx — error boundaries that automatically report to Sentry (if configured).
  • src/app/not-found.tsx — branded 404 page.
  • The API error helper handleApiError() in src/lib/error-handler.ts now reports 5xx errors to Sentry automatically.

Enabling PostHog (analytics)

  1. Create a project at posthog.com and copy the project key.
  2. Install the SDKs:
    npm install posthog-js posthog-node
  3. Add the env vars to .env.local:
    NEXT_PUBLIC_POSTHOG_KEY=phc_...
    NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
  4. Restart the dev server. That's it — pageviews will start flowing, and the ObservabilityProvider will identify users on sign-in and reset on sign-out.

Tracking custom events

// server-side (e.g. webhook, API route)
import { trackEventServer } from "@/lib/observability";

await trackEventServer("user_signed_up", { plan: "pro" }, userId);

// client-side (e.g. onClick)
import { trackEventClient } from "@/lib/observability";

trackEventClient("pricing_cta_clicked", { plan: "pro" });

Both calls are no-ops if the package isn't installed or the key isn't set, so they're safe to leave in your code.

Enabling Sentry (errors)

  1. Create a project at sentry.io and copy the DSN.
  2. Install the Next.js SDK:
    npm install @sentry/nextjs
    npx @sentry/wizard@latest -i nextjs

    The wizard wires up sentry.client.config.ts, sentry.server.config.ts, sentry.edge.config.ts, and the source-map upload in your build.

  3. Add the DSNs to .env.local:
    SENTRY_DSN=https://...@sentry.io/...
    NEXT_PUBLIC_SENTRY_DSN=https://...@sentry.io/...
  4. Restart the dev server. API 5xx errors and app/error.tsx / global-error.tsx exceptions will start landing in your Sentry project.

Capturing errors manually

import { captureError } from "@/lib/observability";

try {
  await somethingRisky();
} catch (err) {
  await captureError(err, {
    tags: { feature: "billing" },
    extras: { userId, planId },
  });
  throw err;
}

Privacy

  • PostHog is configured with person_profiles: "identified_only", so anonymous visitors are not person-profiled.
  • The server logger (src/lib/logger.ts) already redacts email, password, token, apiKey, secret, authorization, and cookie keys. These are also redacted before any analytics event is sent.
  • The shim swallows its own errors. Observability will never break a request.

Disabling

Remove the env var (or set it to empty) and uninstall the package. The shim goes back to no-op, and no data is sent.