Skip to content

Commit

Permalink
Merge pull request #7 from freestarcapital/ENG-623
Browse files Browse the repository at this point in the history
Eng 623
  • Loading branch information
aecook authored May 1, 2018
2 parents 2304848 + a3c483a commit ac46e78
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 6,413 deletions.
102 changes: 102 additions & 0 deletions integrationExamples/gpt/freestar_hello_world.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!--
This page calls a single bidder for a single ad slot. It can be considered a "hello world" example for using
Prebid with the Google Publisher Tag.
It also makes a good test page for new adapter PR submissions. Simply set your server's Bid Params object in the
bids array inside the adUnits, and it will use your adapter to load an ad.
NOTE that many ad servers won't send back an ad if the URL is localhost... so you might need to
set an alias in your /etc/hosts file so that you can load this page from a different domain.
-->

<html>
<head>
<script>
var PREBID_TIMEOUT = 700;

var adUnits = [{
code: 'div-gpt-ad-1460505748561-0',
sizes: [[300, 250], [300,600]],

// Replace this object to test a new Adapter!
bids: [{
bidder: 'freestar',
params: {
placementId: '10433394'
}
}]

}];

var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];

</script>

<script type="text/javascript" src="/build/dist/prebid.js" async></script>
<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function() {
googletag.pubads().disableInitialLoad();
});

pbjs.que.push(function() {
pbjs.addAdUnits(adUnits);
pbjs.requestBids({
bidsBackHandler: sendAdserverRequest
});
});

function sendAdserverRequest() {
if (pbjs.adserverRequestSent) return;
pbjs.adserverRequestSent = true;
googletag.cmd.push(function() {
pbjs.que.push(function() {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh();
});
});
}

setTimeout(function() {
sendAdserverRequest();
}, PREBID_TIMEOUT);

</script>

<script>
(function () {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>

<script>
googletag.cmd.push(function () {
googletag.defineSlot('/19968336/header-bid-tag-0', [[300, 250], [300, 600]], 'div-gpt-ad-1460505748561-0').addService(googletag.pubads());

googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
</head>

<body>
<h2>Prebid.js Test</h2>
<h5>Div-1</h5>
<div id='div-gpt-ad-1460505748561-0'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1460505748561-0'); });
</script>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion modules.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[
"freestarAnalyticsAdapter",
"adyoulikeBidAdapter",
"aolBidAdapter",
"appnexusBidAdapter",
"audienceNetworkBidAdapter",
"conversantBidAdapter",
"criteoBidAdapter",
"freestarAnalyticsAdapter",
"freestarBidAdapter",
"gumgumBidAdapter",
"justpremiumBidAdapter",
"komoonaBidAdapter",
Expand Down
183 changes: 183 additions & 0 deletions modules/freestarBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import {config} from 'src/config';
import {registerBidder} from 'src/adapters/bidderFactory';
import {BANNER, NATIVE, VIDEO} from "../src/mediaTypes";
const BIDDER_CODE = 'freestar';
const ENDPOINT_URL = 'https://ssp.pub.network/ssp-server/HeaderBiddingService';
const syncURLs = [];
export const spec = {
code: BIDDER_CODE,

/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
// @TODO: add some validation
return true;
// return !!(bid.params.placementId || (bid.params.member && bid.params.invCode));
},


/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests) {
const adUnitsToBidUpon = validBidRequests.map(formatBid),
payload = {},
cookie = window.document.cookie.split(';'),
cookieObj = {};
for (var i = 0; i < cookie.length; i++) {
var tmp;
// see if _fs* is within the string
if (cookie[i].indexOf('_fs') != -1) {
// if so, split it
tmp = cookie[i].split('=');
// check if _fsloc, whose values has = within it
if(tmp[0].trim() == '_fsloc') {
// and if so, manipulate differently
tmp.shift();
cookieObj['_fsloc'] = tmp.join('=');
} else {
// if not, add to obj
cookieObj[tmp[0].trim()] = tmp[1].trim();
}
}
}
// throw it all into an object and pass it along
payload['id'] = uid();
payload['adUnitsToBidUpon'] = adUnitsToBidUpon;
payload['site'] = location.hostname;
payload['page'] = location.pathname;
for (var key in cookieObj) {
payload[key] = cookieObj[key];
}
return {
method: 'POST',
url: ENDPOINT_URL,
data: JSON.stringify(payload),
};
},

/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse) {
serverResponse = serverResponse.body;
const bids = [];
if(serverResponse.winningProvider) {
let winners = serverResponse.winningProvider;
if(winners instanceof Array) {
winners.forEach(function(winner) {
bids.push(parseBid(Object.assign(
{},
{
requestId: serverResponse.bidRequest.id,
currency: winner.currency
},
winner.winningSeat.bid[0],
)));
if(typeof winner.supplier.cookieSync != 'undefined') {
syncURLs.push({
type: 'image',
url: decodeURIComponent(winner.supplier.cookieSync).split('\'')[1]
})
}
});
} else {
let winner = winners;
bids.push(parseBid(Object.assign(
{},
{
requestId: serverResponse.bidRequest.id,
currency: winner.currency
},
winner.winningSeat.bid[0],
)));
if(typeof winner.supplier.cookieSync != 'undefined') {
syncURLs.push(decodeURIComponent(winner.supplier.cookieSync).split('\'')[1]);
}
}
}
return bids;
},
// @TODO: How are we doing user sync?
getUserSyncs: function(syncOptions) {
if(syncURLs.length > 0) {
let tmp = syncURLs.filter((elm, pos, arr) => {
console.log('freestar::', 'elm, pos, arr', elm, pos, arr);
});
}
return false;
}
}

registerBidder(spec);

// HELPER FUNCTIONS

/**
* Parse the response from the server, formating it for Prebid to use
*
* @param bid
* @returns {{requestId, cpm, width, height, creativeId: string|string|string|*|string|string, currency, netRevenue: boolean, ttl: number, ad}}
*/
function parseBid(bid) {
const bidResponse = {
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.cid, //@TODO: verify
// // dealId: DEAL_ID,
currency: bid.currency,
netRevenue: true,
ttl: 60, //@TODO: verify
ad: bid.adm
};
return bidResponse;
}

/**
* Creates an object to send to the bidding endpoint
*
* @param bid
* @returns {{}}
*/
function formatBid(bid) {
var str = {}, res = [];
str.id = bid.bidId;
str.adUnitCode = bid.adUnitCode;
str.size = bid.sizes[0].join('x');
str.promo_sizes = bid.sizes.slice(1).map(function(size) {
return size.join('x');
});
str.promo_sizes = str.promo_sizes.join(',');
if(typeof bid.params != 'undefined') {
for(var key in bid.params) {
str[key] = bid.params[key];
}
}
return str;
}

/**
* Generates a basic unique ID
*
* @returns {string}
*/
function uid() {
const src = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var uid = '';
for(var i = 0; i < 16; i++) {
uid += src.substr((Math.floor(Math.random() * src.length) + 1 ),1)
}
return uid;
}
Loading

0 comments on commit ac46e78

Please sign in to comment.