Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Abyfall committed Jul 8, 2024
1 parent 30ee69a commit a1099a7
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 297 deletions.
76 changes: 40 additions & 36 deletions modules/adagioAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import adapterManager from '../src/adapterManager.js';
import { EVENTS } from '../src/constants.js';
import { ajax } from '../src/ajax.js';
import { BANNER } from '../src/mediaTypes.js';
import { getWindowTop, getWindowSelf, deepAccess, logInfo, logError } from '../src/utils.js';
import { getWindowTop, getWindowSelf, deepAccess, logWarn, logError } from '../src/utils.js';
import { getGlobal } from '../src/prebidGlobal.js';
import { config } from '../src/config.js';

Expand Down Expand Up @@ -192,12 +192,7 @@ function handlerAuctionInit(event) {
const adUnitCodes = removeDuplicates(event.adUnitCodes, adUnitCode => adUnitCode);

// Check if Adagio is on the bid requests.
// If not, we don't need to track the auction.
const adagioBidRequest = event.bidderRequests.find(bidRequest => isAdagio(bidRequest.bidderCode));
if (adagioAdapter.options.analyzeAdagioOnly && !adagioBidRequest) {
logInfo(`Adagio is not on the bid requests for auction '${prebidAuctionId}'`)
return;
}

cache.auctions[prebidAuctionId] = {};

Expand All @@ -210,14 +205,6 @@ function handlerAuctionInit(event) {
bidder => bidder.bidder
);

// Check if Adagio is configured for the ad unit.
// If not, we don't need to track the ad unit.
const adagioBidder = bidders.find(bidder => isAdagio(bidder.bidder));
if (adagioAdapter.options.analyzeAdagioOnly && !adagioBidder) {
logInfo(`Adagio is not configured for ad unit '${adUnitCode}'`);
return;
}

// Get all media types and banner sizes configured for the ad unit.
const mediaTypes = adUnits.map(adUnit => adUnit.mediaTypes);
const mediaTypesKeys = removeDuplicates(
Expand All @@ -231,9 +218,17 @@ function handlerAuctionInit(event) {
bannerSize => bannerSize
).sort();

let sortedBidderCodes = bidders.map(bidder => bidder.bidder).sort()

const bidSrcMapper = (bidder) => {
const request = event.bidderRequests.find(br => br.bidderCode === bidder)
return request ? request.bids[0].src : null
}
let biddersSrc = sortedBidderCodes.map(bidSrcMapper).join(',');

let qp = {
org_id: adagioAdapter.options.organizationId,
site: adagioAdapter.options.site,
org_id: adagioAdapter.options.organizationId, // Todo fallback on params
site: adagioAdapter.options.site, // Todo fallback on params
v: 0,
pbjsv: PREBID_VERSION,
pv_id: w.ADAGIO.pageviewId,
Expand All @@ -242,32 +237,29 @@ function handlerAuctionInit(event) {
url_dmn: w.location.hostname,
mts: mediaTypesKeys.join(','),
ban_szs: bannerSizes.join(','),
bdrs: bidders.map(bidder => bidder.bidder).sort().join(','),
bdrs: sortedBidderCodes.join(','),
pgtyp: deepAccess(event.bidderRequests[0], 'ortb2.site.ext.data.pagetype', null),
plcmt: deepAccess(adUnits[0], 'ortb2Imp.ext.data.placement', null),
t_n: deepAccess(event.bidderRequests[0], 'ortb2.site.ext.data.adg_rtd.session.testName', null),
t_v: deepAccess(event.bidderRequests[0], 'ortb2.site.ext.data.adg_rtd.session.testVersion', null),
s_id: deepAccess(event.bidderRequests[0], 'ortb2.site.ext.data.adg_rtd.session.sessionId', null),
s_new: deepAccess(event.bidderRequests[0], 'ortb2.site.ext.data.adg_rtd.session.new', null),
bdrs_src: biddersSrc,
};

if (adagioBidRequest && adagioBidRequest.bids) {
const adagioAdUnitBids = adagioBidRequest.bids.filter(bid => bid.adUnitCode === adUnitCode);
if (adagioAdUnitBids.length <= 0) {
if (adagioAdapter.options.analyzeAdagioOnly) {
// In analyzeAdagioOnly mode, if no bids, we don't need to track the ad unit.
logInfo(`Adagio is not on the bid requests for ad unit '${adUnitCode}' and auction '${prebidAuctionId}'`)
return;
}
} else {
if (adagioAdUnitBids.length > 0) {
// Get all media types requested for Adagio.
const adagioMediaTypes = removeDuplicates(
adagioAdUnitBids.map(bid => Object.keys(bid.mediaTypes)).flat(),
mediaTypeKey => mediaTypeKey
).flat().map(mediaType => getMediaTypeAlias(mediaType)).sort();

qp = {...qp,
adg_mts: adagioMediaTypes.join(',')
};
qp.adg_mts = adagioMediaTypes.join(',');
// if we didn't find organizationId & site but we have a bid from adagio we might still find it in params
qp.org_id = qp.org_id || adagioAdUnitBids[0].params.organizationId;
qp.site = qp.site || adagioAdUnitBids[0].params.site;
}
}

Expand Down Expand Up @@ -319,11 +311,17 @@ function handlerAuctionEnd(event) {

cache.updateAuction(auctionId, adUnitCode, {
bdrs_bid: cache.getBiddersFromAuction(auctionId, adUnitCode).map(bidResponseMapper).join(','),
bdrs_cpm: cache.getBiddersFromAuction(auctionId, adUnitCode).map(bidCpmMapper).join(',')
bdrs_cpm: cache.getBiddersFromAuction(auctionId, adUnitCode).map(bidCpmMapper).join(','),
// check timings at the end of the auction to leave time to the browser to update it
loa_e: Math.round(performance.getEntriesByType('navigation')[0]['loadEventEnd']),
dom_i: Math.round(performance.getEntriesByType('navigation')[0]['domInteractive']),
dom_c: Math.round(performance.getEntriesByType('navigation')[0]['domComplete']),
});

sendNewBeacon(auctionId, adUnitCode);
});
}

function handlerBidWon(event) {
let auctionId = getTargetedAuctionId(event);

Expand Down Expand Up @@ -414,6 +412,11 @@ adagioAdapter.enableAnalytics = adapterConfig => {
logError('Adagio Analytics Adapter requires prebid settings enableTIDs to be true. No beacon will be sent');
return;
}
let modules = getGlobal().installedModules;
if (modules && (modules.length == 0 || modules.indexOf('adagioRtdProvider') == -1)) {
logError('Adagio Analytics Adapter requires adagioRtdProvider module which is not installed. No beacon will be sent');
return;
}

const w = (canAccessTopWindow()) ? getWindowTop() : getWindowSelf();
currentWindow = w;
Expand All @@ -423,15 +426,16 @@ adagioAdapter.enableAnalytics = adapterConfig => {
w.ADAGIO.versions = w.ADAGIO.versions || {};
w.ADAGIO.versions.adagioAnalyticsAdapter = VERSION;
adagioAdapter.options = adapterConfig.options || {};
adagioAdapter.options.analyzeAdagioOnly = typeof adagioAdapter.options.analyzeAdagioOnly === 'boolean' ? adagioAdapter.options.analyzeAdagioOnly : true;
if (!adagioAdapter.options.organizationId || !adagioAdapter.options.site) {
logError('Adagio Analytics Adapter: organizationId and site are required');
return;
if (!adagioAdapter.options.organizationId) {
logWarn('Adagio Analytics Adapter: organizationId is required and is missing will try to fallback on params.');
} else {
adagioAdapter.options.organizationId = adagioAdapter.options.organizationId.toString(); // allows publisher to pass it as a number
}
adagioAdapter.options.organizationId = adagioAdapter.options.organizationId.toString(); // allows publisher to pass it as a number
if (typeof adagioAdapter.options.site !== 'string') {
logError('Adagio Analytics Adapter: site should be a string');
return;
if (!adagioAdapter.options.site) {
logWarn('Adagio Analytics Adapter: site is required and is missing will try to fallback on params.');
} else if (typeof adagioAdapter.options.site !== 'string') {
logWarn('Adagio Analytics Adapter: site should be a string will try to fallback on params.');
adagioAdapter.options.site = undefined;
}
adagioAdapter.originEnableAnalytics(adapterConfig);
}
Expand Down
Loading

0 comments on commit a1099a7

Please sign in to comment.