Skip to content

feat: add PostHog analytics integration - #17

Draft
posthog-eu[bot] wants to merge 2 commits into
mainfrom
posthog/instrumentation-f9984f
Draft

feat: add PostHog analytics integration#17
posthog-eu[bot] wants to merge 2 commits into
mainfrom
posthog/instrumentation-f9984f

Conversation

@posthog-eu

@posthog-eu posthog-eu Bot commented Aug 2, 2026

Copy link
Copy Markdown

Summary

This PR adds PostHog product analytics to the web viewer (packages/web, a Vite app) using the posthog-js SDK.

  • packages/web/src/posthog.ts — new PostHog client initialization, reading the token/host from VITE_PUBLIC_POSTHOG_KEY / VITE_PUBLIC_POSTHOG_HOST, with browser exception autocapture enabled (capture_unhandled_errors, capture_unhandled_rejections).
  • packages/web/src/main.ts — captures command_copied when a copy-command button is used.
  • packages/web/src/viewer/app.js — captures note_opened (with open_mode), note_closed, and search_result_opened at the documentation viewer's interaction handlers.
  • packages/web/package.json, pnpm-lock.yaml — adds the posthog-js dependency.
  • packages/cli/cmd/openknowledge/viewer_assets/viewer.js — regenerated bundled viewer asset reflecting the above (produced by the existing build:viewer script).
  • pnpm-workspace.yaml — minor pnpm build-approval bookkeeping picked up while installing the new dependency.

The app is unauthenticated, so no persistent user identification was added — only anonymous, personless event capture.

Insights and dashboards created

Type Name Link
Dashboard Your starter dashboard https://eu.posthog.com/project/238777/dashboard/867438
Dashboard Analytics basics (wizard) https://eu.posthog.com/project/238777/dashboard/867457
Insight Active users (last 30 days) https://eu.posthog.com/project/238777/insights/fmmlg8BP
Insight Sessions (last 7 days) https://eu.posthog.com/project/238777/insights/yyoo8VJT
Insight Pageviews (last 7 days) https://eu.posthog.com/project/238777/insights/FZbm0VVs
Insight Daily active users (DAUs) https://eu.posthog.com/project/238777/insights/z09IBCE5
Insight Weekly active users (WAUs) https://eu.posthog.com/project/238777/insights/SPelKZq3
Insight Retention https://eu.posthog.com/project/238777/insights/mSZlzzni
Insight Top referrers https://eu.posthog.com/project/238777/insights/icFgMOUk
Insight Visit to interaction funnel https://eu.posthog.com/project/238777/insights/dC58VYO6
Insight Documentation engagement events (wizard) https://eu.posthog.com/project/238777/insights/X313dhOU
Insight Note opens by mode (wizard) https://eu.posthog.com/project/238777/insights/ENXUTg3C
Insight Search-to-note journey (wizard) https://eu.posthog.com/project/238777/insights/tI1VnBmO

How to verify

  1. Deploy this branch (or run pnpm dev / pnpm build && pnpm start locally — the env vars from the section below are already committed).
  2. Open the docs viewer and click around: copy a command snippet, open a note, use search and click a result, close a note panel.
  3. In PostHog, open Activity — you should see command_copied, note_opened, note_closed, and search_result_opened events arriving within a minute.
  4. Check the Your starter dashboard and Analytics basics (wizard) dashboards for data.

Environment variables

Production is deployed to Railway as a Docker image (Dockerfile + railway.json), and the build runs pnpm build:web (which invokes vite build) directly inside the Docker build stage via COPY . . — Railway dashboard variables are not passed into that build step. Because Vite bakes VITE_* variables in at build time, the required variables were committed directly to packages/web/.env (also mirrored in .env.example) in a separate commit on this branch, so PostHog will start working automatically as soon as this branch is deployed to production. No manual action is needed.

  • VITE_PUBLIC_POSTHOG_KEY — the project API token
  • VITE_PUBLIC_POSTHOG_HOSThttps://eu.i.posthog.com

Created with PostHog Code

posthog-eu Bot added 2 commits August 2, 2026 08:16
Generated-By: PostHog Code
Task-Id: 51fb10f1-b921-420e-909f-d184ae1d34c5
Generated-By: PostHog Code
Task-Id: 51fb10f1-b921-420e-909f-d184ae1d34c5
@posthog-eu

posthog-eu Bot commented Aug 2, 2026

Copy link
Copy Markdown
Author

CI status

Three checks are red on this PR, but all three already fail on main at the base commit (3152cea) and are unrelated to the PostHog integration added here:

  • Dependency vulnerabilities — fails on a pre-existing critical CVE in the vitest@3.2.4 devDependency (GHSA-5xrq-8626-4rwp, fixed in 3.2.6). vitest was not touched by this PR.
  • Go tests (macos-latest) — fails on TestRunConnectAndRefreshPreserveGitRefAndSubdir, a CLI git-ref/subdir test unrelated to any file changed here.
  • Go tests (windows-latest) — same pre-existing failure pattern as macOS, unrelated to this PR's changes.

verify, Go tests (ubuntu-latest), Node 18 compatibility, and all CodeQL checks pass.

Since these three failures predate this branch and aren't caused by the PostHog changes, I've left them as-is per this task's scope (integration + prod env vars + minimal CI fixes only).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants