forked from taboola/analytics.js-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
340 lines (298 loc) · 10.7 KB
/
test.js
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
var Analytics = require('analytics.js').constructor;
var integration = require('analytics.js-integration');
var iso = require('to-iso-string');
var sandbox = require('clear-env');
var tester = require('analytics.js-integration-tester');
var Mixpanel = require('./');
describe('Mixpanel', function() {
var analytics;
var mixpanel;
var options = {
token: 'x',
cookieName: 'y',
crossSubdomainCookie: true,
secureCookie: true
};
beforeEach(function() {
analytics = new Analytics();
mixpanel = new Mixpanel(options);
analytics.use(Mixpanel);
analytics.use(tester);
analytics.add(mixpanel);
});
afterEach(function() {
analytics.restore();
analytics.reset();
mixpanel.reset();
sandbox();
});
it('should have the right settings', function() {
analytics.compare(Mixpanel, integration('Mixpanel')
.global('mixpanel')
.option('token', '')
.option('crossSubdomainCookie', false)
.option('secureCookie', false)
.option('nameTag', true)
.option('pageview', false)
.option('people', false)
.option('token', '')
.option('trackAllPages', false)
.option('trackNamedPages', true));
});
describe('before loading', function() {
beforeEach(function() {
analytics.stub(mixpanel, 'load');
});
describe('#initialize', function() {
it('should create window.mixpanel', function() {
analytics.assert(!window.mixpanel);
analytics.initialize();
analytics.page();
analytics.assert(window.mixpanel);
});
it('should call #load', function() {
analytics.initialize();
analytics.page();
analytics.called(mixpanel.load);
});
it('should lowercase increments', function() {
mixpanel.options.increments = ['A', 'b', 'c_'];
analytics.initialize();
analytics.page();
analytics.deepEqual(mixpanel.options.increments, ['a', 'b', 'c_']);
});
});
});
describe('loading', function() {
it('should load', function(done) {
analytics.load(mixpanel, done);
});
});
describe('after loading', function() {
beforeEach(function(done) {
analytics.once('ready', done);
analytics.initialize();
analytics.page();
});
describe('#page', function() {
beforeEach(function() {
analytics.stub(window.mixpanel, 'track');
});
it('should not track anonymous pages by default', function() {
analytics.page();
analytics.didNotCall(window.mixpanel.track);
});
it('should track anonymous pages when the option is on', function() {
mixpanel.options.trackAllPages = true;
analytics.page();
analytics.called(window.mixpanel.track, 'Loaded a Page');
});
it('should track named pages by default', function() {
analytics.page('Name');
analytics.called(window.mixpanel.track, 'Viewed Name Page');
});
it('should track named pages with categories', function() {
analytics.page('Category', 'Name');
analytics.called(window.mixpanel.track, 'Viewed Category Name Page');
});
it('should track categorized pages by default', function() {
analytics.page('Category', 'Name');
analytics.called(window.mixpanel.track, 'Viewed Category Page');
});
it('should not track category pages when the option is off', function() {
mixpanel.options.trackNamedPages = false;
mixpanel.options.trackCategorizedPages = false;
analytics.page('Name');
analytics.page('Category', 'Name');
analytics.didNotCall(window.mixpanel.track);
});
});
describe('#identify', function() {
beforeEach(function() {
analytics.stub(window.mixpanel, 'identify');
analytics.stub(window.mixpanel, 'register');
analytics.stub(window.mixpanel, 'name_tag');
analytics.stub(window.mixpanel.people, 'set');
});
it('should send an id', function() {
analytics.identify('id');
analytics.called(window.mixpanel.identify, 'id');
analytics.called(window.mixpanel.register, { id: 'id' });
});
it('should send traits', function() {
analytics.identify({ trait: true });
analytics.called(window.mixpanel.register, { trait: true });
});
it('should send an id and traits', function() {
analytics.identify('id', { trait: true });
analytics.called(window.mixpanel.identify, 'id');
analytics.called(window.mixpanel.register, { trait: true, id: 'id' });
});
it('should use an id as a name tag', function() {
analytics.identify('id');
analytics.called(window.mixpanel.name_tag, 'id');
});
it('should prefer a username as a name tag', function() {
analytics.identify('id', { username: 'username' });
analytics.called(window.mixpanel.name_tag, 'username');
});
it('should prefer an email as a name tag', function() {
analytics.identify('id', {
username: 'username',
email: '[email protected]'
});
analytics.called(window.mixpanel.name_tag, '[email protected]');
});
it('should send traits to Mixpanel People', function() {
mixpanel.options.people = true;
analytics.identify({ trait: true });
analytics.called(window.mixpanel.people.set, { trait: true });
});
it('should alias traits', function() {
var date = new Date();
analytics.identify({
created: date,
email: '[email protected]',
firstName: 'first',
lastName: 'last',
lastSeen: date,
name: 'name',
username: 'username',
phone: 'phone'
});
analytics.called(window.mixpanel.register, {
$created: iso(date),
$email: '[email protected]',
$first_name: 'first',
$last_name: 'last',
$last_seen: iso(date),
$name: 'name',
$username: 'username',
$phone: 'phone'
});
});
it('should alias traits to Mixpanel People', function() {
mixpanel.options.people = true;
var date = new Date();
analytics.identify({
created: date,
email: '[email protected]',
firstName: 'first',
lastName: 'last',
lastSeen: date,
name: 'name',
username: 'username',
phone: 'phone'
});
analytics.called(window.mixpanel.people.set, {
$created: date,
$email: '[email protected]',
$first_name: 'first',
$last_name: 'last',
$last_seen: date,
$name: 'name',
$username: 'username',
$phone: 'phone'
});
});
it('should remove .created_at', function() {
mixpanel.options.people = true;
var date = new Date();
analytics.identify({
created_at: date,
email: '[email protected]',
firstName: 'first',
lastName: 'last',
lastSeen: date,
name: 'name',
username: 'username',
phone: 'phone'
});
analytics.called(window.mixpanel.people.set, {
$created: date,
$email: '[email protected]',
$first_name: 'first',
$last_name: 'last',
$last_seen: date,
$name: 'name',
$username: 'username',
$phone: 'phone'
});
});
});
describe('#track', function() {
beforeEach(function() {
analytics.stub(window.mixpanel, 'track');
analytics.stub(window.mixpanel.people, 'increment');
analytics.stub(window.mixpanel.people, 'set');
analytics.stub(window.mixpanel.people, 'track_charge');
});
it('should send an event', function() {
analytics.track('event');
analytics.called(window.mixpanel.track, 'event');
});
it('should send an event and properties', function() {
analytics.track('event', { property: true });
analytics.called(window.mixpanel.track, 'event', { property: true });
});
it('should send a revenue property to Mixpanel People', function() {
mixpanel.options.people = true;
analytics.track('event', { revenue: 9.99 });
analytics.called(window.mixpanel.people.track_charge, 9.99);
});
it('should convert dates to iso strings', function() {
var date = new Date();
analytics.track('event', { date: date });
analytics.called(window.mixpanel.track, 'event', { date: iso(date) });
});
it('should increment events that are in .increments option', function() {
mixpanel.options.increments = [0, 'my event', 1];
mixpanel.options.people = true;
analytics.track('my event');
analytics.called(window.mixpanel.people.increment, 'my event');
});
it('should should update people property if the event is in .increments', function() {
mixpanel.options.increments = ['event'];
mixpanel.options.people = true;
analytics.track('event');
var date = window.mixpanel.people.set.args[0][1];
analytics.assert(date.getTime() === new Date().getTime());
analytics.called(window.mixpanel.people.increment, 'event');
analytics.called(window.mixpanel.people.set, 'Last event', date);
});
it('should not convert arrays of simple types', function() {
analytics.track('event', { array: ['a', 'b', 'c'] });
analytics.called(window.mixpanel.track, 'event', { array: ['a', 'b', 'c'] });
analytics.track('event', { array: [13, 28, 99] });
analytics.called(window.mixpanel.track, 'event', { array: [13, 28, 99] });
});
it('should convert arrays of objects to lengths', function() {
analytics.track('event', { array: [{ a: 'asdf' }, { b: 'geasd' }, { c: 'asfee' }] });
analytics.called(window.mixpanel.track, 'event', { array: 3 });
});
it('should remove mixpanel\'s reserved properties', function() {
analytics.track('event', {
distinct_id: 'string',
ip: 'string',
mp_name_tag: 'string',
mp_note: 'string',
token: 'string'
});
analytics.called(window.mixpanel.track, 'event', {});
});
});
describe('#alias', function() {
beforeEach(function() {
analytics.stub(window.mixpanel, 'alias');
});
it('should send a new id', function() {
analytics.alias('new');
analytics.called(window.mixpanel.alias, 'new');
});
it('should send a new and old id', function() {
analytics.alias('new', 'old');
analytics.called(window.mixpanel.alias, 'new', 'old');
});
});
});
});