Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(frontend): keep local dev exceptions out of posthog cloud (#127003)

posthog[bot]027a75e0e95c
diff --git a/frontend/src/loadPostHogJS.test.ts b/frontend/src/loadPostHogJS.test.ts
index 15b28057f31d..2e3161170a92 100644
--- a/frontend/src/loadPostHogJS.test.ts
+++ b/frontend/src/loadPostHogJS.test.ts
@@ -1,4 +1,4 @@
-import posthog from 'posthog-js'
+import posthog, { CaptureResult, PostHogConfig } from 'posthog-js'
import { AppContext } from '~/types'
@@ -70,17 +70,7 @@ describe('loadPostHogJS', () => {
})
})
- it.each([
- ['hobby with self-capture', { run_mode: 'HOBBY' }, true, false],
- ['hobby with Cloud assets', { run_mode: 'HOBBY' }, false, undefined],
- ['US cloud', { run_mode: 'US' }, false, undefined],
- ['EU cloud', { run_mode: 'EU' }, false, undefined],
- ['cloud development', { run_mode: 'DEV' }, false, undefined],
- ['local development', { run_mode: 'LOCAL' }, false, undefined],
- ['E2E', { run_mode: 'E2E' }, false, undefined],
- ['older app context', {}, false, undefined],
- ['missing app context', undefined, false, undefined],
- ] as const)('selects script versioning for %s', (_, context, selfCapture, expected) => {
+ function initConfigFor(context: Partial<AppContext> | undefined, selfCapture: boolean): Partial<PostHogConfig> {
const original = {
key: window.JS_POSTHOG_API_KEY,
host: window.JS_POSTHOG_HOST,
@@ -95,17 +85,48 @@ describe('loadPostHogJS', () => {
jest.mocked(posthog.init).mockClear()
try {
loadPostHogJS()
- const config = jest.mocked(posthog.init).mock.calls[0][1]
- if (expected === undefined) {
- expect(config).not.toHaveProperty('strict_script_versioning')
- } else {
- expect(config).toHaveProperty('strict_script_versioning', expected)
- }
+ return jest.mocked(posthog.init).mock.calls[0][1] ?? {}
} finally {
window.JS_POSTHOG_API_KEY = original.key
window.JS_POSTHOG_HOST = original.host
window.JS_POSTHOG_SELF_CAPTURE = original.selfCapture
window.POSTHOG_APP_CONTEXT = original.context
}
+ }
+
+ it.each([
+ ['hobby with self-capture', { run_mode: 'HOBBY' }, true, false],
+ ['hobby with Cloud assets', { run_mode: 'HOBBY' }, false, undefined],
+ ['US cloud', { run_mode: 'US' }, false, undefined],
+ ['EU cloud', { run_mode: 'EU' }, false, undefined],
+ ['cloud development', { run_mode: 'DEV' }, false, undefined],
+ ['local development', { run_mode: 'LOCAL' }, false, undefined],
+ ['E2E', { run_mode: 'E2E' }, false, undefined],
+ ['older app context', {}, false, undefined],
+ ['missing app context', undefined, false, undefined],
+ ] as const)('selects script versioning for %s', (_, context, selfCapture, expected) => {
+ const config = initConfigFor(context, selfCapture)
+ if (expected === undefined) {
+ expect(config).not.toHaveProperty('strict_script_versioning')
+ } else {
+ expect(config).toHaveProperty('strict_script_versioning', expected)
+ }
+ })
+
+ it.each([
+ ['local development sending to Cloud', { run_mode: 'LOCAL' }, false, false],
+ ['local development with self-capture', { run_mode: 'LOCAL' }, true, true],
+ ['US cloud', { run_mode: 'US' }, false, true],
+ ['hobby with Cloud assets', { run_mode: 'HOBBY' }, false, true],
+ ] as const)('filters exceptions for %s', (_, context, selfCapture, sent) => {
+ const beforeSend = [initConfigFor(context, selfCapture).before_send ?? []].flat()
+ const exception = { uuid: 'example', event: '$exception', properties: {} } as CaptureResult
+ const pageview = { ...exception, event: '$pageview' }
+
+ const runFilters = (event: CaptureResult): CaptureResult | null =>
+ beforeSend.reduce<CaptureResult | null>((current, filter) => (current ? filter(current) : null), event)
+
+ expect(runFilters(exception) !== null).toBe(sent)
+ expect(runFilters(pageview)).not.toBeNull()
})
})
diff --git a/frontend/src/loadPostHogJS.tsx b/frontend/src/loadPostHogJS.tsx
index 99b3f3c69f67..bc0eb4df91a0 100644
--- a/frontend/src/loadPostHogJS.tsx
+++ b/frontend/src/loadPostHogJS.tsx
@@ -58,6 +58,17 @@ export function withLastSeenFeatureFlags(
const stampEmbeddedPageFrame: BeforeSendFn = (event) =>
event && { ...event, properties: { ...event.properties, embedded_page_frame: true } }
+const dropExceptions: BeforeSendFn = (event) => (event?.event === '$exception' ? null : event)
+
+/**
+ * Local development without self-capture sends its events to PostHog Cloud. Its crashes come from
+ * dev-only state, for example a stale Vite prebundle, and file issues that no user can hit.
+ * This reads the raw context because getAppContext() hides it in OAuth mode, which also runs locally.
+ */
+function sendsLocalDevelopmentToCloud(): boolean {
+ return window.POSTHOG_APP_CONTEXT?.run_mode === 'LOCAL' && !window.JS_POSTHOG_SELF_CAPTURE
+}
+
function readLastSeenFeatureFlags(): LastSeenFeatureFlags | null {
try {
const stored = window.localStorage.getItem(LAST_SEEN_FEATURE_FLAGS_KEY)
@@ -141,6 +152,7 @@ export function loadPostHogJS(): void {
// A page in a frame counts its own pageviews, so its events say so and analysis can filter them.
// `register` would persist the property in storage the main window shares, so it is stamped per event.
...(isEmbeddedPageFrame() ? [stampEmbeddedPageFrame] : []),
+ ...(sendsLocalDevelopmentToCloud() ? [dropExceptions] : []),
],
loaded: (loadedInstance) => {
if (loadedInstance.sessionRecording) {