Skip to content

Commit

Permalink
feat: log a page-view end event when the page becomes inactive (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaye-statsig authored Jan 21, 2025
1 parent 8e067d1 commit d4a7dcd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
32 changes: 27 additions & 5 deletions packages/web-analytics/src/AutoCapture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from './Utils';
import { _gatherPageViewPayload } from './payloadUtils';

const PAGE_INACTIVE_TIMEOUT = 600000;
const AUTO_EVENT_MAPPING: Record<string, AutoCaptureEventName> = {
submit: AutoCaptureEventName.FORM_SUBMIT,
click: AutoCaptureEventName.CLICK,
Expand Down Expand Up @@ -58,6 +59,7 @@ export class AutoCapture {
private _previousLoggedPageViewUrl: URL | null = null;
private _eventFilterFunc?: (event: AutoCaptureEvent) => boolean;
private _hasLoggedPageViewEnd = false;
private _inactiveTimer: number | null = null;

constructor(
private _client: PrecomputedEvaluationsInterface,
Expand Down Expand Up @@ -94,13 +96,16 @@ export class AutoCapture {
return;
}

const eventHandler = (event: Event) => {
const eventHandler = (event: Event, userAction = true) => {
this._autoLogEvent(event || win.event);
if (userAction) {
this._bumpInactiveTimer();
}
};

_registerEventHandler(doc, 'click', eventHandler);
_registerEventHandler(doc, 'submit', eventHandler);
_registerEventHandler(win, 'error', eventHandler);
_registerEventHandler(doc, 'click', (e) => eventHandler(e));
_registerEventHandler(doc, 'submit', (e) => eventHandler(e));
_registerEventHandler(win, 'error', (e) => eventHandler(e, false));
_registerEventHandler(win, 'pagehide', () => this._tryLogPageViewEnd());
_registerEventHandler(win, 'beforeunload', () => this._tryLogPageViewEnd());
_registerEventHandler(win, 'scroll', () => this._scrollEventHandler());
Expand Down Expand Up @@ -150,6 +155,20 @@ export class AutoCapture {
this._enqueueAutoCapture(eventName, value, metadata);
}

private _bumpInactiveTimer() {
const win = _getWindowSafe();
if (!win) {
return;
}

if (this._inactiveTimer) {
clearTimeout(this._inactiveTimer);
}
this._inactiveTimer = win.setTimeout(() => {
this._tryLogPageViewEnd(true);
}, PAGE_INACTIVE_TIMEOUT);
}

private _initialize() {
this._addEventHandlers();
this._addPageViewTracking();
Expand Down Expand Up @@ -222,9 +241,10 @@ export class AutoCapture {
addNewSessionMetadata: true,
},
);
this._bumpInactiveTimer();
}

private _tryLogPageViewEnd() {
private _tryLogPageViewEnd(dueToInactivity = false) {
if (this._hasLoggedPageViewEnd) {
return;
}
Expand All @@ -236,6 +256,7 @@ export class AutoCapture {
{
scrollDepth: this._deepestScroll,
pageViewLength: Date.now() - this._startTime,
dueToInactivity,
},
{ flushImmediately: true },
);
Expand Down Expand Up @@ -352,6 +373,7 @@ export class AutoCapture {
this._deepestScroll,
Math.min(100, Math.round(((scrollY + innerHeight) / scrollHeight) * 100)),
);
this._bumpInactiveTimer();
}

private _isNewSession(session: StatsigSession) {
Expand Down
6 changes: 5 additions & 1 deletion tools/scripts/minifier-name-cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@
"$_getParameterStoreImpl": "Ri",
"$_hasLoggedPageViewEnd": "Ei",
"$_tryLogPageViewEnd": "Mi",
"$_configureUser": "xi"
"$_configureUser": "xi",
"$_lastSeenError": "Ai",
"$_lastUsedInitUrl": "Ni",
"$_inactiveTimer": "Pi",
"$_bumpInactiveTimer": "Ci"
}
}
}

0 comments on commit d4a7dcd

Please sign in to comment.