Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: session start when page appear without page hide #53

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading