Skip to content

Commit

Permalink
Taboola Bid Adapter : refactor using ortb conversion library (prebid#…
Browse files Browse the repository at this point in the history
…10910)

* refactor using ORTB conversion library

* refactor using ORTB conversion library

* refactor using ORTB conversion library

* refactor using ORTB conversion library

---------

Co-authored-by: ahmadlob <109217988+ahmadlob@users.noreply.github.com>
  • Loading branch information
barRubi and ahmadlob authored Jan 16, 2024
1 parent aed0f06 commit dc80cff
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 226 deletions.
240 changes: 105 additions & 135 deletions modules/taboolaBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import {config} from '../src/config.js';
import {deepAccess, getWindowSelf, replaceAuctionPrice} from '../src/utils.js';
import {deepAccess, deepSetValue, getWindowSelf, replaceAuctionPrice} from '../src/utils.js';
import {getStorageManager} from '../src/storageManager.js';
import {ajax} from '../src/ajax.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js';

const BIDDER_CODE = 'taboola';
const GVLID = 42;
Expand Down Expand Up @@ -81,6 +82,30 @@ export const internal = {
}
}

const converter = ortbConverter({
context: {
netRevenue: true,
mediaType: BANNER,
ttl: 300
},
imp(buildImp, bidRequest, context) {
let imp = buildImp(bidRequest, context);
fillTaboolaImpData(bidRequest, imp);
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const reqData = buildRequest(imps, bidderRequest, context);
fillTaboolaReqData(bidderRequest, context.bidRequests[0], reqData)
return reqData;
},
bidResponse(buildBidResponse, bid, context) {
const bidResponse = buildBidResponse(bid, context);
bidResponse.nurl = bid.nurl;
bidResponse.ad = replaceAuctionPrice(bid.adm, bid.price);
return bidResponse
}
});

