|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + createEngagedTimeTracker, |
| 4 | + ENGAGED_TIME_THRESHOLDS_MS, |
| 5 | +} from './engaged-time'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Why this exists. |
| 9 | + * |
| 10 | + * PostHog derives `session_duration` from `max(timestamp) - min(timestamp)` |
| 11 | + * over a session's events, and `$pageleave` only fires on `pagehide` — never |
| 12 | + * on `visibilitychange`. A visitor who lands, reads, clicks nothing and leaves |
| 13 | + * the tab open therefore emits exactly one event, records a duration of zero, |
| 14 | + * and is classified as a bounce. Measured 2026-09-18: that is 13.6% of desktop |
| 15 | + * and 17.8% of mobile homepage sessions. |
| 16 | + * |
| 17 | + * This tracker exists to make the recorded duration reflect the real one. That |
| 18 | + * only holds if it counts *visible* time and nothing else — time in a hidden |
| 19 | + * background tab is not engagement, and counting it would inflate the metric |
| 20 | + * rather than correct it. The hidden-time tests below are the ones that keep |
| 21 | + * this honest, so treat them as load-bearing rather than incidental. |
| 22 | + */ |
| 23 | + |
| 24 | +function setup(startVisible = true) { |
| 25 | + let clock = 1_000; |
| 26 | + const onThreshold = vi.fn(); |
| 27 | + const tracker = createEngagedTimeTracker({ |
| 28 | + onThreshold, |
| 29 | + now: () => clock, |
| 30 | + startVisible, |
| 31 | + }); |
| 32 | + return { |
| 33 | + onThreshold, |
| 34 | + tracker, |
| 35 | + advance(ms: number) { |
| 36 | + clock += ms; |
| 37 | + tracker.tick(); |
| 38 | + }, |
| 39 | + advanceWithoutTick(ms: number) { |
| 40 | + clock += ms; |
| 41 | + }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('engaged time thresholds', () => { |
| 46 | + it('exports 10s and 30s, the first matching PostHog bounce cutoff', () => { |
| 47 | + // 10s is not arbitrary: PostHog's bounce test is `session_duration >= 10s`. |
| 48 | + // An event at a genuine 10 visible seconds makes the recorded duration |
| 49 | + // match reality for a reader who never clicks. |
| 50 | + expect(ENGAGED_TIME_THRESHOLDS_MS).toEqual([10_000, 30_000]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('fires the first threshold only once ten visible seconds have passed', () => { |
| 54 | + const { advance, onThreshold } = setup(); |
| 55 | + advance(9_999); |
| 56 | + expect(onThreshold).not.toHaveBeenCalled(); |
| 57 | + advance(1); |
| 58 | + expect(onThreshold).toHaveBeenCalledExactlyOnceWith(10_000); |
| 59 | + }); |
| 60 | + |
| 61 | + it('fires each threshold exactly once, however many times it is ticked', () => { |
| 62 | + const { advance, onThreshold } = setup(); |
| 63 | + advance(10_000); |
| 64 | + advance(1_000); |
| 65 | + advance(1_000); |
| 66 | + expect(onThreshold).toHaveBeenCalledExactlyOnceWith(10_000); |
| 67 | + }); |
| 68 | + |
| 69 | + it('fires the second threshold at thirty visible seconds', () => { |
| 70 | + const { advance, onThreshold } = setup(); |
| 71 | + advance(30_000); |
| 72 | + expect(onThreshold).toHaveBeenNthCalledWith(1, 10_000); |
| 73 | + expect(onThreshold).toHaveBeenNthCalledWith(2, 30_000); |
| 74 | + expect(onThreshold).toHaveBeenCalledTimes(2); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not count time while the page is hidden', () => { |
| 78 | + // The honesty test. A backgrounded tab accrues wall-clock time but no |
| 79 | + // engagement; counting it would inflate session duration rather than |
| 80 | + // correct it. |
| 81 | + const { advance, tracker, onThreshold } = setup(); |
| 82 | + advance(5_000); |
| 83 | + tracker.setVisible(false); |
| 84 | + advance(60_000); |
| 85 | + expect(onThreshold).not.toHaveBeenCalled(); |
| 86 | + expect(tracker.visibleMs()).toBe(5_000); |
| 87 | + }); |
| 88 | + |
| 89 | + it('resumes accumulating when the page becomes visible again', () => { |
| 90 | + const { advance, tracker, onThreshold } = setup(); |
| 91 | + advance(6_000); |
| 92 | + tracker.setVisible(false); |
| 93 | + advance(60_000); |
| 94 | + tracker.setVisible(true); |
| 95 | + advance(4_000); |
| 96 | + expect(onThreshold).toHaveBeenCalledExactlyOnceWith(10_000); |
| 97 | + expect(tracker.visibleMs()).toBe(10_000); |
| 98 | + }); |
| 99 | + |
| 100 | + it('keeps time from earlier visible runs across repeated hide/show cycles', () => { |
| 101 | + // One cycle cannot distinguish `accumulatedMs +=` from `accumulatedMs =`, |
| 102 | + // because the accumulator is still zero. Two can. |
| 103 | + const { advance, tracker, onThreshold } = setup(); |
| 104 | + advance(4_000); |
| 105 | + tracker.setVisible(false); |
| 106 | + advance(60_000); |
| 107 | + tracker.setVisible(true); |
| 108 | + advance(4_000); |
| 109 | + tracker.setVisible(false); |
| 110 | + advance(60_000); |
| 111 | + tracker.setVisible(true); |
| 112 | + advance(2_000); |
| 113 | + expect(tracker.visibleMs()).toBe(10_000); |
| 114 | + expect(onThreshold).toHaveBeenCalledExactlyOnceWith(10_000); |
| 115 | + }); |
| 116 | + |
| 117 | + it('reports completion only once every threshold has fired', () => { |
| 118 | + const { advance, tracker } = setup(); |
| 119 | + advance(10_000); |
| 120 | + expect(tracker.isComplete()).toBe(false); |
| 121 | + advance(20_000); |
| 122 | + expect(tracker.isComplete()).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('never fires for a page that starts hidden and is never seen', () => { |
| 126 | + // A prerendered or background-opened tab must not report engagement. |
| 127 | + const { advance, onThreshold, tracker } = setup(false); |
| 128 | + advance(120_000); |
| 129 | + expect(onThreshold).not.toHaveBeenCalled(); |
| 130 | + expect(tracker.visibleMs()).toBe(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('counts time that elapsed between ticks, not tick count', () => { |
| 134 | + // The production caller ticks on an interval; a throttled background |
| 135 | + // timer must not under-report time the page was genuinely visible. |
| 136 | + const { advanceWithoutTick, tracker, onThreshold } = setup(); |
| 137 | + advanceWithoutTick(30_000); |
| 138 | + tracker.tick(); |
| 139 | + expect(onThreshold).toHaveBeenCalledTimes(2); |
| 140 | + }); |
| 141 | + |
| 142 | + it('does not double count when told it is visible twice', () => { |
| 143 | + const { advance, tracker, onThreshold } = setup(); |
| 144 | + advance(5_000); |
| 145 | + tracker.setVisible(true); |
| 146 | + advance(4_999); |
| 147 | + expect(onThreshold).not.toHaveBeenCalled(); |
| 148 | + expect(tracker.visibleMs()).toBe(9_999); |
| 149 | + }); |
| 150 | +}); |
0 commit comments