-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Platform.io Bidder Adapter update. Prebid v1.0. (#1705)
* Add PlatformioBidAdapter * Update platformioBidAdapter.js * Add files via upload * Update hello_world.html * Update platformioBidAdapter.js * Update platformioBidAdapter_spec.js * Update hello_world.html * Update platformioBidAdapter_spec.js * Update platformioBidAdapter.js * Update hello_world.html * Add files via upload * Update platformioBidAdapter ## Type of change - [x] Other ## Description of change 1. RequestURL changes 2. Add placementCode to request params * Update platformioBidAdapter * Update platformioBidAdapter ## Type of change - [x] Other ## Description of change 1. RequestURL changes 2. Add placementCode to request params * Add files via upload * Add files via upload * Add files via upload * Update platformioBidAdapter.js Endpoint URL change * Update platformioBidAdapter_spec.js Endpoint URL change * Update platformioBidAdapter_spec.js * Update platformioBidAdapter_spec.js * Update platformioBidAdapter.js * Update platformioBidAdapter.js * Update platformioBidAdapter_spec.js * Add files via upload * Add files via upload * Add files via upload
- Loading branch information
1 parent
f8bf197
commit 409fbc5
Showing
3 changed files
with
246 additions
and
219 deletions.
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,63 +1,125 @@ | ||
var bidfactory = require('src/bidfactory.js'); | ||
var bidmanager = require('src/bidmanager.js'); | ||
var adloader = require('src/adloader.js'); | ||
var utils = require('src/utils.js'); | ||
var CONSTANTS = require('src/constants.json'); | ||
var adaptermanager = require('src/adaptermanager'); | ||
|
||
var PlatformIOAdapter = function PlatformIOAdapter() { | ||
function _callBids(params) { | ||
var bidURL; | ||
var bids = params.bids || []; | ||
var requestURL = window.location.protocol + '//js.adx1.com/pb_ortb.js?cb=' + new Date().getTime() + '&ver=1&'; | ||
|
||
for (var i = 0; i < bids.length; i++) { | ||
var requestParams = {}; | ||
var bid = bids[i]; | ||
|
||
requestParams.pub_id = bid.params.pubId; | ||
requestParams.site_id = bid.params.siteId; | ||
requestParams.placement_id = bid.placementCode; | ||
|
||
var parseSized = utils.parseSizesInput(bid.sizes); | ||
var arrSize = parseSized[0].split('x'); | ||
|
||
requestParams.width = arrSize[0]; | ||
requestParams.height = arrSize[1]; | ||
requestParams.callback = '$$PREBID_GLOBAL$$._doPlatformIOCallback'; | ||
requestParams.callback_uid = bid.bidId; | ||
bidURL = requestURL + utils.parseQueryStringParameters(requestParams); | ||
|
||
utils.logMessage('PlatformIO.prebid, Bid ID: ' + bid.bidId + ', Pub ID: ' + bid.params.pubId); | ||
adloader.loadScript(bidURL); | ||
} | ||
} | ||
|
||
$$PREBID_GLOBAL$$._doPlatformIOCallback = function (response) { | ||
var bidObject; | ||
var bidRequest; | ||
var callbackID; | ||
callbackID = response.callback_uid; | ||
bidRequest = utils.getBidRequest(callbackID); | ||
if (response.cpm > 0) { | ||
bidObject = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bidRequest); | ||
bidObject.bidderCode = 'platformio'; | ||
bidObject.cpm = response.cpm; | ||
bidObject.ad = response.tag; | ||
bidObject.width = response.width; | ||
bidObject.height = response.height; | ||
} else { | ||
bidObject = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidRequest); | ||
bidObject.bidderCode = 'platformio'; | ||
utils.logMessage('No Bid response from Platformio request: ' + callbackID); | ||
} | ||
bidmanager.addBidResponse(bidRequest.placementCode, bidObject); | ||
}; | ||
|
||
return { | ||
callBids: _callBids | ||
}; | ||
}; | ||
adaptermanager.registerBidAdapter(new PlatformIOAdapter(), 'platformio'); | ||
|
||
module.exports = PlatformIOAdapter; | ||
|
||
import {logError, getTopWindowLocation} from 'src/utils'; | ||
import { registerBidder } from 'src/adapters/bidderFactory'; | ||
|
||
export const spec = { | ||
|
||
code: 'platformio', | ||
|
||
isBidRequestValid: bid => ( | ||
!!(bid && bid.params && bid.params.pubId && bid.params.siteId) | ||
), | ||
buildRequests: bidRequests => { | ||
const request = { | ||
id: bidRequests[0].bidderRequestId, | ||
at: 2, | ||
imp: bidRequests.map(slot => impression(slot)), | ||
site: site(bidRequests), | ||
device: device(), | ||
}; | ||
return { | ||
method: 'POST', | ||
url: '//piohbdisp.hb.adx1.com/', | ||
data: JSON.stringify(request), | ||
}; | ||
}, | ||
interpretResponse: (response, request) => ( | ||
bidResponseAvailable(request, response) | ||
), | ||
}; | ||
function bidResponseAvailable(bidRequest, bidResponse) { | ||
const idToImpMap = {}; | ||
const idToBidMap = {}; | ||
const ortbRequest = parse(bidRequest.data); | ||
ortbRequest.imp.forEach(imp => { | ||
idToImpMap[imp.id] = imp; | ||
}); | ||
if (bidResponse) { | ||
bidResponse.seatbid.forEach(seatBid => seatBid.bid.forEach(bid => { | ||
idToBidMap[bid.impid] = bid; | ||
})); | ||
} | ||
const bids = []; | ||
Object.keys(idToImpMap).forEach(id => { | ||
if (idToBidMap[id]) { | ||
const bid = { | ||
requestId: id, | ||
cpm: idToBidMap[id].price, | ||
creative_id: id, | ||
creativeId: id, | ||
adId: id, | ||
}; | ||
bid.ad = idToBidMap[id].adm; | ||
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_IMP_ID(%7D|\})/gi, idToBidMap[id].impid); | ||
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_AD_ID(%7D|\})/gi, idToBidMap[id].adid); | ||
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_PRICE(%7D|\})/gi, idToBidMap[id].price); | ||
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_CURRENCY(%7D|\})/gi, bidResponse.cur); | ||
bid.ad = bid.ad.replace(/\$(%7B|\{)AUCTION_BID_ID(%7D|\})/gi, bidResponse.bidid); | ||
bid.width = idToImpMap[id].banner.w; | ||
bid.height = idToImpMap[id].banner.h; | ||
bids.push(bid); | ||
} | ||
}); | ||
return bids; | ||
} | ||
function impression(slot) { | ||
return { | ||
id: slot.bidId, | ||
banner: banner(slot), | ||
bidfloor: '0.000001', | ||
tagid: slot.params.placementId.toString(), | ||
}; | ||
} | ||
function banner(slot) { | ||
const size = slot.params.size.toUpperCase().split('X'); | ||
const width = parseInt(size[0]); | ||
const height = parseInt(size[1]); | ||
return { | ||
w: width, | ||
h: height, | ||
}; | ||
} | ||
function site(bidderRequest) { | ||
const pubId = bidderRequest && bidderRequest.length > 0 ? bidderRequest[0].params.pubId : '0'; | ||
const siteId = bidderRequest && bidderRequest.length > 0 ? bidderRequest[0].params.siteId : '0'; | ||
const appParams = bidderRequest[0].params.app; | ||
if (!appParams) { | ||
return { | ||
publisher: { | ||
id: pubId.toString(), | ||
domain: getTopWindowLocation().hostname, | ||
}, | ||
id: siteId.toString(), | ||
ref: referrer(), | ||
page: getTopWindowLocation().href, | ||
} | ||
} | ||
return null; | ||
} | ||
function referrer() { | ||
try { | ||
return window.top.document.referrer; | ||
} catch (e) { | ||
return document.referrer; | ||
} | ||
} | ||
function device() { | ||
return { | ||
ua: navigator.userAgent, | ||
language: (navigator.language || navigator.browserLanguage || navigator.userLanguage || navigator.systemLanguage), | ||
w: (window.screen.width || window.innerWidth), | ||
h: (window.screen.height || window.innerHeigh), | ||
}; | ||
} | ||
function parse(rawResponse) { | ||
try { | ||
if (rawResponse) { | ||
return JSON.parse(rawResponse); | ||
} | ||
} catch (ex) { | ||
logError('platformio.parse', 'ERROR', ex); | ||
} | ||
return null; | ||
} | ||
|
||
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,27 @@ | ||
# Overview | ||
|
||
**Module Name**: Platform.io Bidder Adapter | ||
**Module Type**: Bidder Adapter | ||
**Maintainer**: sk@ultralab.by | ||
|
||
# Description | ||
|
||
Connects to Platform.io demand source to fetch bids. | ||
Please use ```platformio``` as the bidder code. | ||
|
||
# Test Parameters | ||
``` | ||
var adUnits = [{ | ||
code: 'banner-ad-div', | ||
sizes: [[300, 250]], | ||
bids: [{ | ||
bidder: 'platformio', | ||
params: { | ||
pubId: '28082', | ||
siteId: '26047', | ||
placementId: '123', | ||
size: '250X250' | ||
} | ||
}] | ||
}]; | ||
``` |
Oops, something went wrong.