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.

fix(signals): flush inbox report dwell time on tab close (#81439)

posthog[bot]7cb12f1d590c
diff --git a/products/signals/frontend/inbox/inboxAnalytics.test.ts b/products/signals/frontend/inbox/inboxAnalytics.test.ts
index 2d61b06ce1c3..8195f05c8631 100644
--- a/products/signals/frontend/inbox/inboxAnalytics.test.ts
+++ b/products/signals/frontend/inbox/inboxAnalytics.test.ts
@@ -3,6 +3,7 @@ import posthog from 'posthog-js'
import {
captureInboxQueryChanged,
captureInboxReportAction,
+ captureInboxReportClosed,
captureInboxReportsImpressed,
captureInboxSettingsChanged,
captureInboxViewed,
@@ -219,6 +220,16 @@ describe('inboxAnalytics', () => {
expect(JSON.stringify(props)).not.toContain('acme-alerts')
})
+ it('sends an unload close instantly so the dwell time leaves before the page does', () => {
+ captureInboxReportClosed(
+ { report: makeReport(), timeSpentMs: 4200, closeMethod: 'page_unload' },
+ { send_instantly: true }
+ )
+ const call = (posthog.capture as jest.Mock).mock.calls.find(([name]) => name === INBOX_EVENTS.REPORT_CLOSED)
+ expect(call?.[1]).toMatchObject({ close_method: 'page_unload', time_spent_ms: 4200 })
+ expect(call?.[2]).toEqual({ send_instantly: true })
+ })
+
it('records a connected source with first-connection and wizard flags', () => {
captureSignalSourceConnected({
sourceProduct: 'github',
diff --git a/products/signals/frontend/inbox/inboxAnalytics.ts b/products/signals/frontend/inbox/inboxAnalytics.ts
index b081668316c1..ce76c6110cbb 100644
--- a/products/signals/frontend/inbox/inboxAnalytics.ts
+++ b/products/signals/frontend/inbox/inboxAnalytics.ts
@@ -1,4 +1,5 @@
import posthog from 'posthog-js'
+import type { CaptureOptions } from 'posthog-js'
import { dayjs } from 'lib/dayjs'
@@ -46,8 +47,11 @@ export type InboxReportActionSurface = 'detail_pane' | 'detail_footer' | 'list_r
/** How a report detail was opened. */
export type InboxReportOpenMethod = 'click' | 'deeplink' | 'unknown'
-/** How a report detail was closed. */
-export type InboxReportCloseMethod = 'next_report' | 'deselected' | 'unmount'
+/**
+ * How a report detail was closed. `page_unload` is a tab close or hard page navigation: the scene
+ * never unmounts, so it flushes on `pagehide` instead of the `unmount` path.
+ */
+export type InboxReportCloseMethod = 'next_report' | 'deselected' | 'unmount' | 'page_unload'
/** Sentiment captured by the report feedback thumbs. */
export type InboxReportFeedbackSentiment = 'positive' | 'negative'
@@ -115,8 +119,8 @@ export type ScoutActionType =
/** What a scout chat CTA was asking for. Matches the desktop values. */
export type ScoutChatType = 'author_scout' | 'fleet_overview' | 'recent_signals'
-function captureInboxEvent(event: InboxEvent, properties: Record<string, unknown>): void {
- posthog.capture(event, { inbox_client: INBOX_CLIENT, ...properties })
+function captureInboxEvent(event: InboxEvent, properties: Record<string, unknown>, options?: CaptureOptions): void {
+ posthog.capture(event, { inbox_client: INBOX_CLIENT, ...properties }, options)
}
/** Whole hours since the report was created, rounded to one decimal. Mirrors desktop `report_age_hours`. */
@@ -260,16 +264,24 @@ export function captureInboxReportOpened(params: {
})
}
-export function captureInboxReportClosed(params: {
- report: SignalReport
- timeSpentMs: number
- closeMethod: InboxReportCloseMethod
-}): void {
- captureInboxEvent(INBOX_EVENTS.REPORT_CLOSED, {
- ...baseReportProperties(params.report),
- time_spent_ms: params.timeSpentMs,
- close_method: params.closeMethod,
- })
+export function captureInboxReportClosed(
+ params: {
+ report: SignalReport
+ timeSpentMs: number
+ closeMethod: InboxReportCloseMethod
+ },
+ /** The unload flush passes `{ send_instantly: true }` so the event leaves before the page goes. */
+ options?: CaptureOptions
+): void {
+ captureInboxEvent(
+ INBOX_EVENTS.REPORT_CLOSED,
+ {
+ ...baseReportProperties(params.report),
+ time_spent_ms: params.timeSpentMs,
+ close_method: params.closeMethod,
+ },
+ options
+ )
}
export function captureInboxReportAction(params: {
diff --git a/products/signals/frontend/inbox/inboxSceneLogic.ts b/products/signals/frontend/inbox/inboxSceneLogic.ts
index 3399be5262df..91883d315fe1 100644
--- a/products/signals/frontend/inbox/inboxSceneLogic.ts
+++ b/products/signals/frontend/inbox/inboxSceneLogic.ts
@@ -1,6 +1,7 @@
import { MakeLogicType, actions, connect, events, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { actionToUrl, router, urlToAction } from 'kea-router'
+import type { CaptureOptions } from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
@@ -212,6 +213,24 @@ interface InboxOpenTracking {
openedAt: number
}
+/**
+ * Emit the dwell-time close for a report still open when the view goes away, then clear the tracking
+ * so it can't fire twice. Shared by the scene unmount (`unmount`) and the tab-close flush
+ * (`page_unload`); a no-op when no report is open.
+ */
+function flushOpenReport(
+ cache: Record<string, any>,
+ closeMethod: InboxReportCloseMethod,
+ options?: CaptureOptions
+): void {
+ const open: InboxOpenTracking | undefined = cache.openTracking
+ if (!open) {
+ return
+ }
+ captureInboxReportClosed({ report: open.report, timeSpentMs: Date.now() - open.openedAt, closeMethod }, options)
+ cache.openTracking = undefined
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface inboxSceneLogicValues {
isSessionAnalysisRunning: boolean // signalSourcesLogic
@@ -672,20 +691,27 @@ export const inboxSceneLogic = kea<inboxSceneLogicType>([
})),
events(({ cache }) => ({
+ afterMount: () => {
+ // `beforeUnmount` flushes dwell time on in-app navigation, but a tab close or hard page
+ // unload never unmounts the scene, so half the closes were dropped. Flush on `pagehide`
+ // too. `pauseOnPageHidden: false` keeps the listener alive while the tab is hidden —
+ // `pagehide` fires as the tab goes away, exactly when a paused listener would be gone.
+ cache.disposables.add(
+ () => {
+ const onPageHide = (): void => flushOpenReport(cache, 'page_unload', { send_instantly: true })
+ window.addEventListener('pagehide', onPageHide)
+ return () => window.removeEventListener('pagehide', onPageHide)
+ },
+ 'reportUnloadFlush',
+ { pauseOnPageHidden: false }
+ )
+ },
// The Runs list loads lazily when its tab opens (via the `setActiveTab` listener). There is no
// mount pre-fetch, so an inbox visit that never opens Runs doesn't pay for its two requests.
beforeUnmount: () => {
clearInterval(cache.sessionAnalysisPollInterval)
// Flush dwell time for a report still open when the scene unmounts (navigated away).
- const open: InboxOpenTracking | undefined = cache.openTracking
- if (open) {
- captureInboxReportClosed({
- report: open.report,
- timeSpentMs: Date.now() - open.openedAt,
- closeMethod: 'unmount',
- })
- cache.openTracking = undefined
- }
+ flushOpenReport(cache, 'unmount')
},
})),