This repository was archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.test.js
193 lines (171 loc) · 6.57 KB
/
index.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
'use strict';
var Analytics = require('@segment/analytics.js-core').constructor;
var integration = require('@segment/analytics.js-integration');
var sandbox = require('@segment/clear-env');
var tester = require('@segment/analytics.js-integration-tester');
var GTM = require('../lib/');
describe('Google Tag Manager', function() {
var analytics;
var gtm;
var options = {
containerId: 'GTM-M8M29T',
environment: ''
};
beforeEach(function() {
analytics = new Analytics();
gtm = new GTM(options);
analytics.use(GTM);
analytics.use(tester);
analytics.add(gtm);
});
afterEach(function() {
analytics.restore();
analytics.reset();
gtm.reset();
sandbox();
});
it('should store the correct settings', function() {
analytics.compare(GTM, integration('Google Tag Manager')
.global('dataLayer')
.option('containerId', '')
.option('environment', '')
.option('trackAllPages', false)
.option('trackNamedPages', true)
.option('trackCategorizedPages', true));
});
describe('loading', function() {
it('should load', function(done) {
analytics.load(gtm, done);
});
});
describe('after loading', function() {
beforeEach(function(done) {
options = {
containerId: 'GTM-M8M29T',
environment: ''
};
analytics.once('ready', done);
analytics.initialize();
analytics.page();
});
it('should push initial gtm.start event', function() {
var dl = window.dataLayer;
analytics.assert(dl);
analytics.assert(dl[0].event === 'gtm.js');
analytics.assert(typeof dl[0]['gtm.start'] === 'number');
});
describe('#track', function() {
beforeEach(function() {
analytics.stub(window.dataLayer, 'push');
});
it('should send event', function() {
var anonId = analytics.user().anonymousId();
analytics.track('some-event');
analytics.called(window.dataLayer.push, { segmentAnonymousId: anonId, event: 'some-event' });
});
it('should send userId if it exists', function() {
analytics.user().id('pablo');
var anonId = analytics.user().anonymousId();
analytics.track('some-event');
analytics.called(window.dataLayer.push, { segmentAnonymousId: anonId, userId: 'pablo', event: 'some-event' });
});
it('should send anonymousId if it exists', function() {
analytics.user().anonymousId('el');
analytics.track('stranger things');
analytics.called(window.dataLayer.push, { segmentAnonymousId: 'el', event: 'stranger things' });
});
it('should send event with properties', function() {
var anonId = analytics.user().anonymousId();
analytics.track('event', { prop: true });
analytics.called(window.dataLayer.push, { segmentAnonymousId: anonId, event: 'event', prop: true });
});
});
describe('#page', function() {
beforeEach(function() {
analytics.stub(window.dataLayer, 'push');
});
it('should not track unamed pages by default', function() {
analytics.page();
analytics.didNotCall(window.dataLayer.push);
});
it('should track unamed pages if enabled', function() {
gtm.options.trackAllPages = true;
var anonId = analytics.user().anonymousId();
analytics.page();
analytics.called(window.dataLayer.push, {
event: 'Loaded a Page',
segmentAnonymousId: anonId,
path: window.location.pathname,
referrer: document.referrer,
title: document.title,
search: window.location.search,
url: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname
});
});
it('should track named pages by default', function() {
var anonId = analytics.user().anonymousId();
analytics.page('Name');
analytics.called(window.dataLayer.push, {
event: 'Viewed Name Page',
segmentAnonymousId: anonId,
name: 'Name',
path: window.location.pathname,
referrer: document.referrer,
title: document.title,
search: window.location.search,
url: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname
});
});
it('should track named pages with a category added', function() {
var anonId = analytics.user().anonymousId();
analytics.page('Category', 'Name');
analytics.called(window.dataLayer.push, {
event: 'Viewed Category Name Page',
segmentAnonymousId: anonId,
category: 'Category',
name: 'Name',
path: window.location.pathname,
referrer: document.referrer,
title: document.title,
search: window.location.search,
url: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname
});
});
it('should track categorized pages by default', function() {
var anonId = analytics.user().anonymousId();
analytics.page('Category', 'Name');
analytics.called(window.dataLayer.push, {
event: 'Viewed Category Name Page',
category: 'Category',
segmentAnonymousId: anonId,
name: 'Name',
path: window.location.pathname,
referrer: document.referrer,
title: document.title,
search: window.location.search,
url: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '') + window.location.pathname
});
});
it('should not track name or categorized pages if disabled', function() {
gtm.options.trackNamedPages = false;
gtm.options.trackCategorizedPages = false;
analytics.page('Name');
analytics.page('Category', 'Name');
analytics.didNotCall(window.dataLayer.push);
});
});
});
describe('environment options', function() {
it('should use the right tag if the environment option is set', function() {
gtm.options = {
containerId: 'GTM-M8M29T',
environment: 'test'
};
var tag = '<script src="http://www.googletagmanager.com/gtm.js?id=' + gtm.options.containerId + '&l=dataLayer>m_preview=' + gtm.options.environment + '">';
analytics.spy(gtm, 'load');
analytics.initialize();
analytics.page();
analytics.loaded(tag);
});
});
});