Skip to content

Commit

Permalink
fix: sessionId for first open and session start events
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweii committed Mar 4, 2024
1 parent 8dd878a commit 38fcd8c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
30 changes: 17 additions & 13 deletions src/tracker/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,29 @@ export class Session {
this.pauseTime = new Date().getTime();
}

static getCurrentSession(context: ClickstreamContext): Session {
const storedSession = StorageUtil.getSession();
let sessionIndex = 1;
if (storedSession !== null) {
static getCurrentSession(
context: ClickstreamContext,
previousSession: Session = null
): Session {
let session = previousSession;
if (previousSession === null) {
session = StorageUtil.getSession();
}
if (session !== null) {
if (
new Date().getTime() - storedSession.pauseTime <
new Date().getTime() - session.pauseTime <
context.configuration.sessionTimeoutDuration
) {
return new Session(
storedSession.sessionId,
storedSession.sessionIndex,
storedSession.startTime,
storedSession.pauseTime
);
return session;
} else {
sessionIndex = storedSession.sessionIndex + 1;
return Session.createSession(
context.userUniqueId,
session.sessionIndex + 1
);
}
} else {
return Session.createSession(context.userUniqueId, 1);
}
return Session.createSession(context.userUniqueId, sessionIndex);
}

private static getSessionId(uniqueId: string): string {
Expand Down
4 changes: 3 additions & 1 deletion src/tracker/SessionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export class SessionTracker extends BaseTracker {
logger.debug('page appear');
const pageViewTracker = this.provider.pageViewTracker;
pageViewTracker.updateLastScreenStartTimestamp();
this.session = Session.getCurrentSession(this.context);
if (!isFirstTime) {
this.session = Session.getCurrentSession(this.context, this.session);
}
if (this.session.isNewSession()) {
pageViewTracker.setIsEntrances();
StorageUtil.clearPageInfo();
Expand Down
8 changes: 7 additions & 1 deletion src/util/StorageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ export class StorageUtil {
if (sessionStr === null) {
return null;
}
return JSON.parse(sessionStr) as Session;
const sessionObject = JSON.parse(sessionStr);
return new Session(
sessionObject.sessionId,
sessionObject.sessionIndex,
sessionObject.startTime,
sessionObject.pauseTime
);
}

static getIsFirstOpen(): boolean {
Expand Down
24 changes: 23 additions & 1 deletion test/tracker/SessionTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
Event,
EventRecorder,
} from '../../src/provider';
import { PageViewTracker, SessionTracker } from '../../src/tracker';
import { PageViewTracker, SessionTracker, Session } from '../../src/tracker';
import { StorageUtil } from '../../src/util/StorageUtil';
import { setPerformanceEntries } from '../browser/BrowserUtil';
import { MockObserver } from '../browser/MockObserver';
Expand Down Expand Up @@ -96,6 +96,16 @@ describe('SessionTracker test', () => {
expect(session.isNewSession()).toBeTruthy();
});

test('test first open and session start event has the same sessionId', () => {
sessionTracker.setUp();
const allEvents = JSON.parse(StorageUtil.getAllEvents() + ']');
const firstOpenEvent = allEvents[0];
const sessionStartEvent = allEvents[1];
expect(
firstOpenEvent.sessionId === sessionStartEvent.sessionId
).toBeTruthy();
});

test('test enable app start', () => {
provider.configuration.isTrackAppStartEvents = true;
sessionTracker.setUp();
Expand Down Expand Up @@ -235,6 +245,18 @@ describe('SessionTracker test', () => {
expect(trackPageViewMock).toBeCalled();
});

test('test launch the app use the stored session', () => {
const session = Session.createSession(context.userUniqueId, 2);
session.pauseTime = new Date().getTime() - 1100;
StorageUtil.saveSession(session);
sessionTracker.setUp();
expect(sessionTracker.session.startTime).toBe(session.startTime);
expect(sessionTracker.session.sessionId).toBe(session.sessionId);
expect(sessionTracker.session.sessionIndex).toBe(session.sessionIndex);
expect(sessionTracker.session.pauseTime > 1000).toBeTruthy();
expect(sessionTracker.session.isNewSession()).toBeFalsy();
});

test('test send event in batch mode when hide page', async () => {
const sendEventBackgroundMock = jest.spyOn(
eventRecorder,
Expand Down

0 comments on commit 38fcd8c

Please sign in to comment.