Repository navigation
Expand file tree
/
Copy pathanalytics.spec.js
More file actions
444 lines (351 loc) · 12.7 KB
/
Copy pathanalytics.spec.js
File metadata and controls
444 lines (351 loc) · 12.7 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
'use strict';
/**
* Here we test the Analytics service. Everything is fine, coverage is 100%.
* See the next describe block for comments on what happens when I try to use
* shared constants
*/
describe('Service: Analytics', function () {
var Analytics,
CookieStore,
md5Mock,
$mockWindow;
beforeEach(module('flAnalytics', function($provide) {
// We use our shared mock services
$mockWindow = new WindowMock();
CookieStore = new CookieStoreMock();
md5Mock = jasmine.createSpyObj('md5', ['createHash']);
$provide.value('$window', $mockWindow);
$provide.value('CookieStore', CookieStore);
$provide.value('md5', md5Mock);
}));
beforeEach(inject(function (_Analytics_) {
Analytics = _Analytics_;
// Set some cookies to be used later
CookieStore.put('GETAFREE_AFPRO', '111');
CookieStore.put('GETAFREE_AFPRO_ID', '222');
CookieStore.put('GETAFREE_AFPRO_SESS', '333');
CookieStore.put('GETAFREE_TTREF', '444');
CookieStore.put('_tracking_session', 'blahblahblah');
}));
it('should have a shared mock $window and CookieStore',
inject(function ($window, CookieStore) {
expect(CookieStore).not.toBeNull();
expect($window).not.toBeNull();
}));
it('should track an action', inject(function ($window) {
Analytics.trackAction('category', 'action', 'result');
expect($window.dataLayer).toContain({
event: 'app_action',
eventCategory: 'category',
eventAction: 'action',
eventResult: 'result'
});
}));
it('should track an event', inject(function ($window) {
Analytics.trackEvent('section', 'subsection', 'name', 'action', 'value',
'label', 'reference', 'referenceId');
expect($window.dataLayer).toContain({
event: 'user_action',
eventSection: 'section',
eventSubsection: 'subsection',
eventName: 'name',
eventAction: 'action',
eventValue: 'value',
eventLabel: 'label',
eventReference: 'reference',
eventReferenceId: 'referenceId'
});
}));
it('should push user timing data to Google Tag Manager',
inject(function ($window) {
Analytics.trackAjaxTiming('method', 'url', 'status', 'timespent', 'label');
expect($window.dataLayer).toContain({
event: 'ajax_timing',
ajaxMethod: 'method',
ajaxUrl: 'url',
ajaxStatus: 'status',
ajaxValue: 'timespent',
ajaxLabel: 'label'
});
}));
it('should track an abtest',
inject(function ($window) {
Analytics.trackABTest('name', 0.5);
expect($window.dataLayer).toContain({
event: 'abtest',
testName: 'name',
testVariation: 0.5
});
}));
it('should track user timing in mobile web', inject(function($window) {
var markName = 'markName';
var markTime = 1234.56;
Analytics.trackMobileUserTiming(markName, markTime);
expect($window.dataLayer).toContain({
event: 'mobile_user_timing',
markName: markName,
markTime: markTime
});
}));
it('should track country domain data in mobile web',
inject(function($window) {
var currentDomain = 'freelancer.com';
Analytics.trackMobileCountryDomain(currentDomain);
expect($window.dataLayer).toContain({
currentDomain: currentDomain
});
}));
describe('on tracking GTM conversions', function() {
var orderId;
var conversionType;
beforeEach(function() {
orderId = 1231;
conversionType = 'Test Posted';
});
it('should push tracking for projects', inject(function($window) {
var type = 'project';
$window.dataLayer = [];
Analytics.trackGTMConversion(type, orderId, conversionType);
expect($window.dataLayer).toEqual([{ order_id: 'P' + orderId },
{ event: conversionType }, jasmine.any(Object)]);
}));
it('should push tracking for contests', inject(function($window) {
var type = 'contest';
$window.dataLayer = [];
Analytics.trackGTMConversion(type, orderId, conversionType);
expect($window.dataLayer).toEqual([{ order_id: 'C' + orderId },
{ event: conversionType }, jasmine.any(Object)]);
}));
it('should send track events once done', function() {
var type = 'project';
Analytics.trackEvent = jasmine.createSpy();
Analytics.trackGTMConversion(type, orderId, conversionType);
expect(Analytics.trackEvent).toHaveBeenCalledWith('GTMTracking',
conversionType, false, false, false, 'P' + orderId);
});
});
it('should track the referrer of the page', inject(function($window) {
var url = '/some/route';
var referrerUrl = '/some/referrer';
Analytics.trackPageView(url, referrerUrl);
expect($window.dataLayer).toContain({
event: 'page_view',
referrerUrl: referrerUrl,
url: url
});
}));
it('should track the location', inject(function ($window) {
var latitude = 14.553913500000002;
var longitude = 121.0506986;
Analytics.trackLocation(
latitude,
longitude
);
expect($window.dataLayer).toContain({
event: 'user_location',
lat: latitude,
lon: longitude
});
}));
it('should get the affiliate program tracking data',
inject(function ($window, CookieStore) {
// When cookies are set
expect(Analytics.getAFPROHeader())
.toEqual('afpro=111;afpro_id=222;afro_sess=333');
// When any of the cookies is missing
CookieStore.remove('GETAFREE_AFPRO');
expect(Analytics.getAFPROHeader()).toBeUndefined();
}));
it('should get the tracking data', inject(function ($window, CookieStore) {
// When cookies are set
expect(Analytics.getTrackingHeader()).toEqual('blahblahblah');
// When cookie is missing
CookieStore.remove('_tracking_session');
expect(Analytics.getTrackingHeader()).toBeUndefined();
}));
it('should set the tracking header from md5 generated hash',
inject(function ($window, CookieStore) {
md5Mock.createHash.and.returnValue('randomhashgeneratedbynesiri');
var hashedId = 'randomha-shge-nera-tedb-ynesiri';
CookieStore.remove('_tracking_session');
CookieStore.put = jasmine.createSpy();
var session = Analytics.setTrackingHeader();
expect(CookieStore.put).toHaveBeenCalledWith('_tracking_session', hashedId,
jasmine.any(Object));
expect(session).toBe(hashedId);
}));
it('should set the tracking header from existing cookie hash value',
inject(function ($window, CookieStore) {
CookieStore.put = jasmine.createSpy();
var session = Analytics.setTrackingHeader();
expect(CookieStore.put).not.toHaveBeenCalled();
expect(session).toBe('blahblahblah');
}));
it('should get the TTREF tracking data',
inject(function ($window, CookieStore) {
// When cookies are set
expect(Analytics.getTTREFHeader()).toEqual('444');
// When cookie is missing
CookieStore.remove('GETAFREE_TTREF');
expect(Analytics.getTTREFHeader()).toBeUndefined();
}));
});
describe('AjaxTimingInterceptor', function() {
var AjaxTimingInterceptor,
mockAnalytics,
$mockWindow,
CookieStore;
// Provide dependencies to the service
beforeEach(module('flAnalytics', function($provide) {
$mockWindow = new WindowMock();
CookieStore = new CookieStoreMock();
mockAnalytics = new AnalyticsMock();
$provide.value('$window', $mockWindow);
$provide.value('Analytics', mockAnalytics);
}));
// Inject the service
beforeEach(inject(function(_AjaxTimingInterceptor_) {
AjaxTimingInterceptor = _AjaxTimingInterceptor_;
}));
it('should intercept requests, responses and responseErrors',
inject(function($q, Analytics, $rootScope, API_BASE_URL, GAF_BASE_URL,
AUTH_BASE_URL) {
var requestConfig = {url: '/views/'};
expect(AjaxTimingInterceptor.request(requestConfig))
.toEqual(requestConfig);
// AjaxTimingInterceptor.request
requestConfig.url = '/data/';
expect(AjaxTimingInterceptor.request(requestConfig))
.toEqual(requestConfig);
requestConfig.url = API_BASE_URL;
expect(AjaxTimingInterceptor.request(requestConfig))
.toEqual(requestConfig);
requestConfig.url = AUTH_BASE_URL;
expect(AjaxTimingInterceptor.request(requestConfig))
.toEqual(requestConfig);
requestConfig.url = GAF_BASE_URL;
expect(AjaxTimingInterceptor.request(requestConfig))
.toEqual(requestConfig);
// AjaxTimingInterceptor.response
var responseConfig = {config: {url: '/views/'}};
expect(AjaxTimingInterceptor.response(responseConfig))
.toEqual(responseConfig);
responseConfig.config.url = '/data/';
expect(AjaxTimingInterceptor.response(responseConfig))
.toEqual(responseConfig);
responseConfig.config.url = API_BASE_URL;
expect(AjaxTimingInterceptor.response(responseConfig))
.toEqual(responseConfig);
responseConfig.config.url = AUTH_BASE_URL;
expect(AjaxTimingInterceptor.response({config: {url: AUTH_BASE_URL}}))
.toEqual(responseConfig);
responseConfig.config.url = GAF_BASE_URL;
expect(AjaxTimingInterceptor.response({config: {url: GAF_BASE_URL}}))
.toEqual(responseConfig);
// AjaxTimingInterceptor.responseError
var responseErrorConfig = {url: 'responseError'};
expect(AjaxTimingInterceptor.request(responseErrorConfig))
.toEqual(responseErrorConfig);
responseErrorConfig = {config: responseErrorConfig};
// Set some handlers functions for our promise so we can spy on them
var handlers = {
onResolve: function () {},
onReject: function () {}
};
spyOn(handlers, 'onResolve');
spyOn(handlers, 'onReject');
AjaxTimingInterceptor.responseError(responseErrorConfig)
.then(function() {
handlers.onResolve();
}, function() {
handlers.onReject();
}
);
// Before digest
expect(handlers.onResolve).not.toHaveBeenCalled();
expect(handlers.onReject).not.toHaveBeenCalled();
$rootScope.$apply();
// After digest
expect(handlers.onResolve).not.toHaveBeenCalled();
expect(handlers.onReject).toHaveBeenCalled();
}));
});
describe('AFPROInterceptor', function() {
var AFPROInterceptor,
$mockWindow,
$mockAnalytics;
beforeEach(module('flAnalytics', function($provide) {
$mockWindow = new WindowMock();
$mockAnalytics = new AnalyticsMock();
$provide.value('$window', $mockWindow);
$provide.value('Analytics', $mockAnalytics);
}));
beforeEach(inject(function(_AFPROInterceptor_) {
AFPROInterceptor = _AFPROInterceptor_;
}));
it('should intercept requests', inject(function(API_BASE_URL) {
var requestConfig = {
url: API_BASE_URL + '/users/0.1/users/',
headers: {}
};
expect(AFPROInterceptor.request(requestConfig).headers)
.not.toBeUndefined();
expect(AFPROInterceptor.request(requestConfig).headers['Freelancer-AFPRO'])
.not.toBeUndefined();
expect(AFPROInterceptor.request(requestConfig).headers['Freelancer-AFPRO'])
.toEqual('mockAFPROHeader');
}));
});
describe('TrackingInterceptor', function() {
var TrackingInterceptor,
$mockWindow,
$mockAnalytics;
beforeEach(module('flAnalytics', function($provide) {
$mockWindow = new WindowMock();
$mockAnalytics = new AnalyticsMock();
$provide.value('$window', $mockWindow);
$provide.value('Analytics', $mockAnalytics);
}));
beforeEach(inject(function(_TrackingInterceptor_) {
TrackingInterceptor = _TrackingInterceptor_;
}));
it('should intercept requests', inject(function(API_BASE_URL) {
var requestConfig = {
url: API_BASE_URL,
headers: {}
};
expect(TrackingInterceptor.request(requestConfig).headers)
.not.toBeUndefined();
expect(TrackingInterceptor.request(requestConfig)
.headers['Freelancer-Tracking']).not.toBeUndefined();
expect(TrackingInterceptor.request(requestConfig)
.headers['Freelancer-Tracking']).toEqual('mockTrackingHeader');
}));
});
describe('TTREFInterceptor', function() {
var TTREFInterceptor,
$mockWindow,
$mockAnalytics;
beforeEach(module('flAnalytics', function($provide) {
$mockWindow = new WindowMock();
$mockAnalytics = new AnalyticsMock();
$provide.value('$window', $mockWindow);
$provide.value('Analytics', $mockAnalytics);
}));
beforeEach(inject(function(_TTREFInterceptor_) {
TTREFInterceptor = _TTREFInterceptor_;
}));
it('should intercept requests', inject(function(API_BASE_URL) {
var requestConfig = {
url: API_BASE_URL + '/projects/0.1/projects/',
headers: {},
method: 'POST'
};
expect(TTREFInterceptor.request(requestConfig).headers)
.not.toBeUndefined();
expect(TTREFInterceptor.request(requestConfig)
.headers['Freelancer-TTREF']).not.toBeUndefined();
expect(TTREFInterceptor.request(requestConfig)
.headers['Freelancer-TTREF']).toEqual('mockTTREFHeader');
}));
});