Skip to content

Commit

Permalink
fix: remove redundant start events for multi page mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei authored Oct 27, 2023
1 parent 2eb9c1d commit 617ee9f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/tracker/SessionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BaseTracker } from './BaseTracker';
import { Session } from './Session';
import { BrowserInfo } from '../browser';
import { Event } from '../provider';
import { PageType } from '../types';
import { StorageUtil } from '../util/StorageUtil';

const logger = new Logger('SessionTracker');
Expand Down Expand Up @@ -69,6 +70,8 @@ export class SessionTracker extends BaseTracker {
pageViewTracker.setIsEntrances();
this.provider.record({ name: Event.PresetEvent.SESSION_START });
}
if (isFirstTime && this.isMultiPageApp() && this.isFromCurrentHost())
return;
this.provider.record({
name: Event.PresetEvent.APP_START,
attributes: {
Expand All @@ -77,6 +80,14 @@ export class SessionTracker extends BaseTracker {
});
}

isFromCurrentHost() {
return window.location.host === this.context.browserInfo.latestReferrerHost;
}

isMultiPageApp() {
return this.context.configuration.pageType === PageType.multiPageApp;
}

onPageHide() {
logger.debug('page hide');
this.storeSession();
Expand Down
33 changes: 30 additions & 3 deletions test/tracker/SessionTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

import { JSDOM } from 'jsdom';
import { SendMode } from '../../src';
import { PageType, SendMode } from '../../src';
import { BrowserInfo } from '../../src/browser';
import { NetRequest } from '../../src/network/NetRequest';
import { AnalyticsEventBuilder } from '../../src/provider';
import {
AnalyticsEventBuilder,
ClickstreamContext,
ClickstreamProvider,
Event,
EventRecorder,
} from '../../src/provider';
import { SessionTracker, PageViewTracker } from '../../src/tracker';
import { PageViewTracker, SessionTracker } from '../../src/tracker';
import { StorageUtil } from '../../src/util/StorageUtil';

describe('SessionTracker test', () => {
Expand Down Expand Up @@ -89,6 +89,33 @@ describe('SessionTracker test', () => {
expect(session.isNewSession()).toBeTruthy();
});

test('test multi page mode record app start when setUp', () => {
Object.assign(provider.configuration, {
pageType: PageType.multiPageApp,
});
sessionTracker.setUp();
expect(recordMethodMock).toBeCalledWith({
name: Event.PresetEvent.APP_START,
attributes: {
[Event.ReservedAttribute.IS_FIRST_TIME]: true,
},
});
});

test('test multi page mode not record app start when come from the same host name', () => {
Object.assign(provider.configuration, {
pageType: PageType.multiPageApp,
});
context.browserInfo.latestReferrerHost = 'localhost';
sessionTracker.setUp();
expect(recordMethodMock).not.toBeCalledWith({
name: Event.PresetEvent.APP_START,
attributes: {
[Event.ReservedAttribute.IS_FIRST_TIME]: true,
},
});
});

test('test setUp for unsupported env', () => {
const addEventListenerMock = jest.spyOn(window, 'addEventListener');
const addEventListener = (global as any).document.addEventListener;
Expand Down

0 comments on commit 617ee9f

Please sign in to comment.