@@ -219,7 +219,7 @@ export function newTargeting(auctionManager) {
219219 if ( enableSendAllBids || ( deals && bid . dealId ) ) {
220220 const targetingValue = getTargetingMap ( bid , standardKeys . filter (
221221 key => typeof bid . adserverTargeting [ key ] !== 'undefined' &&
222- ( deals || allowedSendAllBidTargeting . indexOf ( key ) !== - 1 ) ) ) ;
222+ ( deals || allowedSendAllBidTargeting . indexOf ( key ) !== - 1 ) ) ) ;
223223
224224 if ( targetingValue ) {
225225 result . push ( { [ bid . adUnitCode ] : targetingValue } )
@@ -290,7 +290,7 @@ export function newTargeting(auctionManager) {
290290 const adUnitBidLimit = ( sendAllBids && ( bidLimit || bidLimitConfigValue ) ) || 0 ;
291291 const { customKeysByUnit, filteredBids } = getfilteredBidsAndCustomKeys ( adUnitCodes , bidsReceived ) ;
292292 const bidsSorted = getHighestCpmBidsFromBidPool ( filteredBids , winReducer , adUnitBidLimit , undefined , winSorter ) ;
293- let targeting = getTargetingLevels ( bidsSorted , customKeysByUnit ) ;
293+ let targeting = getTargetingLevels ( bidsSorted , customKeysByUnit , adUnitCodes ) ;
294294
295295 const defaultKeys = Object . keys ( Object . assign ( { } , DEFAULT_TARGETING_KEYS , NATIVE_KEYS ) ) ;
296296 let allowedKeys = config . getConfig ( CFG_ALLOW_TARGETING_KEYS ) ;
@@ -337,8 +337,8 @@ export function newTargeting(auctionManager) {
337337 } ) ;
338338 }
339339
340- function getTargetingLevels ( bidsSorted , customKeysByUnit ) {
341- const targeting = getWinningBidTargeting ( bidsSorted )
340+ function getTargetingLevels ( bidsSorted , customKeysByUnit , adUnitCodes ) {
341+ const targeting = getWinningBidTargeting ( bidsSorted , adUnitCodes )
342342 . concat ( getCustomBidTargeting ( bidsSorted , customKeysByUnit ) )
343343 . concat ( getBidderTargeting ( bidsSorted ) )
344344 . concat ( getAdUnitTargeting ( ) ) ;
@@ -568,25 +568,17 @@ export function newTargeting(auctionManager) {
568568 * @return {Array<Object> } - An array of winning bids.
569569 */
570570 targeting . getWinningBids = function ( adUnitCode , bids , winReducer = getHighestCpm , winSorter = sortByHighestCpm ) {
571- const usedCodes = [ ] ;
572571 const bidsReceived = bids || getBidsReceived ( winReducer , winSorter ) ;
573572 const adUnitCodes = getAdUnitCodes ( adUnitCode ) ;
574573
575574 return bidsReceived
576- . reduce ( ( result , bid ) => {
577- const code = bid . adUnitCode ;
578- const cpmEligible = bidderSettings . get ( code , 'allowZeroCpmBids' ) === true ? bid . cpm >= 0 : bid . cpm > 0 ;
579- const isPreferredDeal = config . getConfig ( 'targetingControls.alwaysIncludeDeals' ) && bid . dealId ;
580- const eligible = includes ( adUnitCodes , code ) &&
581- ! includes ( usedCodes , code ) &&
582- ( isPreferredDeal || cpmEligible )
583- if ( eligible ) {
584- result . push ( bid ) ;
585- usedCodes . push ( code ) ;
586- }
587-
588- return result ;
589- } , [ ] ) ;
575+ . filter ( bid => includes ( adUnitCodes , bid . adUnitCode ) )
576+ . filter ( bid => ( bidderSettings . get ( bid . bidderCode , 'allowZeroCpmBids' ) === true ) ? bid . cpm >= 0 : bid . cpm > 0 )
577+ . map ( bid => bid . adUnitCode )
578+ . filter ( uniques )
579+ . map ( adUnitCode => bidsReceived
580+ . filter ( bid => bid . adUnitCode === adUnitCode ? bid : null )
581+ . reduce ( getHighestCpm ) ) ;
590582 } ;
591583
592584 /**
@@ -624,19 +616,11 @@ export function newTargeting(auctionManager) {
624616 /**
625617 * Get targeting key value pairs for winning bid.
626618 * @param {Array<Object> } bidsReceived code array
619+ * @param {string[] } adUnitCodes code array
627620 * @return {targetingArray } winning bids targeting
628621 */
629- function getWinningBidTargeting ( bidsReceived ) {
630- let usedAdUnitCodes = [ ] ;
631- let winners = bidsReceived
632- . reduce ( ( bids , bid ) => {
633- if ( ! includes ( usedAdUnitCodes , bid . adUnitCode ) ) {
634- bids . push ( bid ) ;
635- usedAdUnitCodes . push ( bid . adUnitCode ) ;
636- }
637- return bids ;
638- } , [ ] ) ;
639-
622+ function getWinningBidTargeting ( bidsReceived , adUnitCodes ) {
623+ let winners = targeting . getWinningBids ( adUnitCodes , bidsReceived ) ;
640624 let standardKeys = getStandardKeys ( ) ;
641625
642626 winners = winners . map ( winner => {
0 commit comments