Repository navigation
Expand file tree
/
Copy pathadplusAnalyticsAdapter_spec.js
More file actions
295 lines (258 loc) · 8.11 KB
/
Copy pathadplusAnalyticsAdapter_spec.js
File metadata and controls
295 lines (258 loc) · 8.11 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
import adplusAnalyticsAdapter from 'modules/adplusAnalyticsAdapter.js';
import { expect } from 'chai';
import adapterManager from 'src/adapterManager.js';
import { server } from 'test/mocks/xhr.js';
import { EVENTS } from 'src/constants.js';
import sinon from 'sinon';
import * as refererDetection from 'src/refererDetection.js';
let events = require('src/events');
describe('AdPlus analytics adapter', function () {
let sandbox, clock;
beforeEach(function () {
sandbox = sinon.createSandbox();
sandbox.spy(console, 'log');
sandbox.stub(refererDetection, 'getRefererInfo').returns(mockRefererInfo);
clock = sandbox.useFakeTimers();
sandbox.stub(events, 'getEvents').returns([]);
adapterManager.enableAnalytics({ provider: 'adplus' });
});
afterEach(function () {
sandbox.restore();
adplusAnalyticsAdapter.reset();
});
const auctionId = 'test-auction-123';
const mockRefererInfo = {
page: 'https://test-site.com/page.html',
domain: 'test-site.com',
ref: 'https://google.com'
};
const bidsReceived = [
{
bidderCode: 'adplus',
auctionId,
adUnitCode: 'adunit-1',
cpm: 5,
currency: 'USD',
size: '300x250',
width: 300,
height: 250,
creativeId: 'crea-1',
originalCpm: 5.5,
originalCurrency: 'EUR',
timeToRespond: 120,
netRevenue: true,
dealId: null,
adId: 'ad-id-1',
adUnitId: 'ad-unit-id-1',
requestId: 'request-id-1',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-1'
},
{
bidderCode: 'adplus',
auctionId,
adUnitCode: 'adunit-2',
cpm: 7,
currency: 'USD',
size: '728x90',
width: 728,
height: 90,
creativeId: 'crea-2',
originalCpm: 7.5,
originalCurrency: 'EUR',
timeToRespond: 110,
netRevenue: true,
dealId: 'deal123',
adId: 'ad-id-2',
adUnitId: 'ad-unit-id-2',
requestId: 'request-id-2',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-2'
}
];
const bidWon1 = {
auctionId,
adUnitCode: 'adunit-1',
bidderCode: 'adplus',
cpm: 5,
currency: 'USD',
size: '300x250',
width: 300,
height: 250,
creativeId: 'crea-1',
originalCpm: 5.5,
originalCurrency: 'EUR',
timeToRespond: 120,
netRevenue: true,
dealId: null,
adId: 'ad-id-1',
adUnitId: 'ad-unit-id-1',
requestId: 'request-id-1',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-1'
};
const bidWon2 = {
auctionId,
adUnitCode: 'adunit-2',
bidderCode: 'adplus',
cpm: 7,
currency: 'USD',
size: '728x90',
width: 728,
height: 90,
creativeId: 'crea-2',
originalCpm: 7.5,
originalCurrency: 'EUR',
timeToRespond: 110,
netRevenue: true,
dealId: 'deal123',
adId: 'ad-id-2',
adUnitId: 'ad-unit-id-2',
requestId: 'request-id-2',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-2'
};
it('should store bids on AUCTION_END and not send immediately', function () {
events.emit(EVENTS.AUCTION_END, {
auctionId,
bidsReceived
});
expect(server.requests.length).to.equal(0);
const storedData = adplusAnalyticsAdapter.auctionBids[auctionId];
expect(storedData).to.exist;
expect(Object.keys(storedData)).to.have.length(2);
expect(storedData['adunit-1'][0]).to.include({
auctionId,
adUnitCode: 'adunit-1',
bidder: 'adplus',
cpm: 5,
currency: 'USD',
originalCpm: 5.5,
originalCurrency: 'EUR',
adId: 'ad-id-1',
adUnitId: 'ad-unit-id-1',
requestId: 'request-id-1',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-1'
});
expect(storedData['adunit-1'][0].adId).to.be.a('string');
expect(storedData['adunit-1'][0].adUnitId).to.be.a('string');
expect(storedData['adunit-1'][0].requestId).to.be.a('string');
expect(storedData['adunit-1'][0].transactionId).to.be.a('string');
});
it('should batch BID_WON events and send after delay with retries', function (done) {
// First, send AUCTION_END to prepare data
events.emit(EVENTS.AUCTION_END, { auctionId, bidsReceived });
// Emit first BID_WON - should send immediately
events.emit(EVENTS.BID_WON, bidWon1);
clock.tick(0);
expect(server.requests.length).to.equal(1);
// Fail first request, triggers retry after 200ms
server.requests[0].respond(500, {}, 'Internal Server Error');
clock.tick(200);
expect(server.requests.length).to.equal(2);
// Fail second (retry) request, triggers next retry
server.requests[1].respond(500, {}, 'Internal Server Error');
clock.tick(200);
expect(server.requests.length).to.equal(3);
// Succeed on third retry
server.requests[2].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' }));
// Now emit second BID_WON - queue is empty, should send immediately
events.emit(EVENTS.BID_WON, bidWon2);
// Should wait 200ms after emit.
expect(server.requests.length).to.equal(3);
// Sends the second BID_WON data after 200ms
clock.tick(200);
expect(server.requests.length).to.equal(4);
// Succeed second BID_WON send
server.requests[3].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' }));
// Validate payloads
const payload1 = JSON.parse(server.requests[0].requestBody);
const payload2 = JSON.parse(server.requests[3].requestBody);
expect(payload1).to.include({
pageUrl: 'https://test-site.com/page.html',
domain: 'test-site.com',
referrer: 'https://google.com'
});
expect(payload2).to.include({
pageUrl: 'https://test-site.com/page.html',
domain: 'test-site.com',
referrer: 'https://google.com'
});
expect(payload1.winningBid).to.include({
auctionId,
adUnitCode: 'adunit-1',
bidder: 'adplus',
cpm: 5,
originalCpm: 5.5,
originalCurrency: 'EUR',
adId: 'ad-id-1',
adUnitId: 'ad-unit-id-1',
requestId: 'request-id-1',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-1'
});
expect(payload1.winningBid.adId).to.be.a('string');
expect(payload1.winningBid.adUnitId).to.be.a('string');
expect(payload1.winningBid.requestId).to.be.a('string');
expect(payload1.winningBid.transactionId).to.be.a('string');
expect(payload1.allBids[0]).to.include({
adId: 'ad-id-1',
adUnitId: 'ad-unit-id-1',
requestId: 'request-id-1',
originalCpm: 5.5,
originalCurrency: 'EUR',
mediaType: 'banner',
transactionId: 'transaction-id-1'
});
expect(payload2.winningBid).to.include({
auctionId,
adUnitCode: 'adunit-2',
bidder: 'adplus',
cpm: 7,
originalCpm: 7.5,
originalCurrency: 'EUR',
adId: 'ad-id-2',
adUnitId: 'ad-unit-id-2',
requestId: 'request-id-2',
instl: 0,
mediaType: 'banner',
transactionId: 'transaction-id-2'
});
expect(payload2.allBids[0]).to.include({
adId: 'ad-id-2',
adUnitId: 'ad-unit-id-2',
requestId: 'request-id-2',
originalCpm: 7.5,
originalCurrency: 'EUR',
mediaType: 'banner',
transactionId: 'transaction-id-2'
});
done();
});
it('should skip BID_WON if no auction data available', function () {
// Emit BID_WON without AUCTION_END first
expect(() => events.emit(EVENTS.BID_WON, bidWon1)).to.not.throw();
// No ajax call since no auctionData
expect(server.requests.length).to.equal(0);
});
it('should not fall back to the page URL when referrer is unavailable', function () {
refererDetection.getRefererInfo.restore();
sandbox.stub(refererDetection, 'getRefererInfo').returns({
page: 'https://test-site.com/page.html',
domain: 'test-site.com',
ref: ''
});
events.emit(EVENTS.AUCTION_END, { auctionId, bidsReceived });
events.emit(EVENTS.BID_WON, bidWon1);
clock.tick(0);
const payload = JSON.parse(server.requests[0].requestBody);
expect(payload.referrer).to.not.equal(payload.pageUrl);
});
});