forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John Rosendahl
committed
Aug 22, 2018
1 parent
6565d2e
commit 7483190
Showing
5 changed files
with
89 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
import Adapter from 'src/adapter'; | ||
import adaptermanager from 'src/adaptermanager'; | ||
import { config } from 'src/config'; | ||
import { | ||
config | ||
} from 'src/config'; | ||
import bidfactory from 'src/bidfactory'; | ||
import { userSync } from 'src/userSync'; | ||
import { nativeBidIsValid } from 'src/native'; | ||
import { isValidVideoBid } from 'src/video'; | ||
import { | ||
userSync | ||
} from 'src/userSync'; | ||
import { | ||
nativeBidIsValid | ||
} from 'src/native'; | ||
import { | ||
isValidVideoBid | ||
} from 'src/video'; | ||
import CONSTANTS from 'src/constants.json'; | ||
import events from 'src/events'; | ||
import includes from 'core-js/library/fn/array/includes'; | ||
|
||
import { logWarn, logError, parseQueryStringParameters, delayExecution, parseSizesInput, getBidderRequest } from 'src/utils'; | ||
import { | ||
logWarn, | ||
logError, | ||
parseQueryStringParameters, | ||
delayExecution, | ||
parseSizesInput, | ||
getBidderRequest | ||
} from 'src/utils'; | ||
|
||
/** | ||
* This file aims to support Adapters during the Prebid 0.x -> 1.x transition. | ||
|
@@ -121,7 +136,8 @@ import { logWarn, logError, parseQueryStringParameters, delayExecution, parseSiz | |
*/ | ||
|
||
// common params for all mediaTypes | ||
const COMMON_BID_RESPONSE_KEYS = ['requestId', 'cpm', 'ttl', 'creativeId', 'netRevenue', 'currency']; | ||
const COMMON_BID_RESPONSE_KEYS = ['requestId']; | ||
const GOOD_BID_RESPONSE_KEYS = ['requestId', 'cpm', 'ttl', 'creativeId', 'netRevenue', 'currency']; | ||
|
||
/** | ||
* Register a bidder with prebid, using the given spec. | ||
|
@@ -131,9 +147,8 @@ const COMMON_BID_RESPONSE_KEYS = ['requestId', 'cpm', 'ttl', 'creativeId', 'netR | |
* @param {BidderSpec} spec An object containing the bare-bones functions we need to make a Bidder. | ||
*/ | ||
export function registerBidder(spec) { | ||
const mediaTypes = Array.isArray(spec.supportedMediaTypes) | ||
? { supportedMediaTypes: spec.supportedMediaTypes } | ||
: undefined; | ||
const mediaTypes = Array.isArray(spec.supportedMediaTypes) ? {supportedMediaTypes: spec.supportedMediaTypes} : undefined; | ||
|
||
function putBidder(spec) { | ||
const bidder = newBidder(spec); | ||
adaptermanager.registerBidAdapter(bidder, spec.code, mediaTypes); | ||
|
@@ -143,7 +158,9 @@ export function registerBidder(spec) { | |
if (Array.isArray(spec.aliases)) { | ||
spec.aliases.forEach(alias => { | ||
adaptermanager.aliasRegistry[alias] = spec.code; | ||
putBidder(Object.assign({}, spec, { code: alias })); | ||
putBidder(Object.assign({}, spec, { | ||
code: alias | ||
})); | ||
}); | ||
} | ||
} | ||
|
@@ -156,16 +173,17 @@ export function registerBidder(spec) { | |
*/ | ||
export function newBidder(spec) { | ||
return Object.assign(new Adapter(spec.code), { | ||
getSpec: function() { | ||
getSpec: function () { | ||
return Object.freeze(spec); | ||
}, | ||
registerSyncs, | ||
callBids: function(bidderRequest, addBidResponse, done, ajax) { | ||
callBids: function (bidderRequest, addBidResponse, done, ajax) { | ||
if (!Array.isArray(bidderRequest.bids)) { | ||
return; | ||
} | ||
|
||
const adUnitCodesHandled = {}; | ||
|
||
function addBidWithCode(adUnitCode, bid) { | ||
adUnitCodesHandled[adUnitCode] = true; | ||
if (isValid(adUnitCode, bid, [bidderRequest])) { | ||
|
@@ -176,8 +194,24 @@ export function newBidder(spec) { | |
// After all the responses have come back, call done() and | ||
// register any required usersync pixels. | ||
const responses = []; | ||
|
||
function afterAllResponses(bids) { | ||
const bidsArray = bids ? (bids[0] ? bids : [bids]) : []; | ||
// bidderRequest.end = timestamp(); | ||
let bidIds = bidsArray.map(bid => { | ||
return bid.requestId.toString(); | ||
}); | ||
|
||
let missingBids = bidderRequest.bids.filter(bid => { | ||
return bidIds.indexOf(bid.bidId.toString()) === -1; | ||
}); | ||
|
||
missingBids.forEach(bid => { | ||
const bidRequest = bidRequestMap[bid.bidId]; | ||
bid.requestId = bid.bidId; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
let prebidNoBid = Object.assign(bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidRequest), bid); | ||
addBidWithCode(bid.adUnitCode, prebidNoBid); | ||
}); | ||
|
||
const videoBid = bidsArray.some(bid => bid.mediaType === 'video'); | ||
const cacheEnabled = config.getConfig('cache.url'); | ||
|
@@ -191,16 +225,15 @@ export function newBidder(spec) { | |
// needs to do cleanup before _it_ can be done it should handle that itself in the auction. It should _not_ | ||
// require us, the bidders, to conditionally call done. That makes the whole done API very flaky. | ||
// As soon as that is refactored, we can move this emit event where it should be, within the done function. | ||
events.emit(CONSTANTS.EVENTS.BIDDER_DONE, bidderRequest); | ||
finishBidder() | ||
} | ||
|
||
function finishBidder() { | ||
events.emit(CONSTANTS.EVENTS.BIDDER_DONE, bidderRequest); | ||
registerSyncs(responses, bidderRequest.gdprConsent); | ||
} | ||
|
||
const validBidRequests = bidderRequest.bids.filter(filterAndWarn); | ||
if (validBidRequests.length === 0) { | ||
afterAllResponses(); | ||
return; | ||
} | ||
const bidRequestMap = {}; | ||
validBidRequests.forEach(bid => { | ||
bidRequestMap[bid.bidId] = bid; | ||
|
@@ -210,9 +243,14 @@ export function newBidder(spec) { | |
} | ||
}); | ||
|
||
if (validBidRequests.length === 0) { | ||
afterAllResponses(); | ||
return; | ||
} | ||
|
||
let requests = spec.buildRequests(validBidRequests, bidderRequest); | ||
if (!requests || requests.length === 0) { | ||
afterAllResponses(); | ||
finishBidder() | ||
return; | ||
} | ||
if (!Array.isArray(requests)) { | ||
|
@@ -237,8 +275,7 @@ export function newBidder(spec) { | |
switch (request.method) { | ||
case 'GET': | ||
ajax( | ||
`${request.url}${formatGetParameters(request.data)}`, | ||
{ | ||
`${request.url}${formatGetParameters(request.data)}`, { | ||
success: onSuccess, | ||
error: onFailure | ||
}, | ||
|
@@ -251,8 +288,7 @@ export function newBidder(spec) { | |
break; | ||
case 'POST': | ||
ajax( | ||
request.url, | ||
{ | ||
request.url, { | ||
success: onSuccess, | ||
error: onFailure | ||
}, | ||
|
@@ -370,7 +406,7 @@ function validBidSize(adUnitCode, bid, bidRequests) { | |
// if a banner impression has one valid size, we assign that size to any bid | ||
// response that does not explicitly set width or height | ||
if (parsedSizes.length === 1) { | ||
const [ width, height ] = parsedSizes[0].split('x'); | ||
const [width, height] = parsedSizes[0].split('x'); | ||
bid.width = width; | ||
bid.height = height; | ||
return true; | ||
|
@@ -383,6 +419,9 @@ function validBidSize(adUnitCode, bid, bidRequests) { | |
export function isValid(adUnitCode, bid, bidRequests) { | ||
function hasValidKeys() { | ||
let bidKeys = Object.keys(bid); | ||
if (bid.getStatusCode() === CONSTANTS.STATUS.GOOD) { | ||
return GOOD_BID_RESPONSE_KEYS.every(key => includes(bidKeys, key) && !includes([undefined, null], bid[key])); | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
rachelrj
|
||
return COMMON_BID_RESPONSE_KEYS.every(key => includes(bidKeys, key) && !includes([undefined, null], bid[key])); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Does the requestId always equal the bidId on the bid? If not, is it weird to override it? If so, why isn't this set before?