Skip to content

Commit

Permalink
AdagioAnalyticsAdapter: send PBA for all auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
Abyfall committed Jul 11, 2024
1 parent 85f0731 commit 9db844f
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 94 deletions.
104 changes: 62 additions & 42 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';

const emptyUrl = '';
Expand Down Expand Up @@ -200,12 +200,8 @@ 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 (!adagioBidRequest) {
logInfo(`Adagio is not on the bid requests for auction '${prebidAuctionId}'`)
return;
}

const rtdUid = deepAccess(adagioBidRequest, 'ortb2.site.ext.data.adg_rtd.uid');
cache.addPrebidAuctionIdRef(prebidAuctionId, rtdUid);

Expand All @@ -214,20 +210,12 @@ function handlerAuctionInit(event) {
adUnitCodes.forEach(adUnitCode => {
const adUnits = event.adUnits.filter(adUnit => adUnit.code === adUnitCode);

// Get all bidders configures for the ad unit.
// Get all bidders configured for the ad unit.
const bidders = removeDuplicates(
adUnits.map(adUnit => adUnit.bids.map(bid => ({bidder: bid.bidder, params: bid.params}))).flat(),
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 (!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 @@ -241,45 +229,54 @@ function handlerAuctionInit(event) {
bannerSize => bannerSize
).sort();

// Get all Adagio bids for the ad unit from the bidRequest.
// If no bids, we don't need to track the ad unit.
const adagioAdUnitBids = adagioBidRequest.bids.filter(bid => bid.adUnitCode === adUnitCode);
if (deepAccess(adagioAdUnitBids, 'length', 0) <= 0) {
logInfo(`Adagio is not on the bid requests for ad unit '${adUnitCode}' and auction '${prebidAuctionId}'`)
return;
}
// Get Adagio params from the first bid.
// We assume that all Adagio bids for a same adunit have the same params.
const params = adagioAdUnitBids[0].params;
let sortedBidderCodes = bidders.map(bidder => bidder.bidder).sort()

// 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();
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(',');

// if adagio was involved in the auction we identified it with rtdUid, if not use the prebid auctionId
let auctionId = rtdUid || prebidAuctionId;

const qp = {
let qp = {
org_id: adagioAdapter.options.organizationId,
site: adagioAdapter.options.site,
v: 0,
pbjsv: PREBID_VERSION,
org_id: params.organizationId,
site: params.site,
pv_id: params.pageviewId,
pv_id: w.ADAGIO.pageviewId,
auct_id: auctionId,
adu_code: adUnitCode,
url_dmn: w.location.hostname,
pgtyp: params.pagetype,
plcmt: params.placement,
t_n: params.testName || null,
t_v: params.testVersion || null,
mts: mediaTypesKeys.join(','),
ban_szs: bannerSizes.join(','),
bdrs: bidders.map(bidder => bidder.bidder).sort().join(','),
adg_mts: adagioMediaTypes.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) {
// 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.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;
}
}

cache.auctions[prebidAuctionId][adUnitCode] = qp;
sendNewBeacon(prebidAuctionId, adUnitCode);
});
Expand Down Expand Up @@ -328,11 +325,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 @@ -419,14 +422,31 @@ let adagioAdapter = Object.assign(adapter({ emptyUrl, analyticsType }), {
adagioAdapter.originEnableAnalytics = adagioAdapter.enableAnalytics;

adagioAdapter.enableAnalytics = config => {
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;

w.ADAGIO = w.ADAGIO || {};
w.ADAGIO.queue = w.ADAGIO.queue || [];
w.ADAGIO.versions = w.ADAGIO.versions || {};
w.ADAGIO.versions.adagioAnalyticsAdapter = VERSION;

adagioAdapter.options = config.options || {};
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
}
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(config);
}

Expand Down
4 changes: 4 additions & 0 deletions modules/adagioAnalyticsAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ Analytics adapter for Adagio
```js
pbjs.enableAnalytics({
provider: 'adagio',
options: {
organizationId: "16421", // provided during integration
site: "my-website", // provided during integration
}
});
```
Loading

0 comments on commit 9db844f

Please sign in to comment.