forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalyticsAdapter_spec.js
189 lines (145 loc) · 6.03 KB
/
AnalyticsAdapter_spec.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
import { expect } from 'chai';
import events from 'src/events.js';
import CONSTANTS from 'src/constants.json';
import { server } from 'test/mocks/xhr.js';
const REQUEST_BIDS = CONSTANTS.EVENTS.REQUEST_BIDS;
const BID_REQUESTED = CONSTANTS.EVENTS.BID_REQUESTED;
const BID_RESPONSE = CONSTANTS.EVENTS.BID_RESPONSE;
const BID_WON = CONSTANTS.EVENTS.BID_WON;
const BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT;
const AD_RENDER_FAILED = CONSTANTS.EVENTS.AD_RENDER_FAILED;
const ADD_AD_UNITS = CONSTANTS.EVENTS.ADD_AD_UNITS;
const AnalyticsAdapter = require('src/AnalyticsAdapter').default;
const config = {
url: 'https://localhost:9999/endpoint',
analyticsType: 'endpoint'
};
describe(`
FEATURE: Analytics Adapters API
SCENARIO: A publisher enables analytics
AND an \`example\` instance of \`AnalyticsAdapter\`\n`, () => {
let adapter;
beforeEach(function () {
adapter = new AnalyticsAdapter(config);
});
afterEach(function () {
adapter.disableAnalytics();
});
it(`SHOULD call the endpoint WHEN an event occurs that is to be tracked`, function () {
const eventType = BID_REQUESTED;
const args = { some: 'data' };
adapter.track({ eventType, args });
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {some: 'data'}, eventType: 'bidRequested'});
});
it(`SHOULD queue the event first and then track it WHEN an event occurs before tracking library is available`, function () {
const eventType = BID_RESPONSE;
const args = { wat: 'wot' };
events.emit(eventType, args);
adapter.enableAnalytics();
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {wat: 'wot'}, eventType: 'bidResponse'});
});
describe(`WHEN an event occurs after enable analytics\n`, function () {
beforeEach(function () {
sinon.stub(events, 'getEvents').returns([]); // these tests shouldn't be affected by previous tests
});
afterEach(function () {
events.getEvents.restore();
});
it('SHOULD call global when a bidWon event occurs', function () {
const eventType = BID_WON;
const args = { more: 'info' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {more: 'info'}, eventType: 'bidWon'});
});
it('SHOULD call global when a adRenderFailed event occurs', function () {
const eventType = AD_RENDER_FAILED;
const args = { call: 'adRenderFailed' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'adRenderFailed'}, eventType: 'adRenderFailed'});
});
it('SHOULD call global when an addAdUnits event occurs', function () {
const eventType = ADD_AD_UNITS;
const args = { call: 'addAdUnits' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'addAdUnits'}, eventType: 'addAdUnits'});
});
it('SHOULD call global when a requestBids event occurs', function () {
const eventType = REQUEST_BIDS;
const args = { call: 'request' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'request'}, eventType: 'requestBids'});
});
it('SHOULD call global when a bidRequest event occurs', function () {
const eventType = BID_REQUESTED;
const args = { call: 'request' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'request'}, eventType: 'bidRequested'});
});
it('SHOULD call global when a bidResponse event occurs', function () {
const eventType = BID_RESPONSE;
const args = { call: 'response' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'response'}, eventType: 'bidResponse'});
});
it('SHOULD call global when a bidTimeout event occurs', function () {
const eventType = BID_TIMEOUT;
const args = { call: 'timeout' };
adapter.enableAnalytics();
events.emit(eventType, args);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'timeout'}, eventType: 'bidTimeout'});
});
it('SHOULD NOT call global again when adapter.enableAnalytics is called with previous timeout', function () {
const eventType = BID_TIMEOUT;
const args = { call: 'timeout' };
events.emit(eventType, args);
adapter.enableAnalytics();
events.emit(eventType, args);
expect(server.requests.length).to.equal(1);
});
describe(`AND sampling is enabled\n`, function () {
const eventType = BID_WON;
const args = { more: 'info' };
beforeEach(function () {
sinon.stub(Math, 'random').returns(0.5);
});
afterEach(function () {
Math.random.restore();
});
it(`THEN should enable analytics when random number is in sample range`, function () {
adapter.enableAnalytics({
options: {
sampling: 0.75
}
});
events.emit(eventType, args);
expect(server.requests.length).to.equal(1);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {more: 'info'}, eventType: 'bidWon'});
});
it(`THEN should disable analytics when random number is outside sample range`, function () {
adapter.enableAnalytics({
options: {
sampling: 0.25
}
});
events.emit(eventType, args);
expect(server.requests.length).to.equal(0);
});
});
});
});