Skip to content

Commit

Permalink
fix: idle duration not updated when user hides page (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii committed May 16, 2024
1 parent 6fceae9 commit 650a4d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/tracker/SessionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
import { Logger } from '@aws-amplify/core';
import { BaseTracker } from './BaseTracker';
import { PageViewTracker } from './PageViewTracker';
import { Session } from './Session';
import { BrowserInfo } from '../browser';
import { Event } from '../provider';
Expand Down Expand Up @@ -103,6 +104,7 @@ export class SessionTracker extends BaseTracker {
}

recordUserEngagement(isImmediate: boolean) {
PageViewTracker.updateIdleDuration();
this.provider.pageViewTracker.recordUserEngagement(isImmediate);
}

Expand Down
10 changes: 10 additions & 0 deletions test/tracker/SessionTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,16 @@ describe('SessionTracker test', () => {
expect(clearFailedEventsMock).not.toBeCalled();
});

test('test hide page will update lastActiveTimestamp', async () => {
sessionTracker.setUp();
const startTimestamp = PageViewTracker.lastActiveTimestamp;
await sleep(110);
hidePage();
expect(
PageViewTracker.lastActiveTimestamp - startTimestamp > 100
).toBeTruthy();
});

function hidePage() {
Object.defineProperty(window.document, 'hidden', {
writable: true,
Expand Down

0 comments on commit 650a4d5

Please sign in to comment.