Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: prevent snapshot CI runs from sending PostHog analytics events (#490)

Rohit Malhotrad886a68a431a
diff --git a/.env.sample b/.env.sample
index a073e58538..7ff3196ad6 100644
--- a/.env.sample
+++ b/.env.sample
@@ -16,6 +16,11 @@ VITE_FRONTEND_PORT="3001" # Port to run the frontend application
VITE_USE_TLS="false" # Use HTTPS/WSS for proxied backend connections
VITE_INSECURE_SKIP_VERIFY="false" # Skip TLS certificate verification for proxied backend requests
+# Deployment environment β€” controls which PostHog project key is compiled in.
+# Set to "production" or "staging" only in real deployment build pipelines.
+# Leave unset locally so developers never accidentally send telemetry.
+# VITE_APP_ENV="production"
+
# Mocking / test helpers
VITE_MOCK_API="false" # Enable/disable API mocking with MSW
# VITE_GITHUB_TOKEN="" # GitHub token for repository access (used in some tests)
diff --git a/package.json b/package.json
index 130b4ccf75..60f97ef392 100644
--- a/package.json
+++ b/package.json
@@ -96,7 +96,7 @@
"typecheck:staged": "react-router typegen && npx tsc --noEmit --skipLibCheck",
"check-translation-completeness": "node scripts/check-translation-completeness.cjs",
"build:app": "npm run make-i18n && react-router build",
- "build:lib": "npm run make-i18n && react-router typegen && cross-env BUILD_LIB=true vite build && tsc -p tsconfig.lib.json"
+ "build:lib": "npm run make-i18n && react-router typegen && cross-env BUILD_LIB=true VITE_APP_ENV=production vite build && tsc -p tsconfig.lib.json"
},
"lint-staged": {
"src/**/*.{ts,tsx,js}": [
diff --git a/src/mocks/analytics-handlers.ts b/src/mocks/analytics-handlers.ts
index 09b3ac0c60..4b59d65887 100644
--- a/src/mocks/analytics-handlers.ts
+++ b/src/mocks/analytics-handlers.ts
@@ -1,7 +1,13 @@
import { http, HttpResponse } from "msw";
+// Block both the direct PostHog ingestion endpoint and the OpenHands reverse
+// proxy (z.openhands.dev) used by the library telemetry service so mock-mode
+// builds and snapshot test runs never send analytics events to PostHog.
export const ANALYTICS_HANDLERS = [
http.post("https://us.i.posthog.com/e", async () =>
HttpResponse.json(null, { status: 200 }),
),
+ http.post("https://z.openhands.dev/*", async () =>
+ HttpResponse.json(null, { status: 200 }),
+ ),
];
diff --git a/src/services/telemetry.ts b/src/services/telemetry.ts
index be208df698..3194ae6326 100644
--- a/src/services/telemetry.ts
+++ b/src/services/telemetry.ts
@@ -32,12 +32,20 @@ const TELEMETRY_CONSENT_KEY = "openhands-telemetry-consent";
const TELEMETRY_FIRST_USE_KEY = "openhands-telemetry-first-use";
const TELEMETRY_SESSION_KEY = "openhands-telemetry-session";
-// PostHog configuration - configurable via env vars with OpenHands defaults
-// Note: The default API key sends telemetry to OpenHands' PostHog project.
-// Library consumers can override this with their own PostHog project key.
-const POSTHOG_API_KEY =
- import.meta.env.VITE_POSTHOG_API_KEY ||
- "phc_BgzfxKdgsYMLFTmJqt424ZoyVHvKFfrwttLimzdYTKFK";
+// PostHog project keys β€” one per deployment environment, hardcoded so they
+// are baked into the static bundle at build time and cannot drift at runtime.
+// Replace POSTHOG_STAGING_KEY with a dedicated project key once provisioned.
+const POSTHOG_PROD_KEY = "phc_BgzfxKdgsYMLFTmJqt424ZoyVHvKFfrwttLimzdYTKFK";
+const POSTHOG_STAGING_KEY = "phc_kBtz5nKmxVRRQ7HtPwr2QX9eMC5j65zE86QKocVNwb4U";
+
+// Always use the staging key unless VITE_APP_ENV is explicitly set to
+// "production" at bundle time (hardcoded in build:lib and production CI).
+// Library consumers can always override with VITE_POSTHOG_API_KEY.
+const POSTHOG_API_KEY: string =
+ (import.meta.env.VITE_POSTHOG_API_KEY as string | undefined) ||
+ (import.meta.env.VITE_APP_ENV === "production"
+ ? POSTHOG_PROD_KEY
+ : POSTHOG_STAGING_KEY);
// Default to OpenHands' reverse proxy to bypass ad blockers.
// The proxy at z.openhands.dev routes to PostHog's US region.