Skip to content

Commit

Permalink
Updated adapter to support newer schema and updated position of aduni…
Browse files Browse the repository at this point in the history
…t code
  • Loading branch information
Jimmy Tu committed May 31, 2020
1 parent c39627f commit 9e248e2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 82 deletions.
171 changes: 95 additions & 76 deletions modules/openxAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,12 @@ function onAuctionInit({auctionId, timestamp: startTime, timeout, adUnitCodes})
};

// setup adunit properties in map
auctionMap[auctionId].adUnitCodeToBidderRequestMap = adUnitCodes.reduce((obj, adunitCode) => {
obj[adunitCode] = {};
auctionMap[auctionId].adUnitCodeToAdUnitMap = adUnitCodes.reduce((obj, adunitCode) => {
obj[adunitCode] = {
code: adunitCode,
adPosition: void (0),
bidRequestsMap: {}
};
return obj;
}, {});

Expand All @@ -882,12 +886,12 @@ function onAuctionInit({auctionId, timestamp: startTime, timeout, adUnitCodes})
function onBidRequested(bidRequest) {
const {auctionId, auctionStart, refererInfo, bids: bidderRequests, start} = bidRequest;
const auction = auctionMap[auctionId];
const adUnitCodeToBidderRequestMap = auction.adUnitCodeToBidderRequestMap;
const adUnitCodeToAdUnitMap = auction.adUnitCodeToAdUnitMap;

bidderRequests.forEach(bidderRequest => {
const { adUnitCode, bidder, bidId: requestId, mediaTypes, params, src, userId } = bidderRequest;

adUnitCodeToBidderRequestMap[adUnitCode][requestId] = {
adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId] = {
bidder,
params,
mediaTypes,
Expand Down Expand Up @@ -928,7 +932,7 @@ function onBidResponse(bidResponse) {
meta
} = bidResponse;

auctionMap[auctionId].adUnitCodeToBidderRequestMap[adUnitCode][requestId].bids[adId] = {
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId].bids[adId] = {
cpm,
creativeId,
requestTimestamp,
Expand All @@ -954,12 +958,12 @@ function onBidResponse(bidResponse) {

function onBidTimeout(args) {
utils._each(args, ({auctionId, adUnitCode, bidId: requestId}) => {
if (auctionMap[auctionId]
&& auctionMap[auctionId].adUnitCodeToBidderRequestMap
&& auctionMap[auctionId].adUnitCodeToBidderRequestMap[adUnitCode]
&& auctionMap[auctionId].adUnitCodeToBidderRequestMap[adUnitCode][requestId]
if (auctionMap[auctionId] &&
auctionMap[auctionId].adUnitCodeToAdUnitMap &&
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode] &&
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId]
) {
auctionMap[auctionId].adUnitCodeToBidderRequestMap[adUnitCode][requestId].timedOut = true;
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId].timedOut = true;
}
});
}
Expand All @@ -986,7 +990,14 @@ function onAuctionEnd(endedAuction) {
*/
function onBidWon(bidResponse) {
const { auctionId, adUnitCode, requestId, adId } = bidResponse;
auctionMap[auctionId].adUnitCodeToBidderRequestMap[adUnitCode][requestId].bids[adId].winner = true;
if (auctionMap[auctionId] &&
auctionMap[auctionId].adUnitCodeToAdUnitMap &&
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode] &&
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId] &&
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId].bids[adId]
) {
auctionMap[auctionId].adUnitCodeToAdUnitMap[adUnitCode].bidRequestsMap[requestId].bids[adId].winner = true;
}
}

/**
Expand Down Expand Up @@ -1053,7 +1064,11 @@ function getAuctionByGoogleTagSLot(slot) {
let slotAuction;

utils._each(auctionMap, auction => {
utils._each(auction.adUnitCodeToBidderRequestMap, (bidderRequestIdMap, adUnitCode) => {
if (auction.state === AUCTION_STATES.COMPLETED) {
return;
}

utils._each(auction.adUnitCodeToAdUnitMap, (bidderRequestIdMap, adUnitCode) => {
if (slotAdunitCodes.includes(adUnitCode)) {
slotAuction = auction;
}
Expand All @@ -1064,7 +1079,7 @@ function getAuctionByGoogleTagSLot(slot) {
}

function buildAuctionPayload(auction) {
let {startTime, endTime, state, timeout, auctionOrder, adUnitCodeToBidderRequestMap} = auction;
let {startTime, endTime, state, timeout, auctionOrder, adUnitCodeToAdUnitMap} = auction;
let {publisherPlatformId, publisherAccountId, campaign} = analyticsConfig;

return {
Expand All @@ -1080,73 +1095,77 @@ function buildAuctionPayload(auction) {
deviceOSType: detectOS(),
browser: detectBrowser(),
testCode: analyticsConfig.testCode,
adUnits: buildBidRequestsPayload(adUnitCodeToBidderRequestMap),
adUnits: buildAdUnitsPayload(adUnitCodeToAdUnitMap),
};

function buildBidRequestsPayload(adUnitCodeToBidderRequestMap) {
return utils._map(adUnitCodeToBidderRequestMap, (bidderRequestMap, adUnitCode) => {
let bidRequests = utils._map(bidderRequestMap, (bidderRequest) => {
let {bidder, source, bids, mediaTypes, timedOut, userId} = bidderRequest;
return {
adUnitCode,
bidder,
source,
// return an array of objects containing the module name and id
userIds: buildUserIdPayload(userId),
hasBidderResponded: Object.keys(bids).length > 0,
availableAdSizes: getMediaTypeSizes(mediaTypes),
availableMediaTypes: getMediaTypes(mediaTypes),
timedOut,
bidResponses: utils._map(bidderRequest.bids, (bidderBidResponse) => {
let {
cpm,
creativeId,
responseTimestamp,
ts,
adId,
meta,
mediaType,
dealId,
ttl,
netRevenue,
currency,
originalCpm,
originalCurrency,
width,
height,
latency,
winner,
rendered,
renderTime
} = bidderBidResponse;

return {
microCpm: cpm * 1000,
netRevenue,
currency,
mediaType,
height,
width,
size: `${width}x${height}`,
dealId,
latency,
ttl,
winner,
creativeId,
ts,
rendered,
renderTime,
meta
}
})
}
});
function buildAdUnitsPayload(adUnitCodeToAdUnitMap) {
return utils._map(adUnitCodeToAdUnitMap, (adUnit) => {
let {code, adPosition} = adUnit;

return {
code: adUnitCode,
bidRequests
code,
adPosition,
bidRequests: buildBidRequestPayload(adUnit.bidRequestsMap)
};

function buildBidRequestPayload(bidRequestsMap) {
return utils._map(bidRequestsMap, (bidRequest) => {
let {bidder, source, bids, mediaTypes, timedOut, userId} = bidRequest;
return {
bidder,
source,
// return an array of objects containing the module name and id
userIds: buildUserIdPayload(userId),
hasBidderResponded: Object.keys(bids).length > 0,
availableAdSizes: getMediaTypeSizes(mediaTypes),
availableMediaTypes: getMediaTypes(mediaTypes),
timedOut,
bidResponses: utils._map(bidRequest.bids, (bidderBidResponse) => {
let {
cpm,
creativeId,
responseTimestamp,
ts,
adId,
meta,
mediaType,
dealId,
ttl,
netRevenue,
currency,
originalCpm,
originalCurrency,
width,
height,
latency,
winner,
rendered,
renderTime
} = bidderBidResponse;

return {
microCpm: cpm * 1000,
netRevenue,
currency,
mediaType,
height,
width,
size: `${width}x${height}`,
dealId,
latency,
ttl,
winner,
creativeId,
ts,
rendered,
renderTime,
meta
}
})
}
});
}

function buildUserIdPayload(userId) {
return utils._map(userId, (id, module) => {
return {
Expand Down Expand Up @@ -1191,8 +1210,8 @@ function buildAuctionPayload(auction) {
function getAdUnitByAuctionAndAdId(auction, adId) {
let adunit;

utils._each(auction.adUnitCodeToBidderRequestMap, (bidderRequestIdMap) => {
utils._each(bidderRequestIdMap, bidderRequest => {
utils._each(auction.adUnitCodeToAdUnitMap, (bidderRequestIdMap) => {
utils._each(bidderRequestIdMap.bidRequestsMap, bidderRequest => {
utils._each(bidderRequest.bids, (bid, bidId) => {
if (bidId === adId) {
adunit = bid;
Expand Down
8 changes: 2 additions & 6 deletions test/spec/modules/openxAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ describe('openx analytics adapter', function() {
]);

simulateAuction([
[AUCTION_INIT, {...auctionInit, auctionId: 'second-auction-id'} ],
[AUCTION_INIT, {...auctionInit, auctionId: 'second-auction-id'}],
[SLOT_LOADED]
]);

Expand Down Expand Up @@ -930,11 +930,7 @@ describe('openx analytics adapter', function() {
});

it('should track the adunit code', function () {
let openxBidder = auction.adUnits[0].bidRequests.find(bidderRequest => bidderRequest.bidder === 'openx');
let closexBidder = auction.adUnits[0].bidRequests.find(bidderRequest => bidderRequest.bidder === 'closex');

expect(openxBidder.adUnitCode).to.equal(AD_UNIT_CODE);
expect(closexBidder.adUnitCode).to.equal(AD_UNIT_CODE);
expect(auction.adUnits[0].code).to.equal(AD_UNIT_CODE);
});

it('should track the user ids', function () {
Expand Down

0 comments on commit 9e248e2

Please sign in to comment.