forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.test.ts
More file actions
278 lines (217 loc) · 9.22 KB
/
Copy pathtracker.test.ts
File metadata and controls
278 lines (217 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2021 Snowplow Analytics Ltd, 2010 Anthon Pang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import { SharedState, addTracker } from '@snowplow/browser-tracker-core';
import F from 'lodash/fp';
jest.useFakeTimers('modern');
const getPPEvents = F.compose(F.filter(F.compose(F.eq('pp'), F.get('evt.e'))), F.first);
const extractSchemas = F.map(F.compose(F.get('data'), (cx: string) => JSON.parse(cx), F.get('evt.co')));
const extractPageId = F.compose(F.get('data[0].data.id'), (cx: string) => JSON.parse(cx), F.get('evt.co'));
describe('Activity tracker behaviour', () => {
beforeAll(() => {
document.domain = 'http://localhost';
});
beforeEach(() => {
jest.clearAllTimers();
});
it('supports different timings for ping vs callback activity tracking', () => {
let callbacks = 0;
const state = new SharedState();
const t =
addTracker('sp1', 'sp1', '', '', state, { stateStorageStrategy: 'cookie' }) ?? fail('Failed to create tracker');
t.enableActivityTracking({ minimumVisitLength: 10, heartbeatDelay: 10 });
t.enableActivityTrackingCallback({
minimumVisitLength: 5,
heartbeatDelay: 5,
callback: () => {
callbacks++;
},
});
t.trackPageView();
jest.advanceTimersByTime(2500);
t.updatePageActivity();
jest.advanceTimersByTime(5000); // CB = 1, PP = 0
// callback timer starts tracking
t.updatePageActivity();
jest.advanceTimersByTime(5000); // CB = 2, PP = 1
// page ping timer starts tracking
t.updatePageActivity();
jest.advanceTimersByTime(5000); // CB = 3, PP = 1
// window for callbacks ticks
t.updatePageActivity();
jest.advanceTimersByTime(5000); // CB = 4, PP = 2
// window for page ping ticks
expect(callbacks).toBe(4);
expect(F.size(getPPEvents(state.outQueues))).toBe(2);
});
it('maintains current static context behaviour', () => {
const state = new SharedState();
const t =
addTracker('sp2', 'sp2', '', '', state, {
resetActivityTrackingOnPageView: false,
stateStorageStrategy: 'cookie',
encodeBase64: false,
}) ?? fail('Failed to create tracker');
t.enableActivityTracking({ minimumVisitLength: 0, heartbeatDelay: 2 });
t.trackPageView({
context: [
{
schema: 'iglu:com.acme/static_context/jsonschema/1-0-0',
data: {
staticValue: Date.now(),
},
},
],
});
const pageOneTime = Date.now();
jest.advanceTimersByTime(500);
t.updatePageActivity();
jest.advanceTimersByTime(2000); // PP = 1
// page two with new static context, time has moved on 2 seconds by now
t.trackPageView({
context: [
{
schema: 'iglu:com.acme/static_context/jsonschema/1-0-0',
data: {
staticValue: Date.now(),
},
},
],
});
const pageTwoTime = Date.now();
jest.advanceTimersByTime(500);
t.updatePageActivity();
jest.advanceTimersByTime(2000); // PP = 2
// current behaviour is to capture context on the first trackPageView after enabling
// event tracking. This might not be ideal, but lets make sure it stays this way
// unless we mean it to change when resetActivityTrackingOnPageView = false.
const findWithStaticValue = F.filter(F.get('data.staticValue'));
const extractContextsWithStaticValue = F.compose(findWithStaticValue, F.flatten, extractSchemas, getPPEvents);
const countWithStaticValueEq = (value: number) =>
F.compose(F.size, F.filter(F.compose(F.eq(value), F.get('data.staticValue'))), extractContextsWithStaticValue);
// we expect there to be two page pings with static contexts attached
// they should both have the time from page one.
expect(countWithStaticValueEq(pageOneTime)(state.outQueues)).toBe(2);
expect(countWithStaticValueEq(pageTwoTime)(state.outQueues)).toBe(0);
});
it('does not reset activity tracking on pageview when resetActivityTrackingOnPageView: false,', () => {
const state = new SharedState();
const t =
addTracker('sp3', 'sp3', '', '', state, {
resetActivityTrackingOnPageView: false,
stateStorageStrategy: 'cookie',
encodeBase64: false,
}) ?? fail('Failed to create tracker');
t.enableActivityTracking({ minimumVisitLength: 0, heartbeatDelay: 30 });
t.trackPageView();
jest.advanceTimersByTime(15000);
// activity on page one
t.updatePageActivity();
jest.advanceTimersByTime(25000);
// activity on page one
t.updatePageActivity();
// shift to page two and trigger tick
t.trackPageView();
jest.advanceTimersByTime(25000);
// Activity tracking is currently not reset per page view so we get an extra page ping on page two.
const pps = getPPEvents(state.outQueues);
expect(F.size(pps)).toBe(2);
});
it('does reset activity tracking on pageview by default', () => {
const state = new SharedState();
const t =
addTracker('sp4', 'sp4', '', '', state, {
stateStorageStrategy: 'cookie',
encodeBase64: false,
}) ?? fail('Failed to create tracker');
t.enableActivityTracking({ minimumVisitLength: 0, heartbeatDelay: 30 });
t.trackPageView();
jest.advanceTimersByTime(15000);
// activity on page one
t.updatePageActivity();
jest.advanceTimersByTime(25000); // PP = 1
// activity on page one
t.updatePageActivity();
// shift to page two and trigger tick
t.trackPageView();
jest.advanceTimersByTime(5000);
// activity on page two
t.updatePageActivity();
jest.advanceTimersByTime(20000);
// Activity began tracking on the first page but moved on before 30 seconds.
// Activity tracking should still not have fire despite being on site 30 seconds, as user has moved page.
const pps = getPPEvents(state.outQueues);
expect(F.size(pps)).toBe(1);
});
it('fires initial delayed activity tracking on first pageview and second pageview', () => {
const state = new SharedState();
const t =
addTracker('sp5', 'sp5', '', '', state, {
stateStorageStrategy: 'cookie',
encodeBase64: false,
}) ?? fail('Failed to create tracker');
t.enableActivityTracking({ minimumVisitLength: 10, heartbeatDelay: 5 });
t.trackPageView();
const firstPageId = t.getPageViewId();
jest.advanceTimersByTime(2500);
// initial callback timer starts tracking
t.updatePageActivity();
jest.advanceTimersByTime(5000);
const initial_pps = getPPEvents(state.outQueues);
expect(F.size(initial_pps)).toBe(0);
jest.advanceTimersByTime(5000); // PP = 1
// page ping timer starts tracking
t.updatePageActivity();
jest.advanceTimersByTime(5000); // PP = 2
t.updatePageActivity();
jest.advanceTimersByTime(5000); // PP = 3
// Move to second page view
t.trackPageView();
const secondPageId = t.getPageViewId();
jest.advanceTimersByTime(2500);
// window for callbacks ticks
t.updatePageActivity();
jest.advanceTimersByTime(5000);
// Should still only have 3 page pings from first page
const first_page_only_pps = getPPEvents(state.outQueues);
expect(F.size(first_page_only_pps)).toBe(3);
jest.advanceTimersByTime(5000); // PP = 4
// window for page ping ticks
t.updatePageActivity();
jest.advanceTimersByTime(5000); // PP = 5
// Activity began tracking on the first page and tracked two page pings in 16 seconds.
// Activity tracking only fires one further event over next 11 seconds as a page view event occurs, resetting timer back to 10 seconds.
const pps = getPPEvents(state.outQueues);
expect(F.size(pps)).toBe(5);
const pph = F.head(pps);
const ppl = F.last(pps);
expect(firstPageId).toBe(extractPageId(pph));
expect(secondPageId).toBe(extractPageId(ppl));
});
});