Skip to content

Commit

Permalink
Adagio Analytics: change bidder code for aliases and add bidder cpm (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
soupape34 authored Apr 10, 2024
1 parent beeb901 commit de42f8d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 73 deletions.
79 changes: 41 additions & 38 deletions modules/adagioAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const ADAGIO_GVLID = 617;
const VERSION = '3.0.0';
const PREBID_VERSION = '$prebid.version$';
const ENDPOINT = 'https://c.4dex.io/pba.gif';
const CURRENCY_USD = 'USD';
const ADAGIO_CODE = 'adagio';
const cache = {
auctions: {},
getAuction: function(auctionId, adUnitCode) {
Expand Down Expand Up @@ -92,17 +94,11 @@ function removeDuplicates(arr, getKey) {
});
};

function getAdapterNameForAlias(aliasName) {
return adapterManager.aliasRegistry[aliasName] || aliasName;
};

function isAdagio(value) {
if (!value) {
function isAdagio(alias) {
if (!alias) {
return false
}

return value.toLowerCase().includes('adagio') ||
getAdapterNameForAlias(value).toLowerCase().includes('adagio');
return (alias + adapterManager.aliasRegistry[alias]).toLowerCase().includes(ADAGIO_CODE);
};

function getMediaTypeAlias(mediaType) {
Expand All @@ -129,6 +125,26 @@ function addKeyPrefix(obj, prefix) {
}, {});
}

function getUsdCpm(cpm, currency) {
let netCpm = cpm

if (typeof currency === 'string' && currency.toUpperCase() !== CURRENCY_USD) {
if (typeof getGlobal().convertCurrency === 'function') {
netCpm = parseFloat(Number(getGlobal().convertCurrency(cpm, currency, CURRENCY_USD))).toFixed(3);
} else {
netCpm = null
}
}
return netCpm
}

function getCurrencyData(bid) {
return {
netCpm: getUsdCpm(bid.cpm, bid.currency),
orginalCpm: getUsdCpm(bid.originalCpm, bid.originalCurrency)
}
}

/**
* sendRequest to Adagio. It filter null values and encode each query param.
* @param {Object} qp
Expand Down Expand Up @@ -258,7 +274,7 @@ function handlerAuctionInit(event) {
t_v: params.testVersion || null,
mts: mediaTypesKeys.join(','),
ban_szs: bannerSizes.join(','),
bdrs: bidders.map(bidder => getAdapterNameForAlias(bidder.bidder)).sort().join(','),
bdrs: bidders.map(bidder => bidder.bidder).sort().join(','),
adg_mts: adagioMediaTypes.join(',')
};

Expand Down Expand Up @@ -299,56 +315,43 @@ function handlerAuctionEnd(event) {

const adUnitCodes = cache.getAllAdUnitCodes(auctionId);
adUnitCodes.forEach(adUnitCode => {
const mapper = (bidder) => event.bidsReceived.find(bid => bid.adUnitCode === adUnitCode && bid.bidder === bidder) ? '1' : '0';
const bidResponseMapper = (bidder) => {
const bid = event.bidsReceived.find(bid => bid.adUnitCode === adUnitCode && bid.bidder === bidder)
return bid ? '1' : '0'
}
const bidCpmMapper = (bidder) => {
const bid = event.bidsReceived.find(bid => bid.adUnitCode === adUnitCode && bid.bidder === bidder)
return bid ? getCurrencyData(bid).netCpm : null
}

cache.updateAuction(auctionId, adUnitCode, {
bdrs_bid: cache.getBiddersFromAuction(auctionId, adUnitCode).map(mapper).join(',')
bdrs_bid: cache.getBiddersFromAuction(auctionId, adUnitCode).map(bidResponseMapper).join(','),
bdrs_cpm: cache.getBiddersFromAuction(auctionId, adUnitCode).map(bidCpmMapper).join(',')
});
sendNewBeacon(auctionId, adUnitCode);
});
}

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

if (!guard.bidTracked(auctionId, event.adUnitCode)) {
return;
}

let adsCurRateToUSD = (event.currency === 'USD') ? 1 : null;
let ogCurRateToUSD = (event.originalCurrency === 'USD') ? 1 : null;
try {
if (typeof getGlobal().convertCurrency === 'function') {
// Currency module is loaded, we can calculate the conversion rate.

// Get the conversion rate from the original currency to USD.
ogCurRateToUSD = getGlobal().convertCurrency(1, event.originalCurrency, 'USD');
// Get the conversion rate from the ad server currency to USD.
adsCurRateToUSD = getGlobal().convertCurrency(1, event.currency, 'USD');
}
} catch (error) {
logError('Error on Adagio Analytics Adapter - handlerBidWon', error);
}
const currencyData = getCurrencyData(event)

const adagioAuctionCacheId = (
(event.latestTargetedAuctionId && event.latestTargetedAuctionId !== event.auctionId)
? cache.getAdagioAuctionId(event.auctionId)
: null);

cache.updateAuction(auctionId, event.adUnitCode, {
win_bdr: getAdapterNameForAlias(event.bidder),
win_bdr: event.bidder,
win_mt: getMediaTypeAlias(event.mediaType),
win_ban_sz: event.mediaType === BANNER ? `${event.width}x${event.height}` : null,

// ad server currency
win_cpm: event.cpm,
cur: event.currency,
cur_rate: adsCurRateToUSD,

// original currency from bidder
og_cpm: event.originalCpm,
og_cur: event.originalCurrency,
og_cur_rate: ogCurRateToUSD,
win_net_cpm: currencyData.netCpm,
win_og_cpm: currencyData.orginalCpm,

// cache bid id
auct_id_c: adagioAuctionCacheId,
Expand Down Expand Up @@ -428,7 +431,7 @@ adagioAdapter.enableAnalytics = config => {

adapterManager.registerAnalyticsAdapter({
adapter: adagioAdapter,
code: 'adagio',
code: ADAGIO_CODE,
gvlid: ADAGIO_GVLID,
});

Expand Down
94 changes: 59 additions & 35 deletions test/spec/modules/adagioAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import adagioAnalyticsAdapter from 'modules/adagioAnalyticsAdapter.js';
import { expect } from 'chai';
import * as utils from 'src/utils.js';
import { getGlobal } from 'src/prebidGlobal.js';
import { server } from 'test/mocks/xhr.js';
import * as prebidGlobal from 'src/prebidGlobal.js';

let adapterManager = require('src/adapterManager').default;
let events = require('src/events');
Expand Down Expand Up @@ -647,17 +647,19 @@ describe('adagio analytics adapter', () => {
});

it('builds and sends auction data', () => {
getGlobal().convertCurrency = (cpm, from, to) => {
const convKeys = {
'GBP-EUR': 0.7,
'EUR-GBP': 1.3,
'USD-EUR': 0.8,
'EUR-USD': 1.2,
'USD-GBP': 0.6,
'GBP-USD': 1.6,
};
return cpm * (convKeys[`${from}-${to}`] || 1);
};
sandbox.stub(prebidGlobal, 'getGlobal').returns({
convertCurrency: (cpm, from, to) => {
const convKeys = {
'GBP-EUR': 0.7,
'EUR-GBP': 1.3,
'USD-EUR': 0.8,
'EUR-USD': 1.2,
'USD-GBP': 0.6,
'GBP-USD': 1.6,
};
return cpm * (convKeys[`${from}-${to}`] || 1);
}
});

events.emit(constants.EVENTS.AUCTION_INIT, MOCK.AUCTION_INIT.another);
events.emit(constants.EVENTS.BID_RESPONSE, MOCK.BID_RESPONSE.adagio);
Expand Down Expand Up @@ -697,6 +699,7 @@ describe('adagio analytics adapter', () => {
expect(search.e_sid).to.equal('42');
expect(search.e_pba_test).to.equal('true');
expect(search.bdrs_bid).to.equal('1,1,0');
expect(search.bdrs_cpm).to.equal('1.42,2.052,');
}

{
Expand All @@ -710,27 +713,25 @@ describe('adagio analytics adapter', () => {
expect(search.win_bdr).to.equal('another');
expect(search.win_mt).to.equal('ban');
expect(search.win_ban_sz).to.equal('728x90');
expect(search.win_cpm).to.equal('1.71');
expect(search.cur).to.equal('EUR');
expect(search.cur_rate).to.equal('1.2');
expect(search.og_cpm).to.equal('1.62');
expect(search.og_cur).to.equal('GBP');
expect(search.og_cur_rate).to.equal('1.6');
expect(search.win_net_cpm).to.equal('2.052');
expect(search.win_og_cpm).to.equal('2.592');
}
});

it('builds and sends auction data with a cached bid win', () => {
getGlobal().convertCurrency = (cpm, from, to) => {
const convKeys = {
'GBP-EUR': 0.7,
'EUR-GBP': 1.3,
'USD-EUR': 0.8,
'EUR-USD': 1.2,
'USD-GBP': 0.6,
'GBP-USD': 1.6,
};
return cpm * (convKeys[`${from}-${to}`] || 1);
};
sandbox.stub(prebidGlobal, 'getGlobal').returns({
convertCurrency: (cpm, from, to) => {
const convKeys = {
'GBP-EUR': 0.7,
'EUR-GBP': 1.3,
'USD-EUR': 0.8,
'EUR-USD': 1.2,
'USD-GBP': 0.6,
'GBP-USD': 1.6,
};
return cpm * (convKeys[`${from}-${to}`] || 1);
}
});

events.emit(constants.EVENTS.AUCTION_INIT, MOCK.AUCTION_INIT.bidcached);
events.emit(constants.EVENTS.AUCTION_INIT, MOCK.AUCTION_INIT.another);
Expand Down Expand Up @@ -794,6 +795,7 @@ describe('adagio analytics adapter', () => {
expect(search.e_sid).to.equal('42');
expect(search.e_pba_test).to.equal('true');
expect(search.bdrs_bid).to.equal('0,0,0');
expect(search.bdrs_cpm).to.equal(',,');
}

{
Expand All @@ -808,12 +810,8 @@ describe('adagio analytics adapter', () => {
expect(search.win_bdr).to.equal('adagio');
expect(search.win_mt).to.equal('ban');
expect(search.win_ban_sz).to.equal('728x90');
expect(search.win_cpm).to.equal('1.42');
expect(search.cur).to.equal('USD');
expect(search.cur_rate).to.equal('1');
expect(search.og_cpm).to.equal('1.42');
expect(search.og_cur).to.equal('USD');
expect(search.og_cur_rate).to.equal('1');
expect(search.win_net_cpm).to.equal('1.42');
expect(search.win_og_cpm).to.equal('1.42');
expect(search.rndr).to.not.exist;
}

Expand All @@ -829,5 +827,31 @@ describe('adagio analytics adapter', () => {
expect(search.rndr).to.equal('0');
}
});

it('send an "empty" cpm when adserver currency != USD and convertCurrency() is undefined', () => {
sandbox.stub(prebidGlobal, 'getGlobal').returns({});

events.emit(constants.EVENTS.AUCTION_INIT, MOCK.AUCTION_INIT.another);
events.emit(constants.EVENTS.BID_RESPONSE, MOCK.BID_RESPONSE.adagio);
events.emit(constants.EVENTS.BID_RESPONSE, MOCK.BID_RESPONSE.another);
events.emit(constants.EVENTS.AUCTION_END, MOCK.AUCTION_END.another);
events.emit(constants.EVENTS.BID_WON, MOCK.BID_WON.another);
events.emit(constants.EVENTS.AD_RENDER_SUCCEEDED, MOCK.AD_RENDER_SUCCEEDED.another);

expect(server.requests.length).to.equal(3, 'requests count');

// fail to compute bidder cpm and send an "empty" cpm
{
const { protocol, hostname, pathname, search } = utils.parseUrl(server.requests[1].url);
expect(protocol).to.equal('https');
expect(hostname).to.equal('c.4dex.io');
expect(pathname).to.equal('/pba.gif');
expect(search.v).to.equal('2');
expect(search.e_sid).to.equal('42');
expect(search.e_pba_test).to.equal('true');
expect(search.bdrs_bid).to.equal('1,1,0');
expect(search.bdrs_cpm).to.equal('1.42,,');
}
});
});
});

0 comments on commit de42f8d

Please sign in to comment.