Skip to content

Commit 31751eb

Browse files
committed
Merged in pcman@code_cleanup (pull request prebid#24)
Pcman@code cleanup Approved-by: Yuan-Hung Huang <[email protected]>
2 parents dc65a13 + 3bce50b commit 31751eb

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

modules/appierAnalyticsAdapter.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
9494
contentType: 'application/json'
9595
});
9696
},
97-
newCommonMessageBody(auctionId) {
97+
createCommonMessage(auctionId) {
9898
return {
9999
version: ANALYTICS_VERSION,
100100
auctionId: auctionId,
@@ -126,9 +126,16 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
126126
}
127127
return result;
128128
},
129-
newAuctionMessageBody(auctionEndArgs, winningBids, timeoutBids) {
129+
addBidResponseToMessage(message, bid, status) {
130+
const adUnitCode = parseAdUnitCode(bid);
131+
message.adUnits[adUnitCode] = message.adUnits[adUnitCode] || {};
132+
const bidder = parseBidderCode(bid);
133+
const bidResponse = this.serializeBidResponse(bid, status);
134+
message.adUnits[adUnitCode][bidder] = bidResponse;
135+
},
136+
createBidMessage(auctionEndArgs, winningBids, timeoutBids) {
130137
const {auctionId, timestamp, timeout, auctionEnd, adUnitCodes, bidsReceived, noBids} = auctionEndArgs;
131-
const message = this.newCommonMessageBody(auctionId);
138+
const message = this.createCommonMessage(auctionId);
132139

133140
message.auctionElapsed = (auctionEnd - timestamp);
134141
message.timeout = timeout;
@@ -137,26 +144,9 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
137144
message.adUnits[adUnitCode] = {};
138145
});
139146

140-
bidsReceived.forEach((bid) => {
141-
const adUnitCode = parseAdUnitCode(bid);
142-
const bidder = parseBidderCode(bid);
143-
const bidResponse = this.serializeBidResponse(bid, BIDDER_STATUS.BID);
144-
message.adUnits[adUnitCode][bidder] = bidResponse;
145-
});
146-
147-
noBids.forEach((bid) => {
148-
const adUnitCode = parseAdUnitCode(bid);
149-
const bidder = parseBidderCode(bid);
150-
const bidResponse = this.serializeBidResponse(bid, BIDDER_STATUS.NO_BID);
151-
message.adUnits[adUnitCode][bidder] = bidResponse;
152-
});
153-
154-
timeoutBids.forEach((bid) => {
155-
const adUnitCode = parseAdUnitCode(bid);
156-
const bidder = parseBidderCode(bid);
157-
const bidResponse = this.serializeBidResponse(bid, BIDDER_STATUS.TIMEOUT);
158-
message.adUnits[adUnitCode][bidder] = bidResponse;
159-
});
147+
bidsReceived.forEach(bid => this.addBidResponseToMessage(message, bid, BIDDER_STATUS.BID));
148+
noBids.forEach(bid => this.addBidResponseToMessage(message, bid, BIDDER_STATUS.NO_BID));
149+
timeoutBids.forEach(bid => this.addBidResponseToMessage(message, bid, BIDDER_STATUS.TIMEOUT));
160150

161151
// mark the winning bids with prebidWon = true
162152
winningBids.forEach(bid => {
@@ -166,17 +156,13 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
166156
});
167157
return message;
168158
},
169-
newImpressionMessageBody(bid) {
170-
const message = this.newCommonMessageBody(bid.auctionId);
171-
const adUnitCode = parseAdUnitCode(bid);
172-
message.adUnits[adUnitCode] = {};
173-
const bidder = parseBidderCode(bid);
174-
const bidResponse = this.serializeBidResponse(bid, BIDDER_STATUS.BID_WON);
175-
message.adUnits[adUnitCode][bidder] = bidResponse;
159+
createImpressionMessage(bid) {
160+
const message = this.createCommonMessage(bid.auctionId);
161+
this.addBidResponseToMessage(message, bid, BIDDER_STATUS.BID_WON);
176162
return message;
177163
},
178-
newCreativeMessageBody(auctionId, bids) {
179-
const message = this.newCommonMessageBody(auctionId);
164+
createCreativeMessage(auctionId, bids) {
165+
const message = this.createCommonMessage(auctionId);
180166
bids.forEach((bid) => {
181167
const adUnitCode = parseAdUnitCode(bid);
182168
const bidder = parseBidderCode(bid);
@@ -196,13 +182,13 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
196182
const highestCpmBids = pbjs.getHighestCpmBids();
197183

198184
this.sendEventMessage('bid',
199-
this.newAuctionMessageBody(auctionEndArgs, highestCpmBids, cachedAuction.timeoutBids)
185+
this.createBidMessage(auctionEndArgs, highestCpmBids, cachedAuction.timeoutBids)
200186
);
201187

202188
if (analyticsOptions.adSampled) {
203189
// FIXME: do not send the message if there are no creatives at all to safe bandwidth
204190
this.sendEventMessage('cr',
205-
this.newCreativeMessageBody(auctionEndArgs.auctionId, auctionEndArgs.bidsReceived)
191+
this.createCreativeMessage(auctionEndArgs.auctionId, auctionEndArgs.bidsReceived)
206192
);
207193
}
208194
},
@@ -213,7 +199,7 @@ export const appierAnalyticsAdapter = Object.assign(adapter({DEFAULT_SERVER, ana
213199
});
214200
},
215201
handleBidWon(bidWonArgs) {
216-
this.sendEventMessage('imp', this.newImpressionMessageBody(bidWonArgs));
202+
this.sendEventMessage('imp', this.createImpressionMessage(bidWonArgs));
217203
},
218204
track({eventType, args}) {
219205
if (analyticsOptions.sampled) {

test/spec/modules/appierAnalyticsAdapter_spec.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,14 @@ import {expect} from 'chai';
55
import * as ajax from 'src/ajax';
66

77
const events = require('src/events');
8-
const constants = require('src/constants.json');
98

109
const affiliateId = 'WhctHaViHtI';
1110
const configId = 'd9cc9a9b-e9b2-40ed-a17c-f1c9a8a4b29c';
1211
const serverUrl = 'https://analytics.server.url/v1';
1312
const autoPick = 'none';
14-
const hzid = 'WhctHaV9';
1513
const auctionId = 'b0b39610-b941-4659-a87c-de9f62d3e13e';
16-
const bidderCode = 'appier';
17-
const timeout = 2000;
18-
const auctionStart = Date.now();
1914

2015
describe('Appier Prebid AnalyticsAdapter', function () {
21-
let requests;
22-
let cache;
23-
2416
describe('event tracking and message cache manager', function () {
2517
let ajaxStub;
2618

@@ -39,7 +31,6 @@ describe('Appier Prebid AnalyticsAdapter', function () {
3931
options: configOptions
4032
});
4133

42-
requests = [];
4334
sinon.stub(events, 'getEvents').returns([]);
4435

4536
ajaxStub = sinon.stub(ajax, 'ajax').callsFake(function (url, callbacks) {
@@ -164,16 +155,15 @@ describe('Appier Prebid AnalyticsAdapter', function () {
164155
});
165156
}
166157

167-
describe('#newCommonMessageBody', function() {
158+
describe('#createCommonMessage', function() {
168159
it('should correctly serialize some common fields', function() {
169-
const message = appierAnalyticsAdapter.newCommonMessageBody(auctionId);
160+
const message = appierAnalyticsAdapter.createCommonMessage(auctionId);
170161

171162
assertHavingRequiredMessageFields(message);
172163
});
173164
});
174165

175166
describe('#serializeBidResponse', function() {
176-
177167
it('should handle BID properly and serialize bid price related fields', function() {
178168
const result = appierAnalyticsAdapter.serializeBidResponse(receivedBids[0], BIDDER_STATUS.BID);
179169

@@ -227,7 +217,26 @@ describe('Appier Prebid AnalyticsAdapter', function () {
227217
});
228218
});
229219

230-
describe('#newAuctionMessageBody()', function() {
220+
describe('#addBidResponseToMessage()', function() {
221+
it('should add a bid response in the output message, grouped by adunit_id and bidder', function() {
222+
const message = {
223+
adUnits: {}
224+
};
225+
appierAnalyticsAdapter.addBidResponseToMessage(message, noBids[0], BIDDER_STATUS.NO_BID);
226+
227+
expect(message.adUnits).to.deep.include({
228+
'adunit_2': {
229+
'appier': {
230+
prebidWon: false,
231+
isTimeout: false,
232+
status: BIDDER_STATUS.NO_BID,
233+
}
234+
}
235+
});
236+
});
237+
});
238+
239+
describe('#createBidMessage()', function() {
231240
it('should format auction message sent to the backend', function() {
232241
const args = {
233242
auctionId: auctionId,
@@ -239,7 +248,7 @@ describe('Appier Prebid AnalyticsAdapter', function () {
239248
noBids: noBids
240249
};
241250

242-
const result = appierAnalyticsAdapter.newAuctionMessageBody(args, highestCpmBids, timeoutBids);
251+
const result = appierAnalyticsAdapter.createBidMessage(args, highestCpmBids, timeoutBids);
243252

244253
assertHavingRequiredMessageFields(result);
245254
expect(result).to.deep.include({
@@ -287,10 +296,10 @@ describe('Appier Prebid AnalyticsAdapter', function () {
287296
});
288297
});
289298

290-
describe('#newImpressionMessageBody()', function() {
299+
describe('#createImpressionMessage()', function() {
291300
it('should format message sent to the backend with the bid result', function() {
292301
const bid = receivedBids[0];
293-
const result = appierAnalyticsAdapter.newImpressionMessageBody(bid);
302+
const result = appierAnalyticsAdapter.createImpressionMessage(bid);
294303

295304
assertHavingRequiredMessageFields(result);
296305
expect(result.adUnits).to.deep.include({
@@ -311,9 +320,9 @@ describe('Appier Prebid AnalyticsAdapter', function () {
311320
});
312321
});
313322

314-
describe('#newCreativeMessageBody()', function() {
323+
describe('#createCreativeMessage()', function() {
315324
it('should generate message sent to the backend with ad html grouped by adunit and bidder', function() {
316-
const result = appierAnalyticsAdapter.newCreativeMessageBody(auctionId, receivedBids);
325+
const result = appierAnalyticsAdapter.createCreativeMessage(auctionId, receivedBids);
317326

318327
assertHavingRequiredMessageFields(result);
319328
expect(result.adUnits).to.deep.include({
@@ -338,7 +347,6 @@ describe('Appier Prebid AnalyticsAdapter', function () {
338347

339348
describe('enableAnalytics and config parser', function () {
340349
beforeEach(function () {
341-
requests = [];
342350
sinon.stub(events, 'getEvents').returns([]);
343351
});
344352

0 commit comments

Comments
 (0)