Product analytics (PostHog)
ContentsVision uses PostHog for product analytics: who is doing what, how often, and where they get stuck. It is captured from two sides at once, the browser and the backend, sharing one identity model so an event raised on the server and an event raised in the browser land on the same person and the same team.
It is off by default. With no key configured the backend's wrapper no-ops and the frontend ships with no tracking, so neither touches the network. This keeps local development and the test suite silent and offline. Turn it on by setting the keys in configuration.
Identity model
Both sides key on the local database IDs so events line up:
| Concept | Value | Frontend | Backend |
|---|---|---|---|
Person (distinct_id) | User.id (UUID) | posthog.identify(user.id, …) | analytics.capture(distinct_id=user_id, …) |
| Team group | Team.id (UUID) | posthog.group('team', user.team_id) | groups={'team': team_id} |
Because the same UUIDs are used on both sides, a team's server events (uploads, job completions) and its browser events (page views, clicks) aggregate together under one group, and a user's whole journey is one timeline.
Backend capture
The backend wrapper is app/core/analytics.py. It is deliberately small and fail-safe: every function is a no-op when POSTHOG_API_KEY is unset, the PostHog SDK is imported lazily (so it need not even be installed when analytics is off), and a capture failure is swallowed and logged, never raised. This mirrors the activity log rule that a logging failure must not break the user's real action.
Every user action (the activity-log hook)
The single highest-leverage hook is in services/activity.py. The app already funnels every meaningful mutation through one log_activity() call; analytics piggybacks on it, so one capture there covers them all:
sign-in / sign-out, create / update / delete claim, claim status changes, reassigning a claim, claim tags, submit claim to Adjust Square or Contents Estimation, create / rename / reorder / archive room, upload photos, add / edit / move / delete item, bulk edit, auto-approve, merge, add / edit / move / remove item photo, insured link and integration events, settings, prompt, and hierarchy config changes, saved views, tags, access profiles, dashboard defaults, training events, restore (undo), and admin impersonation.
The action_type becomes the event name; the human label, target, affected entity, and restorable flag ride along as properties, and the actor's name is set on the person profile. The capture sits outside the row write's try, so it still fires if the activity row itself failed to save.
Job outcomes (the pipeline hook)
Pipeline completion happens asynchronously, after the request that started it is long gone, so it cannot go through log_activity. Instead services/pipeline.py calls _track_job_outcome() at every finalize point (video, photo, grouped-photo, and audio pipelines, on both success and failure). It resolves the owning team and the uploader from the job's claim and captures:
| Event | When | Key properties |
|---|---|---|
job_processing_completed | A pipeline finishes successfully | source_type, status, items_found, rooms_detected, duration_seconds, billed_credits, billed_blocks, billed_minutes, photo_request_count, claim_number, job_id |
job_processing_failed | A pipeline raises | the same, plus a truncated error_message (first 300 characters) |
The event's distinct_id is the user who created the claim, falling back to the team (then the job id) for claims that predate creator tracking, so an event is never dropped for lack of an id. When analytics is off, _track_job_outcome() skips the identity lookup entirely (it still persists the job's AI spend and archives its logs, which are not analytics).
These are server truth: they fire whether or not anyone has the app open to watch.
The same finalize points also fire the optional media-processing Slack notification (slack_notifier.notify_job_processed()), which is off unless SLACK_WEBHOOK_URL is set. Both hooks are best-effort and never raise into the pipeline.
Shutdown
The FastAPI lifespan calls analytics.shutdown() on exit, which flushes any queued events before the process stops.
Frontend capture
components/PostHogProvider.tsx is mounted once in the root layout, inside the auth provider. It initialises PostHog (lib/posthog.ts) and wires three things:
- Page views. App Router does not reload the page on navigation, so automatic pageview is disabled and a
$pageviewis sent manually on every route change (under aSuspenseboundary, asuseSearchParamsrequires). Page leaves are captured automatically (capture_pageleave). - Autocapture. Clicks, form submits, and input changes are captured automatically (metadata only, never input values).
- Identity. On login the user is identified and joined to their team group; on logout the client is reset so the next person starts fresh. While an admin is impersonating someone, every event is flagged with
impersonating: trueso those sessions can be excluded from real usage metrics (along withimpersonator_email).
The same identity bridge also sets the Sentry user on login and clears it on logout (ids only: user and team UUIDs), when NEXT_PUBLIC_SENTRY_DSN is set.
A thin lib/analytics.ts track() helper adds a few client-only events that have no server equivalent: buy_credits_click (an external link), inventory_exported (a direct file download), job_processing_confirmed / job_processing_cancelled (the credit-confirmation decision), and session_expired (an API call returned 401 and lib/session.ts signed the user out; a 401 is a handled response, so no error hook sees it), and access_off (the backend refused the person because their ContentsVision product access was switched off; carries reason). Every track() event carries source: 'frontend', and backend events carry source: 'backend'. Everything else is left to autocapture and the backend hooks, so events are not double-counted.
Session replay and PII
Claims carry personal data (insured names, addresses, claim numbers), so session replay is configured to mask all inputs by default. Two further controls:
- Add the
ph-no-captureclass to any element to exclude it from both replay and autocapture. - To mask all on-screen text (not just inputs), set
maskTextSelector: '*'inlib/posthog.ts. It is off by default because it makes replays much less useful; turn it on if your compliance posture requires it.
Person profiles are created for identified users only, so the anonymous login redirect does not spawn person records.
Turning it on
- Create a project in PostHog (or self-host) and copy its Project API Key (
phc_…). - Set
POSTHOG_API_KEY(backend) andNEXT_PUBLIC_POSTHOG_KEY(frontend) to that same key, plus the matching*_HOSTif you are not on US Cloud. See configuration. - Rebuild the frontend (the
NEXT_PUBLIC_*keys are build-time) and restart the backend.
To turn it back off, clear the keys and rebuild / restart.