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 withtrackEventServer,trackEventClient,identifyUserClient,resetClient, andcaptureError.src/providers/ObservabilityProvider.tsx— a client provider that captures pageviews on route change and identifies signed-in users.src/app/error.tsxandsrc/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()insrc/lib/error-handler.tsnow reports 5xx errors to Sentry automatically.
Enabling PostHog (analytics)
- Create a project at posthog.com and copy the project key.
- Install the SDKs:
npm install posthog-js posthog-node
- Add the env vars to
.env.local:NEXT_PUBLIC_POSTHOG_KEY=phc_... NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
- Restart the dev server. That's it — pageviews will start flowing, and the
ObservabilityProviderwill 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)
- Create a project at sentry.io and copy the DSN.
- 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. - Add the DSNs to
.env.local:SENTRY_DSN=https://...@sentry.io/... NEXT_PUBLIC_SENTRY_DSN=https://...@sentry.io/...
- Restart the dev server. API 5xx errors and
app/error.tsx/global-error.tsxexceptions 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 redactsemail,password,token,apiKey,secret,authorization, andcookiekeys. 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.