-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrating TrustX adapter to 1.0 #1709
Changes from 12 commits
031fa56
319cadd
0e877f9
0425234
69ab1f4
817f2fa
ef50012
bd5b75c
bff944b
843440d
cd01e9b
aa249b5
119728b
5f60ac3
2628673
b3badf9
292b4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,165 +1,147 @@ | ||
const utils = require('src/utils.js'); | ||
const bidfactory = require('src/bidfactory.js'); | ||
const bidmanager = require('src/bidmanager.js'); | ||
const adloader = require('src/adloader'); | ||
const adaptermanager = require('src/adaptermanager'); | ||
const CONSTANTS = require('src/constants.json'); | ||
import * as utils from 'src/utils'; | ||
import {registerBidder} from 'src/adapters/bidderFactory'; | ||
const BIDDER_CODE = 'trustx'; | ||
const ENDPOINT_URL = '//sofia.trustx.org/hb'; | ||
const TIME_TO_LIVE = 360; | ||
const ADAPTER_SYNC_URL = '//sofia.trustx.org/push_sync'; | ||
const LOG_ERROR_MESS = { | ||
noAuid: 'Bid from response has no auid parameter - ', | ||
noAdm: 'Bid from response has no adm parameter - ', | ||
noBid: 'Array of bid objects is empty', | ||
noPlacementCode: 'Can\'t find in requested bids the bid with auid - ', | ||
emptyUids: 'Uids should be not empty', | ||
emptySeatbid: 'Seatbid array from response has empty item', | ||
emptyResponse: 'Response is empty', | ||
hasEmptySeatbidArray: 'Response has empty seatbid array', | ||
hasNoArrayOfBids: 'Seatbid from response has no array of bid objects - ' | ||
}; | ||
const bidsRequestMap = {}; | ||
export const spec = { | ||
code: BIDDER_CODE, | ||
/** | ||
* Determines whether or not the given bid request is valid. | ||
* | ||
* @param {BidRequest} bid The bid params to validate. | ||
* @return boolean True if this is a valid bid, and false otherwise. | ||
*/ | ||
isBidRequestValid: function(bid) { | ||
return !!(bid.params.uid && bid.adUnitCode); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
/** | ||
* Make a server request from the list of BidRequests. | ||
* | ||
* @param {BidRequest[]} validBidRequests - an array of bids | ||
* @return ServerRequest Info describing the request to the server. | ||
*/ | ||
buildRequests: function(validBidRequests) { | ||
const auids = []; | ||
const bidsMap = {}; | ||
const bids = validBidRequests || []; | ||
const bidderRequestId = bids[0] && bids[0].bidderRequestId; | ||
|
||
bids.forEach(bid => { | ||
if (!bidsMap[bid.params.uid]) { | ||
bidsMap[bid.params.uid] = [bid]; | ||
auids.push(bid.params.uid); | ||
} else { | ||
bidsMap[bid.params.uid].push(bid); | ||
} | ||
}); | ||
|
||
var TrustxAdapter = function TrustxAdapter() { | ||
const bidderCode = 'trustx'; | ||
const reqHost = '//sofia.trustx.org'; | ||
const reqPath = '/hb?'; | ||
const LOG_ERROR_MESS = { | ||
noAuid: 'Bid from response has no auid parameter - ', | ||
noAdm: 'Bid from response has no adm parameter - ', | ||
noBid: 'Array of bid objects is empty', | ||
noPlacementCode: 'Can\'t find placementCode for bid with auid - ', | ||
havePCodeFor: ', placementCode is available only for the following uids - ', | ||
emptyUids: 'Uids should be not empty', | ||
emptySeatbid: 'Seatbid array from response has empty item', | ||
emptyResponse: 'Response is empty', | ||
hasEmptySeatbidArray: 'Response has empty seatbid array', | ||
hasNoArrayOfBids: 'Seatbid from response has no array of bid objects - ' | ||
}; | ||
bidsRequestMap[bidderRequestId] = bidsMap; | ||
|
||
function _makeHandler(auids, placementMap) { | ||
var cbName = bidderCode + '_callback_wrapper_' + auids.join('_'); | ||
$$PREBID_GLOBAL$$[cbName] = function(resp) { | ||
delete $$PREBID_GLOBAL$$[cbName]; | ||
_responseProcessing(resp, auids, placementMap); | ||
const payload = { | ||
u: utils.getTopWindowUrl(), | ||
pt: window.globalPrebidTrustxPriceType === 'gross' ? 'gross' : 'net', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can pass |
||
auids: auids.join(','), | ||
reqid: bidderRequestId, | ||
}; | ||
return '$$PREBID_GLOBAL$$.' + cbName; | ||
} | ||
|
||
function _sendRequest(auids, placementMap) { | ||
var query = []; | ||
var path = reqPath; | ||
query.push('u=' + encodeURIComponent(location.href)); | ||
query.push('auids=' + encodeURIComponent(auids.join(','))); | ||
query.push('cb=' + _makeHandler(auids, placementMap)); | ||
query.push('pt=' + (window.globalPrebidTrustxPriceType === 'gross' ? 'gross' : 'net')); | ||
|
||
adloader.loadScript(reqHost + path + query.join('&')); | ||
} | ||
|
||
function _callBids(params) { | ||
var auids = []; | ||
var placementMap = {}; | ||
var hasBid; | ||
var bid; | ||
var bids = params.bids || []; | ||
for (var i = 0; i < bids.length; i++) { | ||
bid = bids[i]; | ||
if (bid && bid.bidder === bidderCode && bid.placementCode) { | ||
hasBid = true; | ||
if (bid.params && bid.params.uid) { | ||
if (!placementMap[bid.params.uid]) { | ||
placementMap[bid.params.uid] = [bid.placementCode]; | ||
auids.push(bid.params.uid); | ||
} else { | ||
placementMap[bid.params.uid].push(bid.placementCode); | ||
} | ||
} | ||
} | ||
// const payloadString = JSON.stringify(payload); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove unwanted code. |
||
return { | ||
method: 'GET', | ||
url: ENDPOINT_URL, | ||
data: payload, | ||
}; | ||
}, | ||
/** | ||
* Unpack the response from the server into a list of bids. | ||
* | ||
* @param {*} serverResponse A successful response from the server. | ||
* @param {*} bidRequest | ||
* @return {Bid[]} An array of bids which were nested inside the server. | ||
*/ | ||
interpretResponse: function(serverResponse, bidRequest) { | ||
const bidResponses = []; | ||
const bidsMap = bidsRequestMap[bidRequest.data.reqid]; | ||
|
||
let errorMessage; | ||
|
||
if (!serverResponse) errorMessage = LOG_ERROR_MESS.emptyResponse; | ||
else if (serverResponse.seatbid && !serverResponse.seatbid.length) { | ||
errorMessage = LOG_ERROR_MESS.hasEmptySeatbidArray; | ||
} | ||
|
||
if (auids.length) { | ||
_sendRequest(auids, placementMap); | ||
} else if (hasBid) { | ||
utils.logError(LOG_ERROR_MESS.emptyUids); | ||
if (!errorMessage && serverResponse.seatbid) { | ||
serverResponse.seatbid.forEach(respItem => { | ||
_addBidResponse(_getBidFromResponse(respItem), bidsMap, bidResponses); | ||
}); | ||
} | ||
} | ||
|
||
function _getBidFromResponse(resp) { | ||
if (!resp) { | ||
utils.logError(LOG_ERROR_MESS.emptySeatbid); | ||
} else if (!resp.bid) { | ||
utils.logError(LOG_ERROR_MESS.hasNoArrayOfBids + JSON.stringify(resp)); | ||
} else if (!resp.bid[0]) { | ||
utils.logError(LOG_ERROR_MESS.noBid); | ||
if (errorMessage) utils.logError(errorMessage); | ||
return bidResponses; | ||
}, | ||
getUserSyncs: function(syncOptions) { | ||
if (syncOptions.pixelEnabled) { | ||
return [{ | ||
type: 'image', | ||
url: ADAPTER_SYNC_URL | ||
}]; | ||
} | ||
return resp && resp.bid && resp.bid[0]; | ||
} | ||
|
||
function _forEachPlacement(error, bid, placementCode) { | ||
var bidObject; | ||
if (error) { | ||
bidObject = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bid); | ||
} else { | ||
bidObject = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bid); | ||
bidObject.cpm = bid.price; | ||
bidObject.ad = bid.adm; | ||
bidObject.width = bid.w; | ||
bidObject.height = bid.h; | ||
if (bid.dealid) { | ||
bidObject.dealId = bid.dealid; | ||
} | ||
} | ||
bidObject.bidderCode = bidderCode; | ||
bidmanager.addBidResponse(placementCode, bidObject); | ||
} | ||
|
||
function _getBidFromResponse(respItem) { | ||
if (!respItem) { | ||
utils.logError(LOG_ERROR_MESS.emptySeatbid); | ||
} else if (!respItem.bid) { | ||
utils.logError(LOG_ERROR_MESS.hasNoArrayOfBids + JSON.stringify(respItem)); | ||
} else if (!respItem.bid[0]) { | ||
utils.logError(LOG_ERROR_MESS.noBid); | ||
} | ||
|
||
function _addBidResponse(bid, auids, placementMap) { | ||
if (!bid) return; | ||
var errorMessage, placementCodes; | ||
if (!bid.auid) errorMessage = LOG_ERROR_MESS.noAuid + JSON.stringify(bid); | ||
else { | ||
placementCodes = placementMap.hasOwnProperty(bid.auid) && placementMap[bid.auid]; | ||
if (!placementCodes) { | ||
errorMessage = LOG_ERROR_MESS.noPlacementCode + bid.auid + LOG_ERROR_MESS.havePCodeFor + auids.join(','); | ||
} | ||
} | ||
|
||
if (!errorMessage) { | ||
if (!bid.adm) errorMessage = LOG_ERROR_MESS.noAdm + JSON.stringify(bid); | ||
|
||
var l = placementCodes.length; | ||
while (l--) { | ||
_forEachPlacement(errorMessage, bid, placementCodes[l]); | ||
} | ||
|
||
delete placementMap[bid.auid]; | ||
} | ||
|
||
if (errorMessage) { | ||
utils.logError(errorMessage); | ||
return respItem && respItem.bid && respItem.bid[0]; | ||
} | ||
|
||
function _addBidResponse(serverBid, bidsMap, bidResponses) { | ||
if (!serverBid) return; | ||
let errorMessage; | ||
if (!serverBid.auid) errorMessage = LOG_ERROR_MESS.noAuid + JSON.stringify(serverBid); | ||
if (!serverBid.adm) errorMessage = LOG_ERROR_MESS.noAdm + JSON.stringify(serverBid); | ||
else { | ||
const awaitingBids = bidsMap[serverBid.auid]; | ||
if (awaitingBids) { | ||
awaitingBids.forEach(bid => { | ||
const bidResponse = { | ||
requestId: bid.bidId, // bid.bidderRequestId, | ||
bidderCode: spec.code, | ||
cpm: serverBid.price, | ||
width: serverBid.w, | ||
height: serverBid.h, | ||
creativeId: serverBid.auid, // bid.bidId, | ||
currency: 'USD', | ||
netRevenue: window.globalPrebidTrustxPriceType !== 'gross', | ||
ttl: TIME_TO_LIVE, | ||
ad: serverBid.adm, | ||
dealId: serverBid.dealid | ||
}; | ||
bidResponses.push(bidResponse); | ||
}); | ||
} else { | ||
errorMessage = LOG_ERROR_MESS.noPlacementCode + serverBid.auid; | ||
} | ||
} | ||
|
||
function _responseProcessing(resp, auids, placementMap) { | ||
var errorMessage; | ||
|
||
if (!resp) errorMessage = LOG_ERROR_MESS.emptyResponse; | ||
else if (resp.seatbid && !resp.seatbid.length) errorMessage = LOG_ERROR_MESS.hasEmptySeatbidArray; | ||
|
||
if (!errorMessage) { | ||
resp = resp.seatbid || []; | ||
var l = resp.length; | ||
while (l--) { | ||
_addBidResponse(_getBidFromResponse(resp[l]), auids, placementMap); | ||
} | ||
} | ||
|
||
var n, bidObj; | ||
for (var auid in placementMap) { | ||
if (placementMap.hasOwnProperty(auid) && placementMap[auid]) { | ||
n = placementMap[auid].length; | ||
while (n--) { | ||
bidObj = bidfactory.createBid(CONSTANTS.STATUS.NO_BID); | ||
bidObj.bidderCode = bidderCode; | ||
bidmanager.addBidResponse(placementMap[auid][n], bidObj); | ||
} | ||
} | ||
} | ||
|
||
if (errorMessage) utils.logError(errorMessage); | ||
if (errorMessage) { | ||
utils.logError(errorMessage); | ||
} | ||
} | ||
|
||
return { | ||
callBids: _callBids | ||
}; | ||
}; | ||
|
||
adaptermanager.registerBidAdapter(new TrustxAdapter(), 'trustx'); | ||
|
||
module.exports = TrustxAdapter; | ||
registerBidder(spec); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Overview | ||
|
||
Module Name: TrustX Bidder Adapter | ||
Module Type: Bidder Adapter | ||
Maintainer: paul@trustx.org | ||
|
||
# Description | ||
|
||
Module that connects to TrustX demand source to fetch bids. | ||
|
||
# Test Parameters | ||
``` | ||
window.globalPrebidTrustxPriceType = 'gross'; // by default is 'net' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After update you don't need this in markdown. Update params as required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure you're calling the right prebid location on the page example you've provided? On the page it is "../../build/dev/prebid.js". So I can't see any calls to TX bidder going at all. |
||
var adUnits = [ | ||
{ | ||
code: 'test-div', | ||
sizes: [[300, 250]], | ||
bids: [ | ||
{ | ||
bidder: "trustx", | ||
params: { | ||
uid: '44' | ||
} | ||
} | ||
] | ||
},{ | ||
code: 'test-div', | ||
sizes: [[728, 90]], | ||
bids: [ | ||
{ | ||
bidder: "trustx", | ||
params: { | ||
uid: 45 | ||
} | ||
} | ||
] | ||
} | ||
]; | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No global variables. You can create the same in
spec.buildRequests
scope and add it to request object.spec.interpretResponse
has request object as second argument.For reference, check rubiconBidAdapter