Skip to content

Commit 9e00e19

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: session start when page appear without page hide (#53)
Co-authored-by: xiaoweii <[email protected]>
1 parent 11f1240 commit 9e00e19

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/tracker/Session.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class Session {
1818
startTime: number;
1919
sessionIndex: number;
2020
pauseTime: number;
21+
isRecorded = false;
2122

2223
static createSession(uniqueId: string, sessionIndex: number): Session {
2324
return new Session(
@@ -40,7 +41,7 @@ export class Session {
4041
}
4142

4243
isNewSession(): boolean {
43-
return this.pauseTime === undefined;
44+
return this.pauseTime === undefined && !this.isRecorded;
4445
}
4546

4647
getDuration(): number {
@@ -61,8 +62,9 @@ export class Session {
6162
}
6263
if (session !== null) {
6364
if (
65+
session.pauseTime === undefined ||
6466
new Date().getTime() - session.pauseTime <
65-
context.configuration.sessionTimeoutDuration
67+
context.configuration.sessionTimeoutDuration
6668
) {
6769
return session;
6870
} else {

src/tracker/SessionTracker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class SessionTracker extends BaseTracker {
7272
pageViewTracker.setIsEntrances();
7373
StorageUtil.clearPageInfo();
7474
this.provider.record({ name: Event.PresetEvent.SESSION_START });
75+
this.session.isRecorded = true;
7576
if (!isFirstTime) {
7677
pageViewTracker.onPageChange();
7778
}

test/tracker/SessionTracker.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,31 @@ describe('SessionTracker test', () => {
9393
const session = sessionTracker.session;
9494
expect(session.sessionIndex).toBe(1);
9595
expect(session.sessionId).not.toBeUndefined();
96-
expect(session.isNewSession()).toBeTruthy();
96+
expect(session.isNewSession()).toBeFalsy();
97+
});
98+
99+
test('test page appear without page hide will not record session start twice', () => {
100+
const pageAppearMock = jest.spyOn(sessionTracker, 'onPageAppear');
101+
sessionTracker.setUp();
102+
expect(pageAppearMock).toBeCalledWith(true);
103+
expect(recordMethodMock).toBeCalledWith({
104+
name: Event.PresetEvent.SESSION_START,
105+
});
106+
const session = sessionTracker.session;
107+
expect(session.sessionIndex).toBe(1);
108+
expect(session.sessionId).not.toBeUndefined();
109+
expect(session.isRecorded).toBeTruthy();
110+
expect(session.isNewSession()).toBeFalsy();
111+
112+
sessionTracker.onPageAppear(false);
113+
sessionTracker.onPageAppear(false);
114+
expect(pageAppearMock).toBeCalledWith(false);
115+
const allCalls = recordMethodMock.mock.calls;
116+
const sessionStartCalls = allCalls.filter((call: [any]) => {
117+
const [arg] = call;
118+
return arg && arg.name === Event.PresetEvent.SESSION_START;
119+
});
120+
expect(sessionStartCalls.length).toBe(1);
97121
});
98122

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

0 commit comments

Comments
 (0)