: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(today): measure key number load outcomes and latency (#124446)
Shy Alteraf99ea378ac8
diff --git a/frontend/src/scenes/project-homepage/today/TodayHomePage.test.tsx b/frontend/src/scenes/project-homepage/today/TodayHomePage.test.tsxnew file mode 100644index 000000000000..6fac754d4ccd--- /dev/null+++ b/frontend/src/scenes/project-homepage/today/TodayHomePage.test.tsx@@ -0,0 +1,54 @@+import { cleanup, render } from '@testing-library/react'+import { useActions, useValues } from 'kea'++import { TodayHomePage } from './TodayHomePage'+import { todayLogic } from './todayLogic'+import { makeHighlights, makePulseMetric } from './todayTestFixtures'++jest.mock('kea', () => ({ ...jest.requireActual('kea'), useActions: jest.fn(), useValues: jest.fn() }))+jest.mock('./todayLogic', () => ({ todayLogic: { values: { homeScrollTop: null } }, reportIdFromPath: () => null }))+jest.mock('./todayHighlightsLogic', () => ({ todayHighlightsLogic: {} }))+jest.mock('./TodaySampleBanner', () => ({ TodaySampleBanner: () => null }))+jest.mock('./TodayForYou', () => ({ TodayForYou: () => null }))+jest.mock('./TodayRecents', () => ({ TodayRecents: () => null }))+jest.mock('./TodayPulse', () => ({ TodayPulse: () => null }))++describe('TodayHomePage load measurement', () => {+ afterEach(cleanup)++ it('evaluates existing numbers when leaving sample mode', () => {+ const home = {+ greeting: 'Hello',+ now: Date.now(),+ isNewProject: false,+ recentObjects: [],+ returnFocusReportId: null,+ useSampleData: true,+ }+ const numbers = {+ currentProjectId: 1,+ highlights: makeHighlights({ pulse: [makePulseMetric()] }),+ highlightsLoading: false,+ highlightsFailed: false,+ highlightsPending: false,+ pulseTiles: [makePulseMetric()],+ pulsePinsLoaded: false,+ }+ const actions = {+ setReturnFocusReportId: jest.fn(),+ pulseViewMounted: jest.fn(),+ pulseViewUpdated: jest.fn(),+ pulseViewUnmounted: jest.fn(),+ }+ jest.mocked(useValues).mockImplementation((logic) => (logic === todayLogic ? home : numbers))+ jest.mocked(useActions).mockImplementation(() => actions)++ const view = render(<TodayHomePage />)+ actions.pulseViewUpdated.mockClear()+ home.useSampleData = false+ view.rerender(<TodayHomePage />)++ expect(actions.pulseViewMounted).toHaveBeenLastCalledWith(false)+ expect(actions.pulseViewUpdated).toHaveBeenCalledTimes(1)+ })+})diff --git a/frontend/src/scenes/project-homepage/today/TodayHomePage.tsx b/frontend/src/scenes/project-homepage/today/TodayHomePage.tsxindex 215a4c709ab0..3b206186a3c8 100644--- a/frontend/src/scenes/project-homepage/today/TodayHomePage.tsx+++ b/frontend/src/scenes/project-homepage/today/TodayHomePage.tsx@@ -47,11 +47,38 @@ function TodayMetaLine(): JSX.Element {}export function TodayHomePage(): JSX.Element {- const { greeting, isNewProject, recentObjects, returnFocusReportId } = useValues(todayLogic)+ const { greeting, isNewProject, recentObjects, returnFocusReportId, useSampleData } = useValues(todayLogic)const { setReturnFocusReportId } = useActions(todayLogic)- const { highlights, highlightsPending, pulseTiles, pulsePinsLoaded } = useValues(todayHighlightsLogic)+ const {+ currentProjectId,+ highlights,+ highlightsLoading,+ highlightsFailed,+ highlightsPending,+ pulseTiles,+ pulsePinsLoaded,+ } = useValues(todayHighlightsLogic)+ const { pulseViewMounted, pulseViewUpdated, pulseViewUnmounted } = useActions(todayHighlightsLogic)const rootRef = useRef<HTMLDivElement>(null)+ useLayoutEffect(() => {+ pulseViewMounted(useSampleData)+ return () => pulseViewUnmounted()+ }, [currentProjectId, useSampleData, pulseViewMounted, pulseViewUnmounted])++ useLayoutEffect(() => {+ pulseViewUpdated()+ }, [+ highlights,+ highlightsLoading,+ highlightsFailed,+ highlightsPending,+ pulseTiles,+ pulseViewUpdated,+ currentProjectId,+ useSampleData,+ ])+useLayoutEffect(() => {const scroller = rootRef.current?.closest('main')const savedTop = todayLogic.values.homeScrollTopdiff --git a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.tsindex 9c93935ad246..07764be04775 100644--- a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts+++ b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.test.ts@@ -20,6 +20,7 @@ describe('todayHighlightsLogic', () => {highlightCalls += 1return highlightResponses.length > 1 ? highlightResponses.shift()! : highlightResponses[0]},+ '/api/projects/:team_id/today/pulse_pins/': () => [200, { pins: [] }],},})initKeaTests()@@ -116,4 +117,191 @@ describe('todayHighlightsLogic', () => {expect(logic.values.hiddenHighlights).toEqual({ '1:alert:a': today, '997:alert:a': today })expect(logic.values.doneHighlightKeys).toEqual(['alert:a'])})++ it('measures the first rendered number and the final result once per home visit', async () => {+ jest.useFakeTimers()+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ highlightResponses = [[200, makeHighlights({ status: 'computing', generated_at: null })]]+ const logic = todayHighlightsLogic()+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadHighlightsSuccess'])+ logic.actions.pulseViewMounted(false)+ logic.actions.pulseViewUpdated()+ await jest.advanceTimersByTimeAsync(400)+ logic.actions.loadHighlightsSuccess(makeHighlights({ status: 'computing', pulse: [makePulseMetric()] }))+ logic.actions.pulseViewUpdated()+ logic.actions.pulseViewUpdated()+ await jest.advanceTimersByTimeAsync(300)+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: [makePulseMetric()] }))+ logic.actions.pulseViewUpdated()+ logic.actions.pulseViewUpdated()+ logic.actions.pulseViewUnmounted()++ const events = capture.mock.calls.filter(([event]) => event.startsWith('today pulse'))+ expect(events.map(([event]) => event)).toEqual([+ 'today pulse load started',+ 'today pulse first number shown',+ 'today pulse load finished',+ ])+ expect(events[1][1]).toMatchObject({ duration_ms: 400, first_number_duration_ms: 400, number_count: 1 })+ expect(events[2][1]).toMatchObject({+ load_id: events[0][1]?.load_id,+ duration_ms: 700,+ first_number_duration_ms: 400,+ outcome: 'ready',+ initial_status: 'computing',+ content_at_mount: false,+ was_hidden: false,+ })+ })++ it.each([+ { status: 200, body: makeHighlights(), outcome: 'empty' },+ { status: 200, body: makeHighlights({ status: 'failed' }), outcome: 'failed' },+ { status: 404, body: {}, outcome: 'unavailable' },+ { status: 500, body: {}, outcome: 'request_error' },+ ])('records a terminal $outcome without inventing a first-number time', async ({ status, body, outcome }) => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ highlightResponses = [[status, body]]+ const logic = todayHighlightsLogic()+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ logic.actions.pulseViewMounted(false)+ logic.actions.pulseViewUpdated()++ expect(capture).toHaveBeenCalledWith(+ 'today pulse load finished',+ expect.objectContaining({+ outcome,+ first_number_duration_ms: null,+ number_count: 0,+ })+ )+ expect(capture.mock.calls.some(([event]) => event === 'today pulse first number shown')).toBe(false)+ logic.actions.pulseViewUnmounted()+ })++ it('records an already available number without waiting for another request', async () => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ highlightResponses = [[200, makeHighlights({ pulse: [makePulseMetric()] })]]+ const logic = todayHighlightsLogic()+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ const requests = highlightCalls+ logic.actions.pulseViewMounted(false)+ logic.actions.pulseViewUpdated()++ expect(highlightCalls).toBe(requests)+ expect(capture).toHaveBeenCalledWith(+ 'today pulse first number shown',+ expect.objectContaining({+ content_at_mount: true,+ initial_status: 'ready',+ number_count: 1,+ })+ )+ logic.actions.pulseViewUnmounted()+ })++ it('records a slow unanswered load and abandonment without stopping the request', async () => {+ jest.useFakeTimers()+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ const api = await import('products/today/frontend/generated/api')+ jest.spyOn(api, 'todayHighlightsRetrieve').mockImplementation(() => new Promise(() => {}))+ const logic = todayHighlightsLogic()+ logic.mount()+ logic.actions.pulseViewMounted(false)+ await jest.advanceTimersByTimeAsync(30_000)++ expect(logic.values.highlightsLoading).toBe(true)+ expect(capture).toHaveBeenCalledWith(+ 'today pulse load slow',+ expect.objectContaining({+ duration_ms: 30_000,+ request_count: 1,+ })+ )+ logic.actions.pulseViewUnmounted()+ await jest.advanceTimersByTimeAsync(30_000)+ expect(capture.mock.calls.filter(([event]) => event === 'today pulse load slow')).toHaveLength(1)+ expect(capture).toHaveBeenCalledWith(+ 'today pulse load finished',+ expect.objectContaining({ outcome: 'abandoned' })+ )+ })++ it('does not measure sample visits', async () => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ const logic = todayHighlightsLogic()+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ logic.actions.pulseViewMounted(true)+ logic.actions.pulseViewUpdated()+ logic.actions.pulseViewUnmounted()++ expect(capture.mock.calls.filter(([event]) => event.startsWith('today pulse load'))).toHaveLength(0)+ expect(capture.mock.calls.some(([event]) => event === 'today pulse first number shown')).toBe(false)+ })++ it.each([false, true])('records document departure once and handles cached restoration: %s', async (persisted) => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ highlightResponses = [[200, makeHighlights({ status: 'computing', generated_at: null })]]+ const logic = todayHighlightsLogic()+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadHighlightsSuccess'])+ logic.actions.pulseViewMounted(false)+ const firstLoadId = capture.mock.calls.find(([event]) => event === 'today pulse load started')?.[1]?.load_id++ window.dispatchEvent(new PageTransitionEvent('pagehide', { persisted }))++ expect(capture).toHaveBeenCalledWith(+ 'today pulse load finished',+ expect.objectContaining({ load_id: firstLoadId, outcome: 'abandoned' }),+ { transport: 'sendBeacon', send_instantly: true }+ )+ if (persisted) {+ window.dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true }))+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: [makePulseMetric()] }))+ logic.actions.pulseViewUpdated()+ const restored = capture.mock.calls.find(+ ([event, properties]) => event === 'today pulse load finished' && properties?.outcome === 'ready'+ )+ expect(restored?.[1]?.load_id).not.toBe(firstLoadId)+ expect(restored?.[1]).toMatchObject({ outcome: 'ready', number_count: 1, load_id: expect.any(String) })+ }+ logic.actions.pulseViewUnmounted()+ window.dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true }))+ expect(capture.mock.calls.filter(([event]) => event === 'today pulse load started')).toHaveLength(+ persisted ? 2 : 1+ )+ expect(capture.mock.calls.filter(([event]) => event === 'today pulse load finished')).toHaveLength(+ persisted ? 2 : 1+ )+ })++ it('marks a load that spends time in a hidden tab', async () => {+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)+ highlightResponses = [[200, makeHighlights({ status: 'computing', generated_at: null })]]+ const logic = todayHighlightsLogic()+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadHighlightsSuccess'])+ logic.actions.pulseViewMounted(false)+ const visibility = jest.spyOn(document, 'visibilityState', 'get')+ visibility.mockReturnValue('hidden')+ document.dispatchEvent(new Event('visibilitychange'))+ visibility.mockReturnValue('visible')+ document.dispatchEvent(new Event('visibilitychange'))+ logic.actions.loadHighlightsSuccess(makeHighlights({ pulse: [makePulseMetric()] }))+ logic.actions.pulseViewUpdated()++ expect(capture).toHaveBeenCalledWith(+ 'today pulse load finished',+ expect.objectContaining({+ started_visible: true,+ visible: true,+ was_hidden: true,+ })+ )+ logic.actions.pulseViewUnmounted()+ })})diff --git a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.tsindex 1c83a82fc8d9..6324ba673047 100644--- a/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts+++ b/frontend/src/scenes/project-homepage/today/todayHighlightsLogic.ts@@ -1,6 +1,7 @@import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'import { loaders } from 'kea-loaders'-import posthog from 'posthog-js'+import posthog, { type CaptureOptions } from 'posthog-js'+import { v4 as uuidv4 } from 'uuid'import { toast } from '@posthog/quill'@@ -29,6 +30,66 @@ const PINS_LOAD_RETRIES = 3const PIN_SEARCH_RESULTS = 20const PINNABLE_DISPLAYS = 'ActionsLineGraph,ActionsBar,ActionsStackedBar,ActionsAreaGraph'export const PULSE_TILES = 4+const PULSE_SLOW_MS = 30_000++interface PulseLoad {+ id: string+ startedAt: number+ firstNumberAt: number | null+ requestCount: number+ responseCount: number+ errorCount: number+ initialStatus: HighlightsApi['status'] | null+ contentAtMount: boolean+ startedVisible: boolean+ wasHidden: boolean+ finished: boolean+}++function pulseLoadOutcome(+ status: HighlightsApi['status'] | undefined,+ failed: boolean,+ unavailable: boolean,+ numberCount: number+): 'request_error' | 'unavailable' | 'failed' | 'ready' | 'empty' | null {+ if (failed) {+ return 'request_error'+ }+ if (unavailable) {+ return 'unavailable'+ }+ if (status === 'failed') {+ return 'failed'+ }+ return status === 'ready' ? (numberCount > 0 ? 'ready' : 'empty') : null+}++function capturePulseLoad(+ event: string,+ load: PulseLoad,+ properties: Record<string, unknown> = {},+ options?: CaptureOptions+): void {+ const payload = {+ load_id: load.id,+ duration_ms: Math.round(performance.now() - load.startedAt),+ first_number_duration_ms: load.firstNumberAt === null ? null : Math.round(load.firstNumberAt - load.startedAt),+ request_count: load.requestCount,+ response_count: load.responseCount,+ error_count: load.errorCount,+ initial_status: load.initialStatus,+ content_at_mount: load.contentAtMount,+ started_visible: load.startedVisible,+ was_hidden: load.wasHidden,+ visible: document.visibilityState === 'visible',+ ...properties,+ }+ if (options) {+ posthog.capture(event, payload, options)+ } else {+ posthog.capture(event, payload)+ }+}export interface PulsePlaceholderTile extends PulsePinApi {placeholder: 'computing' | 'unavailable'@@ -181,6 +242,15 @@ export interface todayHighlightsLogicActions {pulsePinsSaved: () => {value: true}+ pulseViewMounted: (sample: boolean) => {+ sample: boolean+ }+ pulseViewUnmounted: (documentDeparture?: boolean) => {+ documentDeparture: boolean+ }+ pulseViewUpdated: () => {+ value: true+ }releasePulseOrder: (owner: string) => {owner: string}@@ -268,6 +338,9 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([holdPulseOrder: (owner: string, keys: string[]) => ({ owner, keys }),releasePulseOrder: (owner: string) => ({ owner }),pulsePinsLoadGaveUp: true,+ pulseViewMounted: (sample: boolean) => ({ sample }),+ pulseViewUpdated: true,+ pulseViewUnmounted: (documentDeparture: boolean = false) => ({ documentDeparture }),}),loaders(({ values, cache }) => ({pulseInsightResults: [@@ -447,6 +520,124 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([],}),listeners(({ actions, values, cache }) => ({+ pulseViewMounted: ({ sample }) => {+ if (sample) {+ return+ }+ const load: PulseLoad = {+ id: uuidv4(),+ startedAt: performance.now(),+ firstNumberAt: null,+ requestCount: values.highlightsLoading ? 1 : 0,+ responseCount: 0,+ errorCount: 0,+ initialStatus: values.highlights?.status ?? null,+ contentAtMount: values.highlights?.generated_at !== null && values.highlights !== null,+ startedVisible: document.visibilityState === 'visible',+ wasHidden: document.visibilityState !== 'visible',+ finished: false,+ }+ cache.pulseLoad = load+ capturePulseLoad('today pulse load started', load)+ cache.disposables.add(+ () => {+ const onPageHide = (): void => actions.pulseViewUnmounted(true)+ const onPageShow = (event: PageTransitionEvent): void => {+ if (event.persisted) {+ actions.pulseViewMounted(sample)+ actions.pulseViewUpdated()+ }+ }+ window.addEventListener('pagehide', onPageHide)+ window.addEventListener('pageshow', onPageShow)+ return () => {+ window.removeEventListener('pagehide', onPageHide)+ window.removeEventListener('pageshow', onPageShow)+ }+ },+ 'pulseLoadPageLifecycle',+ { pauseOnPageHidden: false }+ )+ cache.disposables.add(+ () => {+ const onVisibilityChange = (): void => {+ load.wasHidden ||= document.visibilityState !== 'visible'+ }+ document.addEventListener('visibilitychange', onVisibilityChange)+ return () => document.removeEventListener('visibilitychange', onVisibilityChange)+ },+ 'pulseLoadVisibility',+ { pauseOnPageHidden: false }+ )+ cache.disposables.add(+ () => {+ const timer = window.setTimeout(() => {+ if (!load.finished) {+ capturePulseLoad('today pulse load slow', load)+ }+ }, PULSE_SLOW_MS)+ return () => clearTimeout(timer)+ },+ 'pulseLoadSlow',+ { pauseOnPageHidden: false }+ )+ },+ pulseViewUpdated: () => {+ const load = cache.pulseLoad as PulseLoad | undefined+ if (!load || load.finished) {+ return+ }+ const numberCount = values.highlightsPending+ ? 0+ : values.pulseTiles.filter((tile) => !isPlaceholderTile(tile)).length+ if (numberCount > 0 && load.firstNumberAt === null) {+ load.firstNumberAt = performance.now()+ capturePulseLoad('today pulse first number shown', load, { number_count: numberCount })+ }+ const outcome = pulseLoadOutcome(+ values.highlights?.status,+ values.highlightsFailed,+ cache.highlightsUnavailable === true,+ numberCount+ )+ if (outcome) {+ load.finished = true+ cache.disposables.dispose('pulseLoadSlow')+ cache.disposables.dispose('pulseLoadVisibility')+ capturePulseLoad('today pulse load finished', load, { outcome, number_count: numberCount })+ }+ },+ pulseViewUnmounted: ({ documentDeparture }) => {+ const load = cache.pulseLoad as PulseLoad | undefined+ if (load && !load.finished) {+ load.finished = true+ capturePulseLoad(+ 'today pulse load finished',+ load,+ { outcome: 'abandoned' },+ documentDeparture ? { transport: 'sendBeacon', send_instantly: true } : undefined+ )+ }+ cache.disposables.dispose('pulseLoadSlow')+ cache.disposables.dispose('pulseLoadVisibility')+ if (!documentDeparture) {+ cache.disposables.dispose('pulseLoadPageLifecycle')+ }+ delete cache.pulseLoad+ },+ loadHighlights: () => {+ cache.highlightsUnavailable = false+ const load = cache.pulseLoad as PulseLoad | undefined+ if (load && !load.finished) {+ load.requestCount += 1+ }+ },+ loadHighlightsFailure: () => {+ const load = cache.pulseLoad as PulseLoad | undefined+ if (load && !load.finished) {+ load.errorCount += 1+ }+ },setPinnedPulseKeys: async ({ metrics }, breakpoint) => {cache.pinEdits = (cache.pinEdits ?? 0) + 1const previous: Promise<unknown> = cache.pinsSave ?? Promise.resolve()@@ -560,6 +751,12 @@ export const todayHighlightsLogic = kea<todayHighlightsLogicType>([}, 'pulsePinsLoadRetry')},loadHighlightsSuccess: ({ highlights }) => {+ cache.highlightsUnavailable = highlights === null+ const load = cache.pulseLoad as PulseLoad | undefined+ if (load && !load.finished) {+ load.responseCount += 1+ load.initialStatus ??= highlights?.status ?? null+ }if (!highlights) {return}