export const spec = {
supportedMediaTypes: [BANNER],
gvlid: GVLID,
Expand All @@ -93,85 +118,35 @@ export const spec = {
},
buildRequests: (validBidRequests, bidderRequest) => {
const [bidRequest] = validBidRequests;
const {refererInfo, gdprConsent = {}, uspConsent} = bidderRequest;
const data = converter.toORTB({bidderRequest: bidderRequest, bidRequests: validBidRequests});
const {publisherId} = bidRequest.params;
const site = getSiteProperties(bidRequest.params, refererInfo, bidderRequest.ortb2);
const device = {ua: navigator.userAgent};
const imps = getImps(validBidRequests);
const user = {
buyeruid: userData.getUserId(gdprConsent, uspConsent),
ext: {}
};
const regs = {
coppa: 0,
ext: {}
};

if (gdprConsent.gdprApplies) {
user.ext.consent = bidderRequest.gdprConsent.consentString;
regs.ext.gdpr = 1;
}

if (uspConsent) {
regs.ext.us_privacy = uspConsent;
}

if (bidderRequest.ortb2?.regs?.gpp) {
regs.ext.gpp = bidderRequest.ortb2.regs.gpp;
regs.ext.gpp_sid = bidderRequest.ortb2.regs.gpp_sid;
}

if (config.getConfig('coppa')) {
regs.coppa = 1;
}

const ortb2 = bidderRequest.ortb2 || {
bcat: [],
badv: [],
wlang: []
};

const request = {
id: bidderRequest.bidderRequestId,
imp: imps,
site,
device,
source: {fd: 1},
tmax: (bidderRequest.timeout == undefined) ? undefined : parseInt(bidderRequest.timeout),
bcat: ortb2.bcat || bidRequest.params.bcat || [],
badv: ortb2.badv || bidRequest.params.badv || [],
wlang: ortb2.wlang || bidRequest.params.wlang || [],
user,
regs,
ext: {
pageType: ortb2?.ext?.data?.pageType || ortb2?.ext?.data?.section || bidRequest.params.pageType
}
};

const url = END_POINT_URL + '?publisher=' + publisherId;

return {
url,
method: 'POST',
data: JSON.stringify(request),
data: data,
bids: validBidRequests,
options: {
withCredentials: false
},
};
},
interpretResponse: (serverResponse, {bids}) => {
if (!bids) {
interpretResponse: (serverResponse, request) => {
if (!request || !request.bids || !request.data) {
return [];
}

const {bidResponses, cur: currency} = getBidResponses(serverResponse);
if (!serverResponse || !serverResponse.body) {
return [];
}

if (!bidResponses) {
if (!serverResponse.body.seatbid || !serverResponse.body.seatbid.length || !serverResponse.body.seatbid[0].bid || !serverResponse.body.seatbid[0].bid.length) {
return [];
}

return bidResponses.map((bidResponse) => getBid(bids, currency, bidResponse)).filter(Boolean);
const bids = converter.fromORTB({response: serverResponse.body, request: request.data}).bids;
return bids;
},
onBidWon: (bid) => {
if (bid.nurl) {
Expand Down Expand Up @@ -235,34 +210,76 @@ function getSiteProperties({publisherId}, refererInfo, ortb2) {
}
}

function getImps(validBidRequests) {
return validBidRequests.map((bid, id) => {
const {tagId, position} = bid.params;
const imp = {
id: id + 1,
banner: getBanners(bid, position),
tagid: tagId
}
if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: CURRENCY,
mediaType: BANNER,
size: '*'
});
if (typeof floorInfo === 'object' && floorInfo.currency === CURRENCY && !isNaN(parseFloat(floorInfo.floor))) {
imp.bidfloor = parseFloat(floorInfo.floor);
imp.bidfloorcur = CURRENCY;
}
} else {
const {bidfloor = null, bidfloorcur = CURRENCY} = bid.params;
imp.bidfloor = bidfloor;
imp.bidfloorcur = bidfloorcur;
}
imp['ext'] = {
gpid: deepAccess(bid, 'ortb2Imp.ext.gpid')
function fillTaboolaReqData(bidderRequest, bidRequest, data) {
const {refererInfo, gdprConsent = {}, uspConsent} = bidderRequest;
const site = getSiteProperties(bidRequest.params, refererInfo, bidderRequest.ortb2);
const device = {ua: navigator.userAgent};
const user = {
buyeruid: userData.getUserId(gdprConsent, uspConsent),
ext: {}
};
const regs = {
coppa: 0,
ext: {}
};

if (gdprConsent.gdprApplies) {
user.ext.consent = bidderRequest.gdprConsent.consentString;
regs.ext.gdpr = 1;
}

if (uspConsent) {
regs.ext.us_privacy = uspConsent;
}

if (bidderRequest.ortb2?.regs?.gpp) {
regs.ext.gpp = bidderRequest.ortb2.regs.gpp;
regs.ext.gpp_sid = bidderRequest.ortb2.regs.gpp_sid;
}

if (config.getConfig('coppa')) {
regs.coppa = 1;
}

const ortb2 = bidderRequest.ortb2 || {
bcat: [],
badv: [],
wlang: []
};

data.id = bidderRequest.bidderRequestId;
data.site = site;
data.device = device;
data.source = {fd: 1};
data.tmax = (bidderRequest.timeout == undefined) ? undefined : parseInt(bidderRequest.timeout);
data.bcat = ortb2.bcat || bidRequest.params.bcat || [];
data.badv = ortb2.badv || bidRequest.params.badv || [];
data.wlang = ortb2.wlang || bidRequest.params.wlang || [];
data.user = user;
data.regs = regs;
deepSetValue(data, 'ext.pageType', ortb2?.ext?.data?.pageType || ortb2?.ext?.data?.section || bidRequest.params.pageType);
}

function fillTaboolaImpData(bid, imp) {
const {tagId, position} = bid.params;
imp.banner = getBanners(bid, position);
imp.tagid = tagId;

if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: CURRENCY,
size: '*'
});
if (typeof floorInfo === 'object' && floorInfo.currency === CURRENCY && !isNaN(parseFloat(floorInfo.floor))) {
imp.bidfloor = parseFloat(floorInfo.floor);
imp.bidfloorcur = CURRENCY;
}
return imp;
});
} else {
const {bidfloor = null, bidfloorcur = CURRENCY} = bid.params;
imp.bidfloor = bidfloor;
imp.bidfloorcur = bidfloorcur;
}
deepSetValue(imp, 'ext.gpid', deepAccess(bid, 'ortb2Imp.ext.gpid'));
}

function getBanners(bid, pos) {
Expand All @@ -283,51 +300,4 @@ function getSizes(sizes) {
}
}

function getBidResponses({body}) {
if (!body) {
return [];
}

const {seatbid, cur} = body;

if (!seatbid.length || !seatbid[0].bid || !seatbid[0].bid.length) {
return [];
}

return {
bidResponses: seatbid[0].bid,
cur
};
}

function getBid(bids, currency, bidResponse) {
if (!bidResponse) {
return;
}
let {
price: cpm, nurl, crid: creativeId, adm: ad, w: width, h: height, exp: ttl, adomain: advertiserDomains, meta = {}
} = bidResponse;
let requestId = bids[bidResponse.impid - 1].bidId;
if (advertiserDomains && advertiserDomains.length > 0) {
meta.advertiserDomains = advertiserDomains
}

ad = replaceAuctionPrice(ad, cpm);

return {
requestId,
ttl,
mediaType: BANNER,
cpm,
creativeId,
currency,
ad,
width,
height,
meta,
nurl,
netRevenue: true
};
}

registerBidder(spec);
Loading

0 comments on commit dc80cff

Please sign in to comment.