Skip to content

Commit

Permalink
PubMatic Analytics Adapter : log floor values only when floor file is…
Browse files Browse the repository at this point in the history
… fetched successfully (#10328)

* Log floor values only when floor data is present

* Added test cases when floor location is other than fetch

* Added comment
  • Loading branch information
kapil-tuptewar authored Aug 10, 2023
1 parent 3bbe63b commit 04c7570
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
19 changes: 16 additions & 3 deletions modules/pubmaticAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function gatherPartnerBidsForAdUnitForLogger(adUnit, adUnitId, highestBid) {
'ocpm': bid.bidResponse ? (bid.bidResponse.originalCpm || 0) : 0,
'ocry': bid.bidResponse ? (bid.bidResponse.originalCurrency || CURRENCY_USD) : CURRENCY_USD,
'piid': bid.bidResponse ? (bid.bidResponse.partnerImpId || EMPTY_STRING) : EMPTY_STRING,
'frv': (s2sBidders.indexOf(bid.bidder) > -1) ? undefined : (bid.bidResponse ? (bid.bidResponse.floorData ? bid.bidResponse.floorData.floorRuleValue : undefined) : undefined),
'frv': (bid.bidResponse ? (bid.bidResponse.floorData ? bid.bidResponse.floorData.floorRuleValue : undefined) : undefined),
'md': bid.bidResponse ? getMetadata(bid.bidResponse.meta) : undefined
});
});
Expand Down Expand Up @@ -319,13 +319,26 @@ function getTgId() {
return 0;
}

function getFloorFetchStatus(floorData) {
if (!floorData?.floorRequestData) {
return false;
}
const { location, fetchStatus } = floorData?.floorRequestData;
const isDataValid = location !== CONSTANTS.FLOOR_VALUES.NO_DATA;
const isFetchSuccessful = location === CONSTANTS.FLOOR_VALUES.FETCH && fetchStatus === CONSTANTS.FLOOR_VALUES.SUCCESS;
const isAdUnitOrSetConfig = location === CONSTANTS.FLOOR_VALUES.AD_UNIT || location === CONSTANTS.FLOOR_VALUES.SET_CONFIG;
return isDataValid && (isAdUnitOrSetConfig || isFetchSuccessful);
}

function executeBidsLoggerCall(e, highestCpmBids) {
let auctionId = e.auctionId;
let referrer = config.getConfig('pageUrl') || cache.auctions[auctionId].referer || '';
let auctionCache = cache.auctions[auctionId];
let floorData = auctionCache.floorData;
let outputObj = { s: [] };
let pixelURL = END_POINT_BID_LOGGER;
// will return true if floor data is present.
let fetchStatus = getFloorFetchStatus(auctionCache.floorData);

if (!auctionCache) {
return;
Expand All @@ -347,7 +360,7 @@ function executeBidsLoggerCall(e, highestCpmBids) {
outputObj['dvc'] = {'plt': getDevicePlatform()};
outputObj['tgid'] = getTgId();

if (floorData) {
if (floorData && fetchStatus) {
outputObj['fmv'] = floorData.floorRequestData ? floorData.floorRequestData.modelVersion || undefined : undefined;
outputObj['ft'] = floorData.floorResponseData ? (floorData.floorResponseData.enforcements.enforceJS == false ? 0 : 1) : undefined;
}
Expand All @@ -362,7 +375,7 @@ function executeBidsLoggerCall(e, highestCpmBids) {
'mt': getAdUnitAdFormats(origAdUnit),
'sz': getSizesForAdUnit(adUnit, adUnitId),
'ps': gatherPartnerBidsForAdUnitForLogger(adUnit, adUnitId, highestCpmBids.filter(bid => bid.adUnitCode === adUnitId)),
'fskp': floorData ? (floorData.floorRequestData ? (floorData.floorRequestData.skipped == false ? 0 : 1) : undefined) : undefined,
'fskp': (floorData && fetchStatus) ? (floorData.floorRequestData ? (floorData.floorRequestData.skipped == false ? 0 : 1) : undefined) : undefined,
};
slotsArray.push(slotObject);
return slotsArray;
Expand Down
9 changes: 8 additions & 1 deletion src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,12 @@
"adTemplate",
"rendererUrl",
"type"
]
],
"FLOOR_VALUES": {
"NO_DATA": "noData",
"AD_UNIT": "adUnit",
"SET_CONFIG": "setConfig",
"FETCH": "fetch",
"SUCCESS": "success"
}
}
104 changes: 96 additions & 8 deletions test/spec/modules/pubmaticAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[0].ps[0].af).to.equal('video');
expect(data.s[0].ps[0].ocpm).to.equal(1.23);
expect(data.s[0].ps[0].ocry).to.equal('USD');
expect(data.s[0].ps[0].frv).to.equal(undefined);
expect(data.s[0].ps[0].frv).to.equal(1.1);
// slot 2
expect(data.s[1].sn).to.equal('/19968336/header-bid-tag-1');
expect(data.s[1].fskp).to.equal(0);
Expand Down Expand Up @@ -422,7 +422,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[1].ps[0].af).to.equal('banner');
expect(data.s[1].ps[0].ocpm).to.equal(1.52);
expect(data.s[1].ps[0].ocry).to.equal('USD');
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);

