Skip to content

Commit

Permalink
fix: session start when page appear without page hide (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
  • Loading branch information
zhu-xiaowei and xiaoweii authored May 8, 2024
1 parent 11f1240 commit 9e00e19
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/tracker/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Session {
startTime: number;
sessionIndex: number;
pauseTime: number;
isRecorded = false;

static createSession(uniqueId: string, sessionIndex: number): Session {
return new Session(
Expand All @@ -40,7 +41,7 @@ export class Session {
}

isNewSession(): boolean {
return this.pauseTime === undefined;
return this.pauseTime === undefined && !this.isRecorded;
}

getDuration(): number {
Expand All @@ -61,8 +62,9 @@ export class Session {
}
if (session !== null) {
if (
session.pauseTime === undefined ||
new Date().getTime() - session.pauseTime <
context.configuration.sessionTimeoutDuration
context.configuration.sessionTimeoutDuration
) {
return session;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/tracker/SessionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class SessionTracker extends BaseTracker {
pageViewTracker.setIsEntrances();
StorageUtil.clearPageInfo();
this.provider.record({ name: Event.PresetEvent.SESSION_START });
this.session.isRecorded = true;
if (!isFirstTime) {
pageViewTracker.onPageChange();
}
Expand Down
26 changes: 25 additions & 1 deletion test/tracker/SessionTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,31 @@ describe('SessionTracker test', () => {
const session = sessionTracker.session;
expect(session.sessionIndex).toBe(1);
expect(session.sessionId).not.toBeUndefined();
expect(session.isNewSession()).toBeTruthy();
expect(session.isNewSession()).toBeFalsy();
});

test('test page appear without page hide will not record session start twice', () => {
const pageAppearMock = jest.spyOn(sessionTracker, 'onPageAppear');
sessionTracker.setUp();
expect(pageAppearMock).toBeCalledWith(true);
expect(recordMethodMock).toBeCalledWith({
name: Event.PresetEvent.SESSION_START,
});
const session = sessionTracker.session;
expect(session.sessionIndex).toBe(1);
expect(session.sessionId).not.toBeUndefined();
expect(session.isRecorded).toBeTruthy();
expect(session.isNewSession()).toBeFalsy();

sessionTracker.onPageAppear(false);
sessionTracker.onPageAppear(false);
expect(pageAppearMock).toBeCalledWith(false);
const allCalls = recordMethodMock.mock.calls;
const sessionStartCalls = allCalls.filter((call: [any]) => {
const [arg] = call;
return arg && arg.name === Event.PresetEvent.SESSION_START;
});
expect(sessionStartCalls.length).toBe(1);
});

test('test first open and session start event has the same sessionId', () => {
Expand Down

0 comments on commit 9e00e19

Please sign in to comment.