forked from taboola/analytics.js-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomerio.js
189 lines (161 loc) · 4.99 KB
/
customerio.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
describe('Customer.io', function () {
var analytics = require('analytics');
var assert = require('assert');
var Customerio = require('integrations/lib/customerio');
var equal = require('equals');
var sinon = require('sinon');
var test = require('integration-tester');
var customerio;
var settings = {
siteId: 'x'
};
beforeEach(function () {
analytics.use(Customerio);
customerio = new Customerio.Integration(settings);
customerio.initialize(); // noop
});
afterEach(function () {
customerio.reset();
analytics.user().reset();
});
it('should have the right settings', function () {
test(customerio)
.name('Customer.io')
.assumesPageview()
.readyOnInitialize()
.global('_cio')
.option('siteId', '');
});
describe('#initialize', function () {
beforeEach(function () {
sinon.stub(customerio, 'load');
});
it('should create the window._cio object', function () {
assert(!window._cio);
customerio.initialize();
assert(window._cio);
});
it('should call #load', function () {
customerio.initialize();
assert(customerio.load.called);
});
});
describe('#loaded', function () {
it('should test window._cio.pageHasLoaded', function () {
window._cio = [];
assert(!customerio.loaded());
window._cio.pageHasLoaded = true;
assert(customerio.loaded());
});
});
describe('#load', function () {
beforeEach(function () {
sinon.stub(customerio, 'load');
customerio.initialize();
customerio.load.restore();
});
it('should change loaded state', function (done) {
assert(!customerio.loaded());
customerio.load(function (err) {
if (err) return done(err);
assert(customerio.loaded());
done();
});
});
});
describe('#identify', function () {
beforeEach(function (done) {
customerio.initialize();
customerio.once('load', function () {
window._cio.identify = sinon.spy();
setTimeout(done, 50);
});
});
it('should send an id', function () {
test(customerio).identify('id', {});
assert(window._cio.identify.calledWith({ id: 'id' }));
});
it('should not send only traits', function () {
test(customerio).identify(null, { trait: true });
assert(!window._cio.identify.called);
});
it('should send an id and traits', function () {
test(customerio).identify('id', { trait: true });
assert(window._cio.identify.calledWith({ id: 'id', trait: true }));
});
it('should convert dates', function () {
var date = new Date();
test(customerio).identify('id', { date: date });
assert(window._cio.identify.calledWith({
id: 'id',
date: Math.floor(date / 1000)
}));
});
it('should alias created to created_at', function () {
var date = new Date();
test(customerio).identify('id', { created: date });
assert(window._cio.identify.calledWith({
id: 'id',
created_at: Math.floor(date / 1000)
}));
});
});
describe('#group', function () {
beforeEach(function (done) {
analytics.user().identify('id');
customerio.initialize();
customerio.once('load', function () {
window._cio.identify = sinon.spy();
setTimeout(done, 50);
});
});
it('should send an id', function () {
test(customerio).group('id', {});
assert(window._cio.identify.calledWith({ id: 'id', 'Group id': 'id' }));
});
it('should send traits', function () {
test(customerio).group(null, { trait: true });
assert(window._cio.identify.calledWith({ id: 'id', 'Group trait': true }));
});
it('should send an id and traits', function () {
test(customerio).group('id', { trait: true });
assert(window._cio.identify.calledWith({
id: 'id',
'Group id': 'id',
'Group trait': true
}));
});
it('should convert dates', function () {
var date = new Date();
test(customerio).group(null, { date: date });
assert(window._cio.identify.calledWith({
id: 'id',
'Group date': Math.floor(date / 1000)
}));
});
});
describe('#track', function () {
beforeEach(function (done) {
customerio.initialize();
customerio.once('load', function () {
window._cio.track = sinon.spy();
setTimeout(done, 50);
});
});
it('should send an event', function () {
test(customerio).track('event', {});
assert(window._cio.track.calledWith('event'));
});
it('should send an event and properties', function () {
test(customerio).track('event', { property: true });
assert(window._cio.track.calledWith('event', { property: true }));
});
it('should convert dates', function () {
var date = new Date();
test(customerio).track('event', { date: date });
assert(window._cio.track.calledWith('event', {
date: Math.floor(date / 1000)
}));
});
});
});