Skip to content

Commit

Permalink
NextMillennium Bid Adapter : sending a request with several imp objec…
Browse files Browse the repository at this point in the history
…ts (#12244)

* added support for gpp consent string

* changed test for nextMillenniumBidAdapter

* added some tests

* added site.pagecat, site.content.cat and site.content.language to request

* lint fix

* formated code

* formated code

* formated code

* pachage-lock with prebid

* pachage-lock with prebid

* formatted code

* added device.sua, user.eids

* formatted

* fixed tests

* fixed bug functio getSua

* NextMillennium: Sending a request with several imp objects.
  • Loading branch information
mhlm authored Sep 19, 2024
1 parent 6d2e37a commit 24516d3
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 199 deletions.
169 changes: 80 additions & 89 deletions modules/nextMillenniumBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {
} from '../src/utils.js';
import {getAd} from '../libraries/targetVideoUtils/bidderUtils.js';

import {getGlobal} from '../src/prebidGlobal.js';
import { EVENTS } from '../src/constants.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {config} from '../src/config.js';

import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getRefererInfo} from '../src/refererDetection.js';

const NM_VERSION = '3.1.0';
const NM_VERSION = '4.0.0';
const PBJS_VERSION = 'v$prebid.version$';
const GVLID = 1060;
const BIDDER_CODE = 'nextMillennium';
const ENDPOINT = 'https://pbs.nextmillmedia.com/openrtb2/auction';
Expand Down Expand Up @@ -81,65 +81,46 @@ export const spec = {
buildRequests: function(validBidRequests, bidderRequest) {
const requests = [];
window.nmmRefreshCounts = window.nmmRefreshCounts || {};
const site = getSiteObj();
const device = getDeviceObj();

_each(validBidRequests, (bid) => {
window.nmmRefreshCounts[bid.adUnitCode] = window.nmmRefreshCounts[bid.adUnitCode] || 0;
const id = getPlacementId(bid);
const auctionId = bid.auctionId;
const bidId = bid.bidId;

const site = getSiteObj();
const device = getDeviceObj();
const {cur, mediaTypes} = getCurrency(bid);
const postBody = {
id: bidderRequest?.bidderRequestId,
ext: {
next_mil_imps: [],
},

const postBody = {
id: bidderRequest?.bidderRequestId,
cur,
ext: {
prebid: {
storedrequest: {
id,
},
},
device,
site,
imp: [],
};

nextMillennium: {
nm_version: NM_VERSION,
pbjs_version: getGlobal()?.version || undefined,
refresh_count: window.nmmRefreshCounts[bid.adUnitCode]++,
elOffsets: getBoundingClient(bid),
scrollTop: window.pageYOffset || document.documentElement.scrollTop,
},
},
setConsentStrings(postBody, bidderRequest);
setOrtb2Parameters(postBody, bidderRequest?.ortb2);

device,
site,
imp: [],
};
const urlParameters = parseUrl(getWindowTop().location.href).search;
const isTest = urlParameters['pbs'] && urlParameters['pbs'] === 'test';
setEids(postBody, validBidRequests);

_each(validBidRequests, (bid, i) => {
window.nmmRefreshCounts[bid.adUnitCode] = window.nmmRefreshCounts[bid.adUnitCode] || 0;
const id = getPlacementId(bid);
const {cur, mediaTypes} = getCurrency(bid);
if (i === 0) postBody.cur = cur;
postBody.imp.push(getImp(bid, id, mediaTypes));
setConsentStrings(postBody, bidderRequest);
setOrtb2Parameters(postBody, bidderRequest?.ortb2);
setEids(postBody, bid);

const urlParameters = parseUrl(getWindowTop().location.href).search;
const isTest = urlParameters['pbs'] && urlParameters['pbs'] === 'test';
const params = bid.params;

requests.push({
method: 'POST',
url: isTest ? TEST_ENDPOINT : ENDPOINT,
data: JSON.stringify(postBody),
options: {
contentType: 'text/plain',
withCredentials: true,
},
postBody.ext.next_mil_imps.push(getExtNextMilImp(bid));
});

bidId,
params,
auctionId,
});
this.getUrlPixelMetric(EVENTS.BID_REQUESTED, validBidRequests);

this.getUrlPixelMetric(EVENTS.BID_REQUESTED, bid);
requests.push({
method: 'POST',
url: isTest ? TEST_ENDPOINT : ENDPOINT,
data: JSON.stringify(postBody),
options: {
contentType: 'text/plain',
withCredentials: true,
},
});

return requests;
Expand All @@ -149,16 +130,15 @@ export const spec = {
const response = serverResponse.body;
const bidResponses = [];

const bids = [];
_each(response.seatbid, (resp) => {
_each(resp.bid, (bid) => {
const requestId = bidRequest.bidId;
const params = bidRequest.params;

const {ad, adUrl, vastUrl, vastXml} = getAd(bid);

const bidResponse = {
requestId,
params,
cpm: bid.price,
width: bid.w,
height: bid.h,
Expand All @@ -182,11 +162,13 @@ export const spec = {
};

bidResponses.push(bidResponse);

this.getUrlPixelMetric(EVENTS.BID_RESPONSE, bid);
});

bids.push(resp.bid);
});

this.getUrlPixelMetric(EVENTS.BID_RESPONSE, bids.flat());

return bidResponses;
},

Expand Down Expand Up @@ -226,22 +208,26 @@ export const spec = {
triggerPixel(url);
},

_getUrlPixelMetric(eventName, bid) {
const bidder = bid.bidder || bid.bidderCode;
_getUrlPixelMetric(eventName, bids) {
if (!Array.isArray(bids)) bids = [bids];

const bidder = bids[0]?.bidder || bids[0]?.bidderCode;
if (bidder != BIDDER_CODE) return;

let params;
if (bid.params) {
params = Array.isArray(bid.params) ? bid.params : [bid.params];
} else {
if (Array.isArray(bid.bids)) params = bid.bids.map(bidI => bidI.params);
};
let params = [];
_each(bids, bid => {
if (bid.params) {
params.push(bid.params);
} else {
if (Array.isArray(bid.bids)) params.push(bid.bids.map(bidI => bidI.params));
};
});

if (!params.length) return;

const placementIdsArray = [];
const groupIdsArray = [];
params.forEach(paramsI => {
params.flat().forEach(paramsI => {
if (paramsI.group_id) {
groupIdsArray.push(paramsI.group_id);
} else {
Expand All @@ -252,9 +238,7 @@ export const spec = {
const placementIds = (placementIdsArray.length && `&placements=${placementIdsArray.join(';')}`) || '';
const groupIds = (groupIdsArray.length && `&groups=${groupIdsArray.join(';')}`) || '';

if (!(groupIds || placementIds)) {
return;
};
if (!(groupIds || placementIds)) return;

const url = `${REPORT_ENDPOINT}?event=${eventName}&bidder=${bidder}&source=pbjs${groupIds}${placementIds}`;

Expand All @@ -268,6 +252,21 @@ export const spec = {
},
};

function getExtNextMilImp(bid) {
if (typeof window?.nmmRefreshCounts[bid.adUnitCode] === 'number') ++window.nmmRefreshCounts[bid.adUnitCode];
const nextMilImp = {
impId: bid.adUnitCode,
nextMillennium: {
nm_version: NM_VERSION,
pbjs_version: PBJS_VERSION,
refresh_count: window?.nmmRefreshCounts[bid.adUnitCode] || 0,
scrollTop: window.pageYOffset || document.documentElement.scrollTop,
},
};

return nextMilImp;
}

export function getImp(bid, id, mediaTypes) {
const {banner, video} = mediaTypes;
const imp = {
Expand Down Expand Up @@ -362,10 +361,16 @@ export function setOrtb2Parameters(postBody, ortb2 = {}) {
}
}

export function setEids(postBody, bid) {
if (!isArray(bid.userIdAsEids) || !bid.userIdAsEids.length) return;
export function setEids(postBody = {}, bids = []) {
let isFind = false;
_each(bids, bid => {
if (isFind || !isArray(bid.userIdAsEids) || !bid.userIdAsEids.length) return;

deepSetValue(postBody, 'user.eids', bid.userIdAsEids);
if (bid.userIdAsEids.length) {
deepSetValue(postBody, 'user.eids', bid.userIdAsEids);
isFind = true;
};
});
}

export function replaceUsersyncMacros(url, gdprConsent = {}, uspConsent = '', gppConsent = {}, type = '') {
Expand Down Expand Up @@ -411,30 +416,16 @@ function getCurrency(bid = {}) {
return {cur, mediaTypes};
}

function getAdEl(bid) {
// best way I could think of to get El, is by matching adUnitCode to google slots...
const slot = window.googletag && window.googletag.pubads && window.googletag.pubads().getSlots().find(slot => slot.getAdUnitPath() === bid.adUnitCode);
const slotElementId = slot && slot.getSlotElementId();
if (!slotElementId) return null;
return document.querySelector('#' + slotElementId);
}

function getBoundingClient(bid) {
const el = getAdEl(bid);
if (!el) return {};
return el.getBoundingClientRect();
}

function getPlacementId(bid) {
export function getPlacementId(bid) {
const groupId = getBidIdParameter('group_id', bid.params);
const placementId = getBidIdParameter('placement_id', bid.params);
if (!groupId) return placementId;

let windowTop = getTopWindow(window);
let sizes = [];
if (bid.mediaTypes) {
if (bid.mediaTypes.banner) sizes = bid.mediaTypes.banner.sizes;
if (bid.mediaTypes.video) sizes = [bid.mediaTypes.video.playerSize];
if (bid.mediaTypes.banner) sizes = [...bid.mediaTypes.banner.sizes];
if (bid.mediaTypes.video) sizes.push(bid.mediaTypes.video.playerSize);
};

const host = (windowTop && windowTop.location && windowTop.location.host) || '';
Expand Down
Loading

0 comments on commit 24516d3

Please sign in to comment.