Skip to content
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

Platform.io Bidder Adapter update. Prebid v1.0. #1705

Merged
merged 29 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2cc71fb
Add PlatformioBidAdapter
varashellov Aug 1, 2017
6801ca1
Update platformioBidAdapter.js
varashellov Aug 2, 2017
9bdcd7e
Add files via upload
varashellov Aug 2, 2017
06c04ce
Update hello_world.html
varashellov Aug 2, 2017
3e11a2a
Update platformioBidAdapter.js
varashellov Aug 2, 2017
e3d049e
Update platformioBidAdapter_spec.js
varashellov Aug 2, 2017
c09d8ae
Update hello_world.html
varashellov Aug 2, 2017
e0be49c
Update platformioBidAdapter_spec.js
varashellov Aug 7, 2017
eedbd3e
Update platformioBidAdapter.js
varashellov Aug 14, 2017
faa9963
Update hello_world.html
varashellov Aug 14, 2017
946d081
Add files via upload
varashellov Aug 14, 2017
69b4f57
Update platformioBidAdapter
varashellov Aug 16, 2017
659562a
Update platformioBidAdapter
varashellov Aug 16, 2017
66a3f7f
Update platformioBidAdapter
varashellov Aug 16, 2017
225b798
Merge pull request #1 from varashellov/varashellov-patch-1
varashellov Aug 16, 2017
cd21b96
Add files via upload
varashellov Oct 17, 2017
e426d54
Add files via upload
varashellov Oct 17, 2017
4ac6786
Add files via upload
varashellov Oct 17, 2017
6cc3ec1
Merge branch 'master' into master
varashellov Oct 18, 2017
ccdfd49
Update platformioBidAdapter.js
varashellov Oct 19, 2017
3947c83
Update platformioBidAdapter_spec.js
varashellov Oct 19, 2017
6451e5b
Update platformioBidAdapter_spec.js
varashellov Oct 19, 2017
9282871
Update platformioBidAdapter_spec.js
varashellov Oct 19, 2017
7b60d11
Update platformioBidAdapter.js
varashellov Oct 19, 2017
1124da8
Update platformioBidAdapter.js
varashellov Oct 19, 2017
176c633
Update platformioBidAdapter_spec.js
varashellov Oct 19, 2017
96245d6
Add files via upload
varashellov Oct 20, 2017
380be34
Add files via upload
varashellov Oct 20, 2017
ffcfb29
Add files via upload
varashellov Oct 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 125 additions & 63 deletions modules/platformioBidAdapter.js
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);
27 changes: 27 additions & 0 deletions modules/platformioBidAdapter.md
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'
}
}]
}];
```
Loading