Skip to content

chore: route preview deployments to staging PostHog key - #830

Merged
malhotra5 merged 2 commits into
mainfrom
chore/posthog-staging-preview-key
Sep 23, 2026
Merged

malhotra5 merged 2 commits into
mainfrom
chore/posthog-staging-preview-key

Conversation

@malhotra5

@malhotra5 malhotra5 commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

This PR was created by an AI agent (OpenHands) on behalf of the maintainer.

Problem

openhands/posthog-init.js contained a single hardcoded PostHog project key. Because Mintlify auto-includes every .js file in the content directory on every page, that key ran identically on:

  • Production — the custom domain docs.openhands.dev
  • Preview deployments — the per-PR *.mintlify.site / *.mintlify.app URLs Mintlify generates automatically

So all preview/PR traffic was being captured into the production PostHog project, polluting prod analytics. Unlike the company-website repo (which uses Vercel env vars scoped per environment), the docs repo has no build-time env-var layer available in its Mintlify deploy model.

Solution

Select the PostHog project key at runtime based on window.location.hostname:

Hostname Key used
docs.openhands.dev (prod custom domain) PROD_KEY (existing production key)
*.mintlify.site / *.mintlify.app (PR previews) STAGING_KEY
localhost / 127.0.0.1 / 0.0.0.0 (local dev) STAGING_KEY
any other unknown host PROD_KEY (safe default)

The api_host (https://z.openhands.dev), ui_host, defaults, and person_profiles config are unchanged for both environments.

Why hostname detection instead of env vars / Mintlify native integration

  • No env-var layer: the docs repo deploys via Mintlify-on-push-to-main with no per-preview env-var injection; no .env files or build templating exist.
  • Mintlify native integrations.posthog.apiKey in docs.json was considered but rejected: it routes through Mintlify's proxy (ph.mintlify.com), uses a single static key, and auto-opts-out of capturing on previews — giving "no analytics on previews" rather than "staging analytics on previews".
  • Mintlify's preview URL scheme is documented and non-configurable (<branch>-<subdomain>.mintlify.site), so hostname is a reliable runtime differentiator.

Verification of routing logic

Extracted the isStagingHostname logic and tested all branches:

PASS | docs.openhands.dev                      -> PROD    (production custom domain)
PASS | feature-foo-bar.openhands.mintlify.site -> STAGING (PR preview mintlify.site)
PASS | openhands.mintlify.app                  -> STAGING (preview mintlify.app)
PASS | localhost                               -> STAGING (local dev)
PASS | 127.0.0.1                               -> STAGING (local dev ip)
PASS | 0.0.0.0                                 -> STAGING (local dev ip)
PASS | some-unknown-host.example.com           -> PROD    (unknown host -> prod safe default)

File also passes node --check.

Confirmed that staging posthog is receiving events as expected.

image

How to verify after merge

  1. Production (docs.openhands.dev): open DevTools → Network, filter posthog; confirm decide/batch requests still go to https://z.openhands.dev and events land in the production PostHog project.
  2. Preview (open the Mintlify preview URL from a PR bot comment, host *.mintlify.site):
    • With placeholder still in place: you should see the console warning and no PostHog network requests.
    • After replacing the placeholder: PostHog requests go to https://z.openhands.dev but events land in the staging PostHog project.
  3. The docs site uses PostHog autocapture (the posthog.init snippet autoloads array.js), so look for $pageview and $autocapture click events — there are no custom named events in docs (unlike company-website's homepage_* events).

Notes / follow-ups

  • If more production hostnames are added later, extend the PRODUCTION_HOSTNAMES array.
  • This keeps the reo-init.js sibling script untouched (separate Reo.dev tracking, out of scope).

@malhotra5 can click here to continue refining the PR

Mintlify auto-includes every .js file in the content directory on every
page, so posthog-init.js previously ran with a single hardcoded key on
both production and PR preview deployments. Select the PostHog project
key at runtime based on hostname: docs.openhands.dev uses the production
key, while *.mintlify.site / *.mintlify.app previews and localhost use a
staging key. The staging key is a placeholder until the real value is
dropped in; until then previews warn in the console and skip init.

Co-authored-by: openhands <openhands@all-hands.dev>
@mintlify

mintlify Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
all-hands-ai 🟢 Ready View Preview Sep 23, 2026, 6:19 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

Replace the staging key placeholder with the real staging project key so
PR preview deployments report to the staging PostHog project. Removes the
now-dead placeholder guard that skipped init when the placeholder was
present.

Co-authored-by: openhands <openhands@all-hands.dev>

@DevinVinson DevinVinson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@malhotra5
malhotra5 merged commit 1a327e7 into main Sep 23, 2026
5 checks passed
@malhotra5
malhotra5 deleted the chore/posthog-staging-preview-key branch September 23, 2026 19:34

This branch was successfully deployed

1 active deployment
staging — 544b17ec Deployed Sep 23, 2026 by mintlify[bot]
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.

3 participants