Skip to content

Commit

Permalink
Media impact and adpartner bid adapters: reduce duplication (#12411)
Browse files Browse the repository at this point in the history
* Create index.js

* Update mediaimpactBidAdapter.js

* Update adpartnerBidAdapter.js

* Update adpartnerBidAdapter_spec.js

* Update mediaimpactBidAdapter_spec.js

* Update adpartnerBidAdapter_spec.js

* Update adpartnerBidAdapter.js

* Update adpartnerBidAdapter.js

* Update mediaimpactBidAdapter.js

* Update index.js

* Update mediaimpactBidAdapter.js

* Update adpartnerBidAdapter.js

* Update adpartnerBidAdapter.js

* Update index.js

* Update mediaimpactBidAdapter.js

* Update mediaimpactBidAdapter_spec.js

* Update mediaimpactBidAdapter.js

* Update mediaimpactBidAdapter_spec.js
  • Loading branch information
patmmccann authored Nov 18, 2024
1 parent a42abdd commit b67898b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 185 deletions.
61 changes: 61 additions & 0 deletions libraries/mediaImpactUtils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { buildUrl } from '../../src/utils.js';
import { ajax } from '../../src/ajax.js';

/**
* Builds the bid requests and beacon parameters.
* @param {Array} validBidRequests - The array of valid bid requests.
* @param {string} referer - The referer URL.
* @returns {Object} - An object containing bidRequests and beaconParams.
*/
export function buildBidRequestsAndParams(validBidRequests, referer) {
const bidRequests = [];
const beaconParams = { tag: [], partner: [], sizes: [], referer: encodeURIComponent(referer) };

validBidRequests.forEach(function (validBidRequest) {
const sizes = validBidRequest.params.sizes || validBidRequest.sizes;

const bidRequestObject = {
adUnitCode: validBidRequest.adUnitCode,
sizes: sizes,
bidId: validBidRequest.bidId,
referer: referer,
};

if (parseInt(validBidRequest.params.unitId)) {
bidRequestObject.unitId = parseInt(validBidRequest.params.unitId);
beaconParams.tag.push(validBidRequest.params.unitId);
}

if (parseInt(validBidRequest.params.partnerId)) {
bidRequestObject.unitId = 0;
bidRequestObject.partnerId = parseInt(validBidRequest.params.partnerId);
beaconParams.partner.push(validBidRequest.params.partnerId);
}

bidRequests.push(bidRequestObject);
beaconParams.sizes.push(joinSizesToString(sizes));
});

// Finalize beaconParams
if (beaconParams.partner.length > 0) {
beaconParams.partner = beaconParams.partner.join(',');
} else {
delete beaconParams.partner;
}
beaconParams.tag = beaconParams.tag.join(',');
beaconParams.sizes = beaconParams.sizes.join(',');

return { bidRequests, beaconParams };
}

export function joinSizesToString(sizes) {
return sizes.map(size => size.join('x')).join('|');
}

export function postRequest(endpoint, data) {
ajax(endpoint, null, data, { method: 'POST' });
}

export function buildEndpointUrl(protocol, hostname, pathname, searchParams) {
return buildUrl({ protocol, hostname, pathname, search: searchParams });
}
114 changes: 26 additions & 88 deletions modules/adpartnerBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { buildUrl } from '../src/utils.js'
import {ajax} from '../src/ajax.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { buildBidRequestsAndParams, postRequest, buildEndpointUrl } from '../libraries/mediaImpactUtils/index.js';

const BIDDER_CODE = 'adpartner';
export const ENDPOINT_PROTOCOL = 'https';
Expand All @@ -15,74 +14,25 @@ export const spec = {
},

buildRequests: function (validBidRequests, bidderRequest) {
// TODO does it make sense to fall back to window.location.href?
const referer = bidderRequest?.refererInfo?.page || window.location.href;

let bidRequests = [];
let beaconParams = {
tag: [],
partner: [],
sizes: [],
referer: ''
};

validBidRequests.forEach(function(validBidRequest) {
let bidRequestObject = {
adUnitCode: validBidRequest.adUnitCode,
sizes: validBidRequest.sizes,
bidId: validBidRequest.bidId,
referer: referer
};

if (parseInt(validBidRequest.params.unitId)) {
bidRequestObject.unitId = parseInt(validBidRequest.params.unitId);
beaconParams.tag.push(validBidRequest.params.unitId);
}

if (parseInt(validBidRequest.params.partnerId)) {
bidRequestObject.unitId = 0;
bidRequestObject.partnerId = parseInt(validBidRequest.params.partnerId);
beaconParams.partner.push(validBidRequest.params.partnerId);
}

bidRequests.push(bidRequestObject);
// Use the common function to build bidRequests and beaconParams
const { bidRequests, beaconParams } = buildBidRequestsAndParams(validBidRequests, referer);

beaconParams.sizes.push(spec.joinSizesToString(validBidRequest.sizes));
beaconParams.referer = encodeURIComponent(referer);
});

if (beaconParams.partner.length > 0) {
beaconParams.partner = beaconParams.partner.join(',');
} else {
delete beaconParams.partner;
}

beaconParams.tag = beaconParams.tag.join(',');
beaconParams.sizes = beaconParams.sizes.join(',');

let adPartnerRequestUrl = buildUrl({
protocol: ENDPOINT_PROTOCOL,
hostname: ENDPOINT_DOMAIN,
pathname: ENDPOINT_PATH,
search: beaconParams
});
const adPartnerRequestUrl = buildEndpointUrl(
ENDPOINT_PROTOCOL,
ENDPOINT_DOMAIN,
ENDPOINT_PATH,
beaconParams
);

return {
method: 'POST',
url: adPartnerRequestUrl,
data: JSON.stringify(bidRequests)
data: JSON.stringify(bidRequests),
};
},

joinSizesToString: function(sizes) {
let res = [];
sizes.forEach(function(size) {
res.push(size.join('x'));
});

return res.join('|');
},

interpretResponse: function (serverResponse, bidRequest) {
const validBids = JSON.parse(bidRequest.data);

Expand All @@ -91,15 +41,12 @@ export const spec = {
}

return validBids
.map(bid => ({
bid: bid,
ad: serverResponse.body[bid.adUnitCode]
}))
.map(bid => ({ bid: bid, ad: serverResponse.body[bid.adUnitCode] }))
.filter(item => item.ad)
.map(item => spec.adResponse(item.bid, item.ad));
},

adResponse: function(bid, ad) {
adResponse: function (bid, ad) {
const bidObject = {
requestId: bid.bidId,
ad: ad.ad,
Expand All @@ -110,38 +57,30 @@ export const spec = {
creativeId: ad.creativeId,
netRevenue: ad.netRevenue,
currency: ad.currency,
winNotification: ad.winNotification
}

bidObject.meta = {};
if (ad.adomain && ad.adomain.length > 0) {
bidObject.meta.advertiserDomains = ad.adomain;
}
winNotification: ad.winNotification,
meta: ad.adomain && ad.adomain.length > 0 ? { advertiserDomains: ad.adomain } : {},
};

return bidObject;
},

onBidWon: function(data) {
data.winNotification.forEach(function(unitWon) {
let adPartnerBidWonUrl = buildUrl({
protocol: ENDPOINT_PROTOCOL,
hostname: ENDPOINT_DOMAIN,
pathname: unitWon.path
});
onBidWon: function (data) {
data.winNotification.forEach(function (unitWon) {
const adPartnerBidWonUrl = buildEndpointUrl(
ENDPOINT_PROTOCOL,
ENDPOINT_DOMAIN,
unitWon.path
);

if (unitWon.method === 'POST') {
spec.postRequest(adPartnerBidWonUrl, JSON.stringify(unitWon.data));
postRequest(adPartnerBidWonUrl, JSON.stringify(unitWon.data));
}
});

return true;
},

postRequest(endpoint, data) {
ajax(endpoint, null, data, {method: 'POST'});
},

getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
getUserSyncs: function (syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncs = [];

if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) {
Expand Down Expand Up @@ -205,7 +144,6 @@ export const spec = {

return syncs;
},

}
};

registerBidder(spec);
Loading

0 comments on commit b67898b

Please sign in to comment.