// tracker slot1
let firstTracker = requests[0].url;
Expand Down Expand Up @@ -452,6 +452,94 @@ describe('pubmatic analytics adapter', function () {
expect(data.af).to.equal('video');
});

it('Logger: do not log floor fields when prebids floor shows noData in location property', function() {
const BID_REQUESTED_COPY = utils.deepClone(MOCK.BID_REQUESTED);
BID_REQUESTED_COPY['bids'][1]['floorData']['location'] = 'noData';

this.timeout(5000)

sandbox.stub($$PREBID_GLOBAL$$, 'getHighestCpmBids').callsFake((key) => {
return [MOCK.BID_RESPONSE[0], MOCK.BID_RESPONSE[1]]
});

config.setConfig({
testGroupId: 15
});

events.emit(AUCTION_INIT, MOCK.AUCTION_INIT);
events.emit(BID_REQUESTED, BID_REQUESTED_COPY);
events.emit(BID_RESPONSE, MOCK.BID_RESPONSE[0]);
events.emit(BID_RESPONSE, MOCK.BID_RESPONSE[1]);
events.emit(BIDDER_DONE, MOCK.BIDDER_DONE);
events.emit(AUCTION_END, MOCK.AUCTION_END);
events.emit(SET_TARGETING, MOCK.SET_TARGETING);
events.emit(BID_WON, MOCK.BID_WON[0]);
events.emit(BID_WON, MOCK.BID_WON[1]);

clock.tick(2000 + 1000);
expect(requests.length).to.equal(3); // 1 logger and 2 win-tracker
let request = requests[2]; // logger is executed late, trackers execute first
expect(request.url).to.equal('https://t.pubmatic.com/wl?pubid=9999');

let data = getLoggerJsonFromRequest(request.requestBody);

expect(data.pubid).to.equal('9999');
expect(data.fmv).to.equal(undefined);

// slot 1
expect(data.s[0].sn).to.equal('/19968336/header-bid-tag-0');
expect(data.s[0].au).to.equal('/19968336/header-bid-tag-0');

// slot 2
expect(data.s[1].sn).to.equal('/19968336/header-bid-tag-1');
expect(data.s[1].au).to.equal('/19968336/header-bid-tag-1');
expect(data.s[1].fskp).to.equal(undefined);
});

it('Logger: log floor fields when prebids floor shows setConfig in location property', function() {
const BID_REQUESTED_COPY = utils.deepClone(MOCK.BID_REQUESTED);
BID_REQUESTED_COPY['bids'][1]['floorData']['location'] = 'setConfig';

this.timeout(5000)

sandbox.stub($$PREBID_GLOBAL$$, 'getHighestCpmBids').callsFake((key) => {
return [MOCK.BID_RESPONSE[0], MOCK.BID_RESPONSE[1]]
});

config.setConfig({
testGroupId: 15
});

events.emit(AUCTION_INIT, MOCK.AUCTION_INIT);
events.emit(BID_REQUESTED, BID_REQUESTED_COPY);
events.emit(BID_RESPONSE, MOCK.BID_RESPONSE[0]);
events.emit(BID_RESPONSE, MOCK.BID_RESPONSE[1]);
events.emit(BIDDER_DONE, MOCK.BIDDER_DONE);
events.emit(AUCTION_END, MOCK.AUCTION_END);
events.emit(SET_TARGETING, MOCK.SET_TARGETING);
events.emit(BID_WON, MOCK.BID_WON[0]);
events.emit(BID_WON, MOCK.BID_WON[1]);

clock.tick(2000 + 1000);
expect(requests.length).to.equal(3); // 1 logger and 2 win-tracker
let request = requests[2]; // logger is executed late, trackers execute first
expect(request.url).to.equal('https://t.pubmatic.com/wl?pubid=9999');

let data = getLoggerJsonFromRequest(request.requestBody);

expect(data.pubid).to.equal('9999');
expect(data.fmv).to.equal('floorModelTest');

// slot 1
expect(data.s[0].sn).to.equal('/19968336/header-bid-tag-0');
expect(data.s[0].au).to.equal('/19968336/header-bid-tag-0');

// slot 2
expect(data.s[1].sn).to.equal('/19968336/header-bid-tag-1');
expect(data.s[1].au).to.equal('/19968336/header-bid-tag-1');
expect(data.s[1].fskp).to.equal(0);
});

it('bidCpmAdjustment: USD: Logger: best case + win tracker', function() {
const bidCopy = utils.deepClone(BID);
bidCopy.cpm = bidCopy.originalCpm * 2; // bidCpmAdjustment => bidCpm * 2
Expand Down Expand Up @@ -501,7 +589,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[0].ps[0].af).to.equal('video');
expect(data.s[0].ps[0].ocpm).to.equal(1.23);
expect(data.s[0].ps[0].ocry).to.equal('USD');
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);
// tracker slot1
let firstTracker = requests[0].url;
expect(firstTracker.split('?')[0]).to.equal('https://t.pubmatic.com/wt');
Expand Down Expand Up @@ -711,7 +799,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[1].ps[0].af).to.equal('banner');
expect(data.s[1].ps[0].ocpm).to.equal(1.52);
expect(data.s[1].ps[0].ocry).to.equal('USD');
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);
});

it('Logger: currency conversion check', function() {
Expand Down Expand Up @@ -818,7 +906,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[1].ps[0].af).to.equal('banner');
expect(data.s[1].ps[0].ocpm).to.equal(1.52);
expect(data.s[1].ps[0].ocry).to.equal('USD');
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);
expect(data.dvc).to.deep.equal({'plt': 2});
// respective tracker slot
let firstTracker = requests[1].url;
Expand Down Expand Up @@ -876,7 +964,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[1].ps[0].ocpm).to.equal(1.52);
expect(data.s[1].ps[0].ocry).to.equal('USD');
expect(data.dvc).to.deep.equal({'plt': 1});
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);
// respective tracker slot
let firstTracker = requests[1].url;
expect(firstTracker.split('?')[0]).to.equal('https://t.pubmatic.com/wt');
Expand Down Expand Up @@ -929,7 +1017,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[1].ps[0].af).to.equal('banner');
expect(data.s[1].ps[0].ocpm).to.equal(1.52);
expect(data.s[1].ps[0].ocry).to.equal('USD');
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);
// respective tracker slot
let firstTracker = requests[1].url;
expect(firstTracker.split('?')[0]).to.equal('https://t.pubmatic.com/wt');
Expand Down Expand Up @@ -1091,7 +1179,7 @@ describe('pubmatic analytics adapter', function () {
expect(data.s[1].ps[0].af).to.equal('banner');
expect(data.s[1].ps[0].ocpm).to.equal(1.52);
expect(data.s[1].ps[0].ocry).to.equal('USD');
expect(data.s[1].ps[0].frv).to.equal(undefined);
expect(data.s[1].ps[0].frv).to.equal(1.1);

// tracker slot1
let firstTracker = requests[0].url;
Expand Down

0 comments on commit 04c7570

Please sign in to comment.