Skip to content

Instantly share code, notes, and snippets.

@alexwcarl
Created December 13, 2016 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexwcarl/e90eae5bb8c78991e0553413200d87a9 to your computer and use it in GitHub Desktop.
Save alexwcarl/e90eae5bb8c78991e0553413200d87a9 to your computer and use it in GitHub Desktop.
Adzerk/prebid.js Header Bidding Example HTML
<html>
<head>
<script>
/* Load adOS.js */
(function() {
var d = document;
var protocol = d.location.protocol == "https:" ? "https" : "http";
var z = d.createElement("script");
z.type = "text/javascript";
z.src = protocol + "://static.adzerk.net/ados.js";
z.async = true;
var s = d.getElementsByTagName("script")[0];
s.parentNode.insertBefore(z,s);
})();
function getWinner(bidResponses) {
var veryLowNumber = -1 * Math.pow(2,32);
var highestCPM = veryLowNumber;
var bids = bidResponses[PREBID_CODE_KEY].bids;
for (var i = 0; i < bids.length; i++) {
if (highestCPM > bids[i].cpm) {
continue;
} else {
highestCPM = bids[i].cpm;
tmpWinner = bids[i];
}
}
return tmpWinner;
}
function makeOverrides(winningBid) {
var ovr = {"ads": {}};
ovr.ads[ADZERK_AD_ID] = {"ecpm": winningBid.cpm};
return ovr;
}
function makeProperties(winningBid) {
console.log({"hb_adid": winningBid.adId});
return {"hb_adid": winningBid.adId};
}
/* Fill in these values with your own! */
var PREBID_TIMEOUT = 7000,
PREBID_CODE_KEY = 'azk8677',
// The various ids of the ad placeholder in adzerk.
ADZERK_ADTYPE_ID = 5,
ADZERK_NETWORK_ID = 23,
ADZERK_SITE_ID = 682165,
ADZERK_AD_ID = 2158819;
/* pbjs.initAdserver will be called either when all bids are back, or
when the timeout is reached. */
function initAdserver() {
if (pbjs.initAdserverSet) return;
pbjs.initAdserverSet = true;
ados.run.push(function () {
pbjs.que.push(function () {
// bid info is back from Prebid
console.log('Dumping all bid objects to console:', pbjs.getBidResponses());
var winner = getWinner(pbjs.getBidResponses());
console.log("bid winner: ", winner);
// add an ados placement to the ados request
ados_add_placement(ADZERK_NETWORK_ID, ADZERK_SITE_ID, 'azk8677', ADZERK_ADTYPE_ID)
.setRedirectUrl('-optional-click-macro-')
.setProperties(makeProperties(winner))
.setOverrides(makeOverrides(winner));
ados_load();
});
});
}
// Load GPT when timeout is reached.
setTimeout(initAdserver, PREBID_TIMEOUT);
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
/* ados */
var ados = ados || {};
ados.run = ados.run || [];
// Load the Prebid Javascript Library Async. We recommend loading it immediately after
// the initAdserver() and setTimeout functions.
(function () {
var d = document;
var pbs = d.createElement("script");
pbs.type = "text/javascript";
pbs.src = 'http://headertag.com/prebid/prebid-0.8-beta.js';
var target = d.getElementsByTagName("head")[0];
target.insertBefore(pbs, target.firstChild);
})();
pbjs.que.push(function () {
/* 1. Register bidder tag Ids
Registers the bidder tags for your ad units. Once the prebid.js
library loads, it reads the pbjs.adUnits object and sends out
bid requests. Find the complete reference on bidders at
http://prebid.org/bidders.html.
*/
var adUnits = [
{
code: PREBID_CODE_KEY,
sizes: [[300, 250]],
bids: [
{
bidder: 'indexExchange',
params: {
id: '1',
siteID: 999990
}
},
{
bidder: 'aol',
params: {
placement: '3675026',
network: '9599.1'
}
}
]
}
];
// add the adUnits
pbjs.addAdUnits(adUnits);
// register a callback handler
pbjs.addCallback('adUnitBidsBack', function (adUnitCode) {
console.log('ad unit bids back for : ' + adUnitCode);
});
/* Request bids for the added ad units. If adUnits or adUnitCodes are
not specified, the function will request bids for all added ad units. */
pbjs.requestBids({
/* The bidsBack function will be called when either timeout is
reached, or when all bids come back, whichever happens sooner. */
bidsBackHandler: function (bidResponses) {
initAdserver();
}
});
});
</script>
<script>
</script>
</head>
<body>
<h2>Prebid + Adzerk Test</h2>
<div id="azk8677"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment