Skip to content

Commit

Permalink
AdagioBidAdapter: prepare to work with Adagio RTD Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
osazos committed May 14, 2024
1 parent 802ef77 commit 08b8b27
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 76 deletions.
151 changes: 84 additions & 67 deletions modules/adagioBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {find} from '../src/polyfill.js';
import {
canAccessWindowTop,
cleanObj,
deepAccess,
deepClone,
Expand All @@ -8,17 +9,18 @@ import {
getUniqueIdentifierStr,
getWindowSelf,
getWindowTop,
inIframe,
isArray,
isArrayOfNums,
isFn,
inIframe,
isInteger,
isNumber,
isArrayOfNums,
isSafeFrameWindow,
isStr,
logError,
logInfo,
logWarn,
mergeDeep,
isStr,
} from '../src/utils.js';
import {config} from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
Expand Down Expand Up @@ -100,13 +102,15 @@ export const GlobalExchange = (function() {
getOrSetGlobalFeatures: function () {
if (!features) {
features = {
type: 'bidAdapter',
page_dimensions: getPageDimensions().toString(),
viewport_dimensions: getViewPortDimensions().toString(),
user_timestamp: getTimestampUTC().toString(),
dom_loading: getDomLoadingDuration().toString(),
}
}
return features;

return { ...features };
},

prepareExchangeData(storageValue) {
Expand All @@ -126,7 +130,7 @@ export const GlobalExchange = (function() {
const data = {
session: {
new: newSession,
rnd: random
rnd: random,
}
}

Expand All @@ -145,6 +149,9 @@ export const GlobalExchange = (function() {
};
})();

/**
* @deprecated will be removed in Prebid.js 9.
*/
export function adagioScriptFromLocalStorageCb(ls) {
try {
if (!ls) {
Expand Down Expand Up @@ -175,6 +182,9 @@ export function adagioScriptFromLocalStorageCb(ls) {
}
}

/**
* @deprecated will be removed in Prebid.js 9.
*/
export function getAdagioScript() {
storage.getDataFromLocalStorage(ADAGIO_LOCALSTORAGE_KEY, (ls) => {
internal.adagioScriptFromLocalStorageCb(ls);
Expand All @@ -200,31 +210,14 @@ export function getAdagioScript() {
});
}

function canAccessTopWindow() {
try {
if (getWindowTop().location.href) {
return true;
}
} catch (error) {
return false;
}
}

function getCurrentWindow() {
return currentWindow || getWindowSelf();
}

function isSafeFrameWindow() {
const ws = getWindowSelf();
return !!(ws.$sf && ws.$sf.ext);
}

function initAdagio() {
if (canAccessTopWindow()) {
currentWindow = (canAccessTopWindow()) ? getWindowTop() : getWindowSelf();
}
currentWindow = (canAccessWindowTop()) ? getWindowTop() : getWindowSelf();

const w = internal.getCurrentWindow();
const w = currentWindow;

w.ADAGIO = w.ADAGIO || {};
w.ADAGIO.adUnits = w.ADAGIO.adUnits || {};
Expand All @@ -236,13 +229,16 @@ function initAdagio() {

storage.getDataFromLocalStorage('adagio', (storageData) => {
try {
GlobalExchange.prepareExchangeData(storageData);
if (w.ADAGIO.hasRtd !== true) {
logInfo(`${LOG_PREFIX} RTD module not found. Loading external script from adagioBidAdapter is deprecated and will be removed in Prebid.js 9.`);

GlobalExchange.prepareExchangeData(storageData);
getAdagioScript();
}
} catch (e) {
logError(LOG_PREFIX, e);
}
});

getAdagioScript();
}

function enqueue(ob) {
Expand Down Expand Up @@ -355,6 +351,12 @@ function setPlayerName(bidRequest) {
return playerName;
}

function hasRtd() {
const w = internal.getCurrentWindow();

return !!(w.ADAGIO && w.ADAGIO.hasRtd);
};

export const internal = {
enqueue,
getPageviewId,
Expand All @@ -364,9 +366,10 @@ export const internal = {
getRefererInfo,
adagioScriptFromLocalStorageCb,
getCurrentWindow,
canAccessTopWindow,
canAccessWindowTop,
isRendererPreferredFromPublisher,
isNewSession
isNewSession,
hasRtd
};

function _getGdprConsent(bidderRequest) {
Expand Down Expand Up @@ -681,7 +684,7 @@ function autoFillParams(bid) {
}

function getPageDimensions() {
if (isSafeFrameWindow() || !canAccessTopWindow()) {
if (isSafeFrameWindow() || !canAccessWindowTop()) {
return '';
}

Expand All @@ -704,7 +707,7 @@ function getPageDimensions() {
* @returns
*/
function getViewPortDimensions() {
if (!isSafeFrameWindow() && !canAccessTopWindow()) {
if (!isSafeFrameWindow() && !canAccessWindowTop()) {
return '';
}

Expand Down Expand Up @@ -742,7 +745,7 @@ function getSlotPosition(adUnitElementId) {
return '';
}

if (!isSafeFrameWindow() && !canAccessTopWindow()) {
if (!isSafeFrameWindow() && !canAccessWindowTop()) {
return '';
}

Expand All @@ -765,7 +768,7 @@ function getSlotPosition(adUnitElementId) {

position.x = Math.round(sfGeom.t);
position.y = Math.round(sfGeom.l);
} else if (canAccessTopWindow()) {
} else if (canAccessWindowTop()) {
try {
// window.top based computing
const wt = getWindowTop();
Expand Down Expand Up @@ -819,22 +822,14 @@ function getTimestampUTC() {
return Math.floor(new Date().getTime() / 1000) - new Date().getTimezoneOffset() * 60;
}

function getPrintNumber(adUnitCode, bidderRequest) {
if (!bidderRequest.bids || !bidderRequest.bids.length) {
return 1;
}
const adagioBid = find(bidderRequest.bids, bid => bid.adUnitCode === adUnitCode);
return adagioBid.bidderRequestsCount || 1;
}

/**
* domLoading feature is computed on window.top if reachable.
*/
function getDomLoadingDuration() {
let domLoadingDuration = -1;
let performance;

performance = (canAccessTopWindow()) ? getWindowTop().performance : getWindowSelf().performance;
performance = (canAccessWindowTop()) ? getWindowTop().performance : getWindowSelf().performance;

if (performance && performance.timing && performance.timing.navigationStart > 0) {
const val = performance.timing.domLoading - performance.timing.navigationStart;
Expand Down Expand Up @@ -954,6 +949,31 @@ const OUTSTREAM_RENDERER = {
}
};

/**
*
* @param {*} bidRequest
* @returns
*/
const _getFeatures = (bidRequest) => {
const f = { ...deepAccess(bidRequest, 'ortb2.ext.features', GlobalExchange.getOrSetGlobalFeatures()) } || {};

f.print_number = deepAccess(bidRequest, 'bidderRequestsCount', 1).toString();

if (f.type === 'bidAdapter') {
f.adunit_position = getSlotPosition(bidRequest.params.adUnitElementId)
} else {
f.adunit_position = deepAccess(bidRequest, 'ortb2Imp.ext.data.adunit_position');
}

Object.keys(f).forEach((prop) => {
if (f[prop] === '') {
delete f[prop];
}
});

return f;
}

export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
Expand Down Expand Up @@ -981,6 +1001,7 @@ export const spec = {
const device = internal.getDevice();
const site = internal.getSite(bidderRequest);
const pageviewId = internal.getPageviewId();
const hasRtd = internal.hasRtd();
const gdprConsent = _getGdprConsent(bidderRequest) || {};
const uspConsent = _getUspConsent(bidderRequest) || {};
const coppa = _getCoppa();
Expand All @@ -993,6 +1014,9 @@ export const spec = {
// We don't validate the dsa object in adapter and let our server do it.
const dsa = deepAccess(bidderRequest, 'ortb2.regs.ext.dsa');

let rtdSamplingSession = deepAccess(bidderRequest, 'ortb2.ext.session');
const dataExchange = (rtdSamplingSession) ? { session: rtdSamplingSession } : GlobalExchange.getExchangeData();

const aucId = generateUUID()

const adUnits = validBidRequests.map(rawBidRequest => {
Expand All @@ -1001,13 +1025,6 @@ export const spec = {
// Fix https://github.com/prebid/Prebid.js/issues/9781
bidRequest.auctionId = aucId

const globalFeatures = GlobalExchange.getOrSetGlobalFeatures();
const features = {
...globalFeatures,
print_number: getPrintNumber(bidRequest.adUnitCode, bidderRequest).toString(),
adunit_position: getSlotPosition(bidRequest.params.adUnitElementId) // adUnitElementId à déplacer ???
};

// Force the Split Keyword to be a String
if (bidRequest.params.splitKeyword) {
if (isStr(bidRequest.params.splitKeyword) || isNumber(bidRequest.params.splitKeyword)) {
Expand Down Expand Up @@ -1051,23 +1068,20 @@ export const spec = {
}
}

Object.keys(features).forEach((prop) => {
if (features[prop] === '') {
delete features[prop];
}
});

const features = _getFeatures(bidRequest);
bidRequest.features = features;

internal.enqueue({
action: 'features',
ts: Date.now(),
data: {
features: bidRequest.features,
params: bidRequest.params,
adUnitCode: bidRequest.adUnitCode
}
});
if (!hasRtd) {
internal.enqueue({
action: 'features',
ts: Date.now(),
data: {
features,
params: { ...bidRequest.params },
adUnitCode: bidRequest.adUnitCode
}
});
}

// Handle priceFloors module
// We need to use `rawBidRequest` as param because:
Expand Down Expand Up @@ -1122,8 +1136,10 @@ export const spec = {
bidRequest.gpid = gpid;
}

// store the whole bidRequest (adUnit) object in the ADAGIO namespace.
storeRequestInAdagioNS(bidRequest);
if (!hasRtd) {
// store the whole bidRequest (adUnit) object in the ADAGIO namespace.
storeRequestInAdagioNS(bidRequest);
}

// Remove some params that are not needed on the server side.
delete bidRequest.params.siteId;
Expand Down Expand Up @@ -1171,12 +1187,13 @@ export const spec = {
url: ENDPOINT,
data: {
organizationId: organizationId,
hasRtd: hasRtd ? 1 : 0,
secure: secure,
device: device,
site: site,
pageviewId: pageviewId,
adUnits: groupedAdUnits[organizationId],
data: GlobalExchange.getExchangeData(),
data: dataExchange,
regs: {
gdpr: gdprConsent,
coppa: coppa,
Expand Down
Loading

0 comments on commit 08b8b27

Please sign in to comment.