-
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
031fa56
Add trustx adapter and tests for it
PWyrembak 319cadd
update integration example
PWyrembak 0e877f9
Merge remote-tracking branch 'upstream/master'
PWyrembak 0425234
Merge remote-tracking branch 'upstream/master'
PWyrembak 69ab1f4
Update trustx adapter
PWyrembak 817f2fa
Merge remote-tracking branch 'upstream/master'
PWyrembak ef50012
Post-review fixes of Trustx adapter
PWyrembak bd5b75c
Merge remote-tracking branch 'upstream/master'
PWyrembak bff944b
Merge remote-tracking branch 'upstream/master'
PWyrembak 843440d
Code improvement for trustx adapter: changed default price type from …
PWyrembak cd01e9b
Merge remote-tracking branch 'upstream/master'
PWyrembak aa249b5
Update TrustX adapter to support the 1.0 version
PWyrembak 119728b
Merge remote-tracking branch 'upstream/master'
PWyrembak 5f60ac3
Make requested changes for TrustX adapter
PWyrembak 2628673
Updated markdown file for TrustX adapter
PWyrembak b3badf9
Merge remote-tracking branch 'upstream/master'
PWyrembak 292b4dd
Fix TrustX adapter and spec file
PWyrembak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
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 - ' | ||
}; | ||
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 - ' | ||
}; | ||
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; | ||
}, | ||
/** | ||
* 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 || []; | ||
let priceType = 'net'; | ||
|
||
bids.forEach(bid => { | ||
if (bid.params.priceType === 'gross') { | ||
priceType = 'gross'; | ||
} | ||
if (!bidsMap[bid.params.uid]) { | ||
bidsMap[bid.params.uid] = [bid]; | ||
auids.push(bid.params.uid); | ||
} else { | ||
bidsMap[bid.params.uid].push(bid); | ||
} | ||
}); | ||
|
||
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: priceType, | ||
auids: auids.join(','), | ||
}; | ||
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); | ||
} | ||
} | ||
} | ||
return { | ||
method: 'GET', | ||
url: ENDPOINT_URL, | ||
data: payload, | ||
bidsMap: bidsMap, | ||
}; | ||
}, | ||
/** | ||
* 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 = bidRequest.bidsMap; | ||
const priceType = bidRequest.data.pt; | ||
|
||
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, priceType, 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, priceType, 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: priceType !== '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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
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 | ||
} | ||
} | ||
] | ||
} | ||
]; | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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.