forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(greenbids,analytics): fetch bidder params when bid is valid #6
Open
AlexisBRENON
wants to merge
5
commits into
master
Choose a base branch
from
bidder_params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ddfe18
fix(greenbids,analytics): fetch bidder params when bid is valid
AlexisBRENON 48700ad
WIP: test failing
AlexisBRENON a4a74ec
fix tests
AlexisBRENON fb4e25a
revert unwanted modif
AlexisBRENON abb9bbb
add edge case tests
AlexisBRENON File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,20 +1,40 @@ | ||
import {ajax} from '../src/ajax.js'; | ||
import { ajax } from '../src/ajax.js'; | ||
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js'; | ||
import { EVENTS } from '../src/constants.js'; | ||
import adapterManager from '../src/adapterManager.js'; | ||
import {deepClone, generateUUID, logError, logInfo, logWarn, getParameterByName} from '../src/utils.js'; | ||
import { deepClone, generateUUID, logError, logInfo, logWarn, getParameterByName } from '../src/utils.js'; | ||
|
||
/** | ||
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid | ||
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest | ||
*/ | ||
|
||
/** | ||
* @typedef {object} Message Payload message sent to the Greenbids API | ||
*/ | ||
|
||
/** | ||
* @typedef AuctionEndArgs | ||
* @type {object} | ||
* @property {string} auctionId | ||
* @property {number} timestamp - Auction start epoch | ||
* @property {number} auctionEnd - Auction end epoch | ||
* @property {Bid[]} bidsReceived | ||
* @property {BidRequest[]} noBids | ||
*/ | ||
|
||
/** | ||
* @typedef GreenbidsCachedOption | ||
* @type {object} | ||
* @property {any[]} timeoutBids | ||
* @property {?string} greenbidsId | ||
* @property {?string} billingId | ||
* @property {boolean} isSampled | ||
*/ | ||
|
||
const analyticsType = 'endpoint'; | ||
|
||
export const ANALYTICS_VERSION = '2.3.2'; | ||
export const ANALYTICS_VERSION = '2.3.3'; | ||
|
||
const ANALYTICS_SERVER = 'https://a.greenbids.ai'; | ||
|
||
|
@@ -33,7 +53,7 @@ export const BIDDER_STATUS = { | |
|
||
const analyticsOptions = {}; | ||
|
||
export const isSampled = function(greenbidsId, samplingRate, exploratorySamplingSplit) { | ||
export const isSampled = function (greenbidsId, samplingRate, exploratorySamplingSplit) { | ||
const isSamplingForced = getParameterByName('greenbids_force_sampling'); | ||
if (isSamplingForced) { | ||
logInfo('Greenbids Analytics: sampling flag detected, forcing analytics'); | ||
|
@@ -52,7 +72,7 @@ export const isSampled = function(greenbidsId, samplingRate, exploratorySampling | |
return isExtraSampled; | ||
} | ||
|
||
export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER, analyticsType}), { | ||
export const greenbidsAnalyticsAdapter = Object.assign(adapter({ ANALYTICS_SERVER, analyticsType }), { | ||
|
||
cachedAuctions: {}, | ||
exploratorySamplingSplit: 0.9, | ||
|
@@ -125,83 +145,82 @@ export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER | |
}; | ||
}, | ||
/** | ||
* @param {Bid} bid | ||
* @param {BIDDER_STATUS} status | ||
*/ | ||
serializeBidResponse(bid, status) { | ||
return { | ||
bidder: bid.bidder, | ||
isTimeout: (status === BIDDER_STATUS.TIMEOUT), | ||
hasBid: (status === BIDDER_STATUS.BID), | ||
params: (bid.params && Object.keys(bid.params).length > 0) ? bid.params : {}, | ||
...(status === BIDDER_STATUS.BID ? { | ||
cpm: bid.cpm, | ||
currency: bid.currency | ||
} : {}), | ||
}; | ||
}, | ||
/** | ||
* @param {*} message Greenbids API payload | ||
* @param {Bid} bid Bid to add to the payload | ||
* @param {BIDDER_STATUS} status Bidding status | ||
* @param {AuctionEndArgs} auctionEndArgs | ||
* @param {GreenbidsCachedOption} cachedAuction | ||
* @returns {Message} | ||
*/ | ||
addBidResponseToMessage(message, bid, status) { | ||
const adUnitCode = bid.adUnitCode.toLowerCase(); | ||
const adUnitIndex = message.adUnits.findIndex((adUnit) => { | ||
return adUnit.code === adUnitCode; | ||
}); | ||
if (adUnitIndex === -1) { | ||
logError('Trying to add to non registered adunit'); | ||
return; | ||
} | ||
const bidderIndex = message.adUnits[adUnitIndex].bidders.findIndex((bidder) => { | ||
return bidder.bidder === bid.bidder; | ||
}); | ||
if (bidderIndex === -1) { | ||
message.adUnits[adUnitIndex].bidders.push(this.serializeBidResponse(bid, status)); | ||
} else { | ||
message.adUnits[adUnitIndex].bidders[bidderIndex].params = (bid.params && Object.keys(bid.params).length > 0) ? bid.params : {}; | ||
if (status === BIDDER_STATUS.BID) { | ||
message.adUnits[adUnitIndex].bidders[bidderIndex].hasBid = true; | ||
message.adUnits[adUnitIndex].bidders[bidderIndex].cpm = bid.cpm; | ||
message.adUnits[adUnitIndex].bidders[bidderIndex].currency = bid.currency; | ||
} else if (status === BIDDER_STATUS.TIMEOUT) { | ||
message.adUnits[adUnitIndex].bidders[bidderIndex].isTimeout = true; | ||
} | ||
} | ||
}, | ||
createBidMessage(auctionEndArgs) { | ||
const {auctionId, timestamp, auctionEnd, adUnits, bidsReceived, noBids} = auctionEndArgs; | ||
const cachedAuction = this.getCachedAuction(auctionId); | ||
const message = this.createCommonMessage(auctionId); | ||
const timeoutBids = cachedAuction.timeoutBids || []; | ||
createBidMessage(auctionEndArgs, cachedAuction) { | ||
const { | ||
auctionId, | ||
timestamp, | ||
auctionEnd, | ||
adUnits, | ||
bidsReceived, | ||
noBids | ||
} = auctionEndArgs; | ||
const timeoutBids = (cachedAuction || this.getCachedAuction(auctionId)).timeoutBids || []; | ||
|
||
const message = this.createCommonMessage(auctionId); | ||
message.auctionElapsed = (auctionEnd - timestamp); | ||
|
||
const biddersSubMessages = new Map() | ||
|
||
adUnits.forEach((adUnit) => { | ||
const adUnitCode = adUnit.code?.toLowerCase() || 'unknown_adunit_code'; | ||
message.adUnits.push({ | ||
code: adUnitCode, | ||
mediaTypes: { | ||
...(adUnit.mediaTypes?.banner !== undefined) && {banner: adUnit.mediaTypes.banner}, | ||
...(adUnit.mediaTypes?.video !== undefined) && {video: adUnit.mediaTypes.video}, | ||
...(adUnit.mediaTypes?.native !== undefined) && {native: adUnit.mediaTypes.native} | ||
...(adUnit.mediaTypes?.banner !== undefined) && { banner: adUnit.mediaTypes.banner }, | ||
...(adUnit.mediaTypes?.video !== undefined) && { video: adUnit.mediaTypes.video }, | ||
...(adUnit.mediaTypes?.native !== undefined) && { native: adUnit.mediaTypes.native } | ||
}, | ||
ortb2Imp: adUnit.ortb2Imp || {}, | ||
bidders: [], | ||
|
||
bidders: (adUnit.bids || []).map((bid) => { | ||
const subMessage = { | ||
bidder: bid.bidder, | ||
params: (bid.params && Object.keys(bid.params).length > 0) ? bid.params : {}, | ||
} | ||
biddersSubMessages.set((adUnitCode, bid.bidder), subMessage) | ||
return subMessage | ||
}) | ||
}); | ||
}); | ||
|
||
// We enrich noBid then bids, then timeouts, because in case of a timeout, one response from a bidder | ||
// Can be in the 3 arrays, and we want that case reflected in the call | ||
noBids.forEach(bid => this.addBidResponseToMessage(message, bid, BIDDER_STATUS.NO_BID)); | ||
|
||
bidsReceived.forEach(bid => this.addBidResponseToMessage(message, bid, BIDDER_STATUS.BID)); | ||
|
||
timeoutBids.forEach(bid => this.addBidResponseToMessage(message, bid, BIDDER_STATUS.TIMEOUT)); | ||
logInfo(noBids) | ||
noBids.forEach(rqst => { | ||
Object.assign( | ||
biddersSubMessages.get((rqst.adUnitCode, rqst.bidder)) || {}, | ||
{ hasBid: false } | ||
) | ||
}) | ||
logInfo(bidsReceived) | ||
bidsReceived.forEach(bid => { | ||
Object.assign( | ||
biddersSubMessages.get((bid.adUnitCode, bid.bidder)) || {}, | ||
{ | ||
hasBid: true, | ||
cpm: bid.cpm, | ||
currency: bid.currency | ||
} | ||
) | ||
}) | ||
logInfo(timeoutBids) | ||
timeoutBids.forEach(badBid => { | ||
Object.assign( | ||
biddersSubMessages.get((badBid.adUnitCode, badBid.bidder)) || {}, | ||
{ isTimeout: true } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: seems that |
||
) | ||
}); | ||
|
||
return message; | ||
}, | ||
/** | ||
* @param {string} auctionId | ||
* @returns {GreenbidsCachedOption} | ||
*/ | ||
getCachedAuction(auctionId) { | ||
this.cachedAuctions[auctionId] = this.cachedAuctions[auctionId] || { | ||
timeoutBids: [], | ||
|
@@ -221,6 +240,9 @@ export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER | |
} | ||
cachedAuction.isSampled = isSampled(cachedAuction.greenbidsId, analyticsOptions.options.greenbidsSampling, this.exploratorySamplingSplit); | ||
}, | ||
/** | ||
* @param {AuctionEndArgs} auctionEndArgs | ||
*/ | ||
handleAuctionEnd(auctionEndArgs) { | ||
const cachedAuction = this.getCachedAuction(auctionEndArgs.auctionId); | ||
const isFilteringForced = getParameterByName('greenbids_force_filtering'); | ||
|
@@ -243,7 +265,7 @@ export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER | |
cachedAuction.billingId = billableArgs.billingId || 'unknown_billing_id'; | ||
} | ||
}, | ||
track({eventType, args}) { | ||
track({ eventType, args }) { | ||
try { | ||
if (eventType === AUCTION_INIT) { | ||
this.handleAuctionInit(args); | ||
|
@@ -273,7 +295,7 @@ export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER | |
|
||
greenbidsAnalyticsAdapter.originEnableAnalytics = greenbidsAnalyticsAdapter.enableAnalytics; | ||
|
||
greenbidsAnalyticsAdapter.enableAnalytics = function(config) { | ||
greenbidsAnalyticsAdapter.enableAnalytics = function (config) { | ||
this.initConfig(config); | ||
if (typeof config.options.sampling === 'number') { | ||
// Set sampling to 1 to prevent prebid analytics integrated sampling to happen | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: the auctionId is cached twice here. Could we cache it once at
handleAuctionInit
and then reuse?