Skip to content

Commit 33b9812

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: support configure tracking _app_start and _app_end events (#42)
Co-authored-by: xiaoweii <[email protected]>
1 parent 51d5188 commit 33b9812

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ ClickstreamAnalytics.init({
157157
isTrackSearchEvents: true,
158158
isTrackScrollEvents: true,
159159
isTrackPageLoadEvents: true,
160+
isTrackAppStartEvents: true,
161+
isTrackAppEndEvents: true,
160162
pageType: PageType.SPA,
161163
isLogEvents: false,
162164
authCookie: "your auth cookie",
@@ -178,6 +180,8 @@ Here is an explanation of each property:
178180
- **isTrackSearchEvents**: whether auto record search result page events in browser, default is `true`
179181
- **isTrackScrollEvents**: whether auto record page scroll events in browser, default is `true`
180182
- **isTrackPageLoadEvents**: whether auto record page load performance events in browser, default is `false`
183+
- **isTrackAppStartEvents**: whether auto record app start events in browser when pages becomes visible, default is `false`
184+
- **isTrackAppEndEvents**: whether auto record app end events in browser when pages becomes invisible, default is `false`
181185
- **pageType**: the website type, `SPA` for single page application, `multiPageApp` for multiple page application, default is `SPA`. This attribute works only when the attribute `isTrackPageViewEvents`'s value is `true`
182186
- **isLogEvents**: whether to print out event json for debugging, default is false.
183187
- **authCookie**: your auth cookie for AWS application load balancer auth cookie.
@@ -200,6 +204,8 @@ ClickstreamAnalytics.updateConfigure({
200204
isTrackScrollEvents: false,
201205
isTrackSearchEvents: false,
202206
isTrackPageLoadEvents: false,
207+
isTrackAppStartEvents: true,
208+
isTrackAppStartEvents: true,
203209
});
204210
```
205211

src/provider/ClickstreamProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export class ClickstreamProvider implements AnalyticsProvider {
6262
isTrackSearchEvents: true,
6363
isTrackScrollEvents: true,
6464
isTrackPageLoadEvents: false,
65+
isTrackAppStartEvents: false,
66+
isTrackAppEndEvents: false,
6567
pageType: PageType.SPA,
6668
isLogEvents: false,
6769
sessionTimeoutDuration: 1800000,

src/tracker/SessionTracker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class SessionTracker extends BaseTracker {
7171
pageViewTracker.setIsEntrances();
7272
this.provider.record({ name: Event.PresetEvent.SESSION_START });
7373
}
74+
if (!this.provider.configuration.isTrackAppStartEvents) return;
7475
if (isFirstTime && this.isFromCurrentHost()) return;
7576
if (isFirstTime && BrowserInfo.isFromReload()) return;
7677
this.provider.record({
@@ -99,6 +100,7 @@ export class SessionTracker extends BaseTracker {
99100
}
100101

101102
recordAppEnd(isImmediate: boolean) {
103+
if (!this.provider.configuration.isTrackAppEndEvents) return;
102104
this.provider.record({
103105
name: Event.PresetEvent.APP_END,
104106
isImmediate: isImmediate,

src/types/Analytics.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface Configuration {
3232
isTrackScrollEvents?: boolean;
3333
isTrackSearchEvents?: boolean;
3434
isTrackPageLoadEvents?: boolean;
35+
isTrackAppStartEvents?: boolean;
36+
isTrackAppEndEvents?: boolean;
3537
}
3638

3739
export enum SendMode {

test/provider/ImmediateModeCache.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('ImmediateModeCache test', () => {
4444
const failedEvents = JSON.parse(
4545
StorageUtil.getFailedEvents() + Event.Constants.SUFFIX
4646
);
47-
expect(failedEvents.length).toBeGreaterThan(4);
47+
expect(failedEvents.length).toBeGreaterThan(3);
4848
const mockSendRequestSuccess = jest.fn().mockResolvedValue(true);
4949
jest
5050
.spyOn(NetRequest, 'sendRequest')

test/tracker/SessionTracker.test.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ describe('SessionTracker test', () => {
7171
test('test initial state for setUp', () => {
7272
const pageAppearMock = jest.spyOn(sessionTracker, 'onPageAppear');
7373
sessionTracker.setUp();
74+
expect(provider.configuration.isTrackAppStartEvents).toBe(false);
75+
expect(provider.configuration.isTrackAppEndEvents).toBe(false);
7476
expect(StorageUtil.getIsFirstOpen()).toBe(false);
7577

7678
expect(recordMethodMock).toBeCalledWith({
@@ -80,7 +82,7 @@ describe('SessionTracker test', () => {
8082
expect(recordMethodMock).toBeCalledWith({
8183
name: Event.PresetEvent.SESSION_START,
8284
});
83-
expect(recordMethodMock).toBeCalledWith({
85+
expect(recordMethodMock).not.toBeCalledWith({
8486
name: Event.PresetEvent.APP_START,
8587
attributes: {
8688
[Event.ReservedAttribute.IS_FIRST_TIME]: true,
@@ -93,7 +95,19 @@ describe('SessionTracker test', () => {
9395
expect(session.isNewSession()).toBeTruthy();
9496
});
9597

98+
test('test enable app start', () => {
99+
provider.configuration.isTrackAppStartEvents = true;
100+
sessionTracker.setUp();
101+
expect(recordMethodMock).toBeCalledWith({
102+
name: Event.PresetEvent.APP_START,
103+
attributes: {
104+
[Event.ReservedAttribute.IS_FIRST_TIME]: true,
105+
},
106+
});
107+
});
108+
96109
test('test multi page mode record app start when setUp', () => {
110+
provider.configuration.isTrackAppStartEvents = true;
97111
Object.assign(provider.configuration, {
98112
pageType: PageType.multiPageApp,
99113
});
@@ -107,6 +121,7 @@ describe('SessionTracker test', () => {
107121
});
108122

109123
test('test multi page mode not record app start when come from the same host name', () => {
124+
provider.configuration.isTrackAppStartEvents = true;
110125
Object.assign(provider.configuration, {
111126
pageType: PageType.multiPageApp,
112127
});
@@ -220,6 +235,7 @@ describe('SessionTracker test', () => {
220235
});
221236

222237
test('test send event in batch mode when close window', async () => {
238+
provider.configuration.isTrackAppEndEvents = true;
223239
const sendEventBackgroundMock = jest.spyOn(
224240
eventRecorder,
225241
'sendEventsInBackground'
@@ -229,7 +245,6 @@ describe('SessionTracker test', () => {
229245
sessionTracker,
230246
'recordUserEngagement'
231247
);
232-
const recordAppEndMock = jest.spyOn(sessionTracker, 'recordAppEnd');
233248
sessionTracker.setUp();
234249
provider.record({ name: 'testEvent' });
235250
await sleep(100);
@@ -238,7 +253,10 @@ describe('SessionTracker test', () => {
238253
expect(sendEventBackgroundMock).toBeCalledWith(true);
239254
expect(clearAllEventsMock).toBeCalled();
240255
expect(recordUserEngagementMock).toBeCalledWith(true);
241-
expect(recordAppEndMock).toBeCalledWith(true);
256+
expect(recordMethodMock).toBeCalledWith({
257+
name: Event.PresetEvent.APP_END,
258+
isImmediate: true,
259+
});
242260
});
243261

244262
test('test send event in batch mode when hide window in firefox', async () => {
@@ -267,6 +285,7 @@ describe('SessionTracker test', () => {
267285
});
268286

269287
test('test send event in batch mode when close window in firefox', async () => {
288+
provider.configuration.isTrackAppEndEvents = true;
270289
const sendEventBackgroundMock = jest.spyOn(
271290
eventRecorder,
272291
'sendEventsInBackground'
@@ -275,7 +294,6 @@ describe('SessionTracker test', () => {
275294
sessionTracker,
276295
'recordUserEngagement'
277296
);
278-
const recordAppEndMock = jest.spyOn(sessionTracker, 'recordAppEnd');
279297
Object.defineProperty(navigator, 'userAgent', {
280298
writable: true,
281299
value: 'firefox',
@@ -287,7 +305,10 @@ describe('SessionTracker test', () => {
287305
hidePage();
288306
expect(sendEventBackgroundMock).not.toBeCalled();
289307
expect(recordUserEngagementMock).toBeCalledWith(false);
290-
expect(recordAppEndMock).toBeCalledWith(false);
308+
expect(recordMethodMock).not.toBeCalledWith({
309+
name: Event.PresetEvent.APP_END,
310+
isImmediate: true,
311+
});
291312
});
292313

293314
test('test send failed event in immediate mode when hide page', async () => {

0 commit comments

Comments
 (0)