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 9, 2024
1 parent c3fa5a0 commit 6b4abcd
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 94 deletions.
102 changes: 60 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';
import { config } from '../src/config.js';

Expand Down Expand Up @@ -192,32 +192,19 @@ 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;
}

cache.auctions[prebidAuctionId] = {};

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 @@ -231,42 +218,51 @@ 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(',');

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: prebidAuctionId,
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 @@ -315,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 @@ -410,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 @@ -418,7 +425,18 @@ adagioAdapter.enableAnalytics = adapterConfig => {
w.ADAGIO.queue = w.ADAGIO.queue || [];
w.ADAGIO.versions = w.ADAGIO.versions || {};
w.ADAGIO.versions.adagioAnalyticsAdapter = VERSION;

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

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 6b4abcd

Please sign in to comment.