feat(web): track product analytics with posthog #3

Open
kennethz wants to merge 1 commit from feat/web-posthog-events into hotfix
Member

Summary

  • Moves PostHog setup into apps/web/src/lib/analytics.ts. It initializes only when a key is configured, so local development and previews send nothing.
    • The key and host come from build-time VITE_PUBLIC_POSTHOG_* first, then from kennel's runtime /_kennel/env.json (PUBLIC_POSTHOG_KEY, PUBLIC_POSTHOG_HOST).
    • The host defaults to https://v.scottylabs.org, the reverse proxy governance sets up. ui_host is set so the toolbar works.
  • Fixes anonymous visitor tracking. posthog.reset() used to run whenever no user was loaded, so signed-out visitors got a new distinct ID on every page load. Users are now identified once the session loads, and the ID is reset only after an identified user signs out.
  • Adds typed custom events:
    • search_result_selected
    • login_prompted
    • navigation_started
    • navigation_ended
    • location_shared
    • map_view_changed
  • Page views fire on pathname changes only, so viewport query param updates aren't counted.
  • Adds docs/src/Analytics.md.

Testing

  • bun run check passes.
  • Ran built bundles in headless Chromium with PostHog traffic stubbed:
    • A build-time key and a runtime /_kennel/env.json key both initialize against v.scottylabs.org.
    • Without a key, no requests are made.
    • Query-only replaceState adds no page views.
    • The anonymous ID stays the same across reloads. The previous code changed it on every reload.
    • map_view_changed arrives with its mode property.
  • Search, navigation, share and login events need the API and database, so they are type-checked only.

Notes

  • No preview site: devenv.nix sets kennel.previewDeployments = false and doesn't define a deployable web site yet.
  • For production on kennel, the key has to reach the browser through [scopes.public], and kennel requires those names to start with PUBLIC_. Governance writes POSTHOG_KEY / POSTHOG_HOST to maps/prod (its docs say PUBLIC_POSTHOG_*), so that naming needs fixing on the governance side before production analytics work.
## Summary - Moves PostHog setup into `apps/web/src/lib/analytics.ts`. It initializes only when a key is configured, so local development and previews send nothing. - The key and host come from build-time `VITE_PUBLIC_POSTHOG_*` first, then from kennel's runtime `/_kennel/env.json` (`PUBLIC_POSTHOG_KEY`, `PUBLIC_POSTHOG_HOST`). - The host defaults to `https://v.scottylabs.org`, the reverse proxy governance sets up. `ui_host` is set so the toolbar works. - Fixes anonymous visitor tracking. `posthog.reset()` used to run whenever no user was loaded, so signed-out visitors got a new distinct ID on every page load. Users are now identified once the session loads, and the ID is reset only after an identified user signs out. - Adds typed custom events: - `search_result_selected` - `login_prompted` - `navigation_started` - `navigation_ended` - `location_shared` - `map_view_changed` - Page views fire on pathname changes only, so viewport query param updates aren't counted. - Adds `docs/src/Analytics.md`. ## Testing - `bun run check` passes. - Ran built bundles in headless Chromium with PostHog traffic stubbed: - A build-time key and a runtime `/_kennel/env.json` key both initialize against `v.scottylabs.org`. - Without a key, no requests are made. - Query-only `replaceState` adds no page views. - The anonymous ID stays the same across reloads. The previous code changed it on every reload. - `map_view_changed` arrives with its `mode` property. - Search, navigation, share and login events need the API and database, so they are type-checked only. ## Notes - No preview site: `devenv.nix` sets `kennel.previewDeployments = false` and doesn't define a deployable web site yet. - For production on kennel, the key has to reach the browser through `[scopes.public]`, and kennel requires those names to start with `PUBLIC_`. Governance writes `POSTHOG_KEY` / `POSTHOG_HOST` to `maps/prod` (its docs say `PUBLIC_POSTHOG_*`), so that naming needs fixing on the governance side before production analytics work.
feat(web): track product analytics with posthog
Some checks failed
Development launcher / launcher (macos-latest) (pull_request) Has been cancelled
Development launcher / launcher (ubuntu-latest) (pull_request) Has been cancelled
Development launcher / launcher (windows-latest) (pull_request) Has been cancelled
Development launcher / native-services (macos-latest) (pull_request) Has been cancelled
Development launcher / native-services (ubuntu-latest) (pull_request) Has been cancelled
nixbot/nix-eval evaluation failed
nixbot/nix-build build failed
kennel/build skipped: no services or static sites defined
16fa972140
Move PostHog setup into lib/analytics.ts. It initializes only when a key
is configured, reading the build-time VITE_PUBLIC_POSTHOG_* values first
and kennel's runtime /_kennel/env.json second, and defaults the host to
the ScottyLabs reverse proxy with ui_host set for the toolbar.

Identify users only after the session loads and reset only after an
identified user signs out. Previously reset() ran on every load for
signed-out visitors, giving them a new anonymous ID each time.

Capture search selections, login prompts, navigation start and end,
link shares, and map view changes, and document them in docs/.
Some checks failed
Development launcher / launcher (macos-latest) (pull_request) Has been cancelled
Development launcher / launcher (ubuntu-latest) (pull_request) Has been cancelled
Development launcher / launcher (windows-latest) (pull_request) Has been cancelled
Development launcher / native-services (macos-latest) (pull_request) Has been cancelled
Development launcher / native-services (ubuntu-latest) (pull_request) Has been cancelled
nixbot/nix-eval evaluation failed
nixbot/nix-build build failed
kennel/build skipped: no services or static sites defined
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feat/web-posthog-events:feat/web-posthog-events
git switch feat/web-posthog-events

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch hotfix
git merge --no-ff feat/web-posthog-events
git switch feat/web-posthog-events
git rebase hotfix
git switch hotfix
git merge --ff-only feat/web-posthog-events
git switch feat/web-posthog-events
git rebase hotfix
git switch hotfix
git merge --no-ff feat/web-posthog-events
git switch hotfix
git merge --squash feat/web-posthog-events
git switch hotfix
git merge --ff-only feat/web-posthog-events
git switch hotfix
git merge feat/web-posthog-events
git push origin hotfix
Sign in to join this conversation.
No description provided.