Skip to content
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 dwid pairing when bid timeouted. #25

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions modules/fluctAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ const find = _find;
const url = 'https://an.adingo.jp';

/** @typedef {{ad: string, adId: string, adUnitCode: string, adUrl: string, adserverTargeting: any, auctionId: string, bidder: string, bidderCode: string, cpm: number, creativeId: string, currency: string, dealId: string, height: number, mediaType: string, netRevenue: boolean, originalCpm: number, originalCurrency: string, params: any, pbAg: string, pbCg: string, pbDg: string, pbHg: string, pbLg: string, pbMg: string, pbLg: string, requestId: string, requestTimestamp: number, responseTimestamp: number, size: string, source: string, status: string, statusMessage: string, timeToRespond: string, ttl: string, width: number, bidId?: string, params: any}} BidResponse */
/** @typedef {{bidder: string, params: any}} Bid */
/** @typedef {{adUnitCode: string, auctionId: string, bidId: string, bidder: string, bidderRequestId: number, dwid: string, mediaTypes: {banner: {name: string, sizes: number[][]}}} Bid */
/** @typedef {{code: string, _code?: string, dwid: string, path?: string, analytics?: {bidder: string, dwid: string}[], bids?: Bid[], mediaTypes: {banner: {name: string, sizes: number[][]}}, sizes: number[][], transactionId: string}} AdUnit */
/** @typedef {BidResponse & {noBid: boolean, prebidWon: boolean, bidWon: boolean, timeout: boolean}} ModBidResponse */
/** @typedef {{adUnitCodes: string[], adUnits: AdUnit[], auctionEnd: number, auctionId: string, auctionStatus: string, bidderRequests: any[], bidsReceived: BidResponse[], labels?: string, noBids: BidResponse[], timeout: number, timestamp: number, winningBids: BidResponse[], bids: {[requestId: string]: ModBidResponse}}} PbAuction */
/** @typedef {{registered: boolean}} Gpt */
/** @typedef {{[divId: string]: string }} Slots `{divId: adUnitPath}` */

/** @type {{auctions: {[auctionId: string]: PbAuction}, adUnits: {[adUnitCode: string]: AdUnit}, gpt: Gpt, timeouts: {[auctionId: string]: number}}} */
/** @type {{auctions: {[auctionId: string]: PbAuction}, adUnits: {[adUnitCode: string]: AdUnit}, gpt: Gpt, timeouts: {[auctionId: string]: number}, bidRequests: {[bidId: string]: Bid} }} */
const cache = {
auctions: {},
gpt: {},
timeouts: {},
bidRequests: {},
};
$$PREBID_GLOBAL$$.fluct = { cache: cache }; /** for debug */

Expand Down Expand Up @@ -108,6 +109,12 @@ let fluctAnalyticsAdapter = Object.assign(
}
break;
}
case EVENTS.BID_REQUESTED: {
/** @type {{ auctionId: string, auctionStart: number, bidderCode: string, bidderRequestId: string, bids: Bid[], refererInfo: {referer: string, reachedTop: boolean, isAmp: boolean, numIframes: number, stack: string[], start: number, timeout: number }}} */
let bidRequestedEvent = args;
bidRequestedEvent.bids.forEach(bid => { cache.bidRequests[bid.bidId] = bid });
break;
}
case EVENTS.BID_TIMEOUT: {
/** @type {BidResponse[]} */
let timeoutEvent = args;
Expand All @@ -118,14 +125,15 @@ let fluctAnalyticsAdapter = Object.assign(
prebidWon: false,
bidWon: false,
timeout: true,
dwid: (cache.bidRequests[bid.requestId] || {}).dwid,
};
});
break;
}
case EVENTS.AUCTION_END: {
/** @type {PbAuction} */
let auctionEndEvent = args;
let { adUnitCodes, auctionId, bidderRequests, bidsReceived, noBids } = auctionEndEvent;
let { adUnitCodes, auctionId, bidsReceived, noBids } = auctionEndEvent;
Object.assign(cache.auctions[auctionId], auctionEndEvent, { aidSuffix: isBrowsiId(auctionId) ? generateUUID() : undefined });

let prebidWonBidRequestIds = adUnitCodes.map(adUnitCode =>
Expand All @@ -136,17 +144,14 @@ let fluctAnalyticsAdapter = Object.assign(
, {}).requestId
);

const requestBids = []
bidderRequests.forEach(bidderRequest => bidderRequest.bids.forEach(bid => requestBids.push(bid)))

const bidResults = [
...bidsReceived.map(bid => ({
...bid,
noBid: false,
prebidWon: prebidWonBidRequestIds.includes(bid.requestId),
bidWon: false,
timeout: false,
dwid: (find(requestBids, requestBid => requestBid.bidId === bid.requestId) || {}).dwid, /** module版browsiには存在しない */
dwid: (cache.bidRequests[bid.requestId] || {}).dwid,
})),
...noBids.map(bid => ({
...bid,
Expand Down Expand Up @@ -205,7 +210,7 @@ const sendMessage = (auctionId) => {
auctionId: aidSuffix ? `${auctionId}_${aidSuffix}` : auctionId,
adUnits,
bids: Object.values(bids).map(bid => {
const { noBid, prebidWon, bidWon, timeout, dwid, adId, adUnitCode, adUrl, bidder, status, netRevenue, cpm, currency, originalCpm, originalCurrency, params, requestId, size, source, timeToRespond } = bid;
const { noBid, prebidWon, bidWon, timeout, dwid, adId, adUnitCode, adUrl, bidder, status, netRevenue, cpm, currency, originalCpm, originalCurrency, requestId, size, source, timeToRespond } = bid;
const adUnit = find(adUnits, adUnit => [adUnit._code, adUnit.code].includes(adUnitCode));

return {
Expand Down
13 changes: 13 additions & 0 deletions test/spec/modules/fluctAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ const MOCK = {
'auctionStatus': 'inProgress',
'adUnits': adUnits
},
BID_REQUESTED: {
'auctionId': 'eeca6754-525b-4c4c-a697-b06b1fc6c352',
'bidderRequestId': '9481bc7a501fb8',
'bids': [{
'bidder': adUnits[0].bids[0].bidder,
'dwid': adUnits[0].bids[0].dwid,
'params': adUnits[0].bids[0].params,
'adUnitCode': adUnits[0].code,
'bidId': '22697ff3e5bf7ee',
'bidderRequestId': '9481bc7a501fb8',
}]
},
AUCTION_END: {
'auctionId': 'eeca6754-525b-4c4c-a697-b06b1fc6c352',
'timestamp': 1635837149209,
Expand Down Expand Up @@ -215,6 +227,7 @@ describe('fluct analytics adapter', () => {
})
it('EVENT: `AUCTION_END` 時に値を送信できる', () => {
events.emit(EVENTS.AUCTION_INIT, MOCK.AUCTION_INIT)
events.emit(EVENTS.BID_REQUESTED, MOCK.BID_REQUESTED)
events.emit(EVENTS.AUCTION_END, MOCK.AUCTION_END)
expect(server.requests.length).to.equal(1)

Expand Down