UNPKG

mk9-prebid

Version:

Header Bidding Management Library

165 lines (145 loc) 5.31 kB
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="robots" content="noindex"> <script> /** This file is the AMP x-domain iframe source file. * Host this file on a cross-domain from your AMP pages, * set the `amp-3p-iframe-src` meta tag on your AMP pages * (see `./amp_page.html`) * * see "Enhance incoming ad configuration" section of AMP docs * https://www.ampproject.org/docs/reference/components/amp-ad */ (function() { var v = location.search.substr(1); if (!(/^\d+(-canary)?$/.test(v))) return; var u = 'https://3p.ampproject.net/'+encodeURIComponent(v)+'/f.js'; document.write('<script'+' src="'+encodeURI(u)+'"><'+'/script>'); })(); </script> <!-- start Prebid.js AMP integration --> <script> // The Prebid global must match Prebid.js setting. This example file is not preprocessed so below we refer to global as `pbjs`. var $$PREBID_GLOBAL$$ = pbjs; var prebidSrc = 'https://publisher.com:9999/build/dev/prebid.js'; var adUnits; var requestBidsDuration; // load Prebid.js (function () { var d = document, pbs = d.createElement("script"), pro = d.location.protocal; pbs.type = "text/javascript"; pbs.src = prebidSrc; var target = document.getElementsByTagName("head")[0]; target.insertBefore(pbs, target.firstChild); })(); var pbjs = pbjs || {}; pbjs.que = pbjs.que || []; var date = new Date().getTime(); /** wrap the rest of the setup in a function that will be called by the * AMP `draw3p` hook see * this example for more info: * https://dfp-amp-testing-1185.appspot.com/amp_tests/dfp-3p-iframe.html * */ function loadPrebidJS() { pbjs.que.push(function () { pbjs.setConfig({ debug: true }); pbjs.addAdUnits(adUnits); pbjs.requestBids({ bidsBackHandler: function (bidResponses) { initAdserver(); console.log('bidsBackHandler responses: ', bidResponses); }, timeout: requestBidsDuration }); }); } function setTargeting(config, done) { config.targeting = getTargeting(config.slot); done(config); } function getTargeting(slot) { var targeting = window.context.master.pbjs.getAdserverTargeting()[slot] || {}; for (var key in targeting) { if (targeting.hasOwnProperty(key)) { targeting[key] = [targeting[key]]; } } if (Object.keys(targeting).length) { // only set this key if prebid targeting is present targeting['prebid_amp'] = ['true']; } return targeting; } function initAdserver() { var i; var prebidAdCalls = window.context.master.prebidAdCalls; var len = prebidAdCalls.length; for (i = 0; i < len; i++) { prebidAdCalls.pop()(); } } function listenAdRequestFromCreative() { addEventListener('message', sendAdToCreative, false); } function sendAdToCreative(ev) { var key = ev.message ? 'message' : 'data'; var data = {}; try { data = JSON.parse(ev[key]); } catch (e) { // Do nothing. No ad found. } if (data.adId) { // AMP ads a `context` object to `window`s and that is used to find the // `master` iframe where Prebid is loaded var adObject = window.context.master.pbjs._bidsReceived.find(function (bid) { return bid.adId === data.adId; }); var ad = adObject.ad; var adUrl = adObject.adUrl; var width = adObject.width; var height = adObject.height; var message = JSON.stringify({ message: 'Prebid creative sent: ' + data.adId, ad: ad, adUrl: adUrl, width: width, height: height }); ev.source.postMessage(message, '*'); } } </script> </head> <body style="margin:0"> <div id="c" style="position:absolute;top:0;left:0;bottom:0;right:0;"> <script> /** The draw3p function is the integration point between AMP and content in third party * iframes. For more info see: https://github.com/ampproject/amphtml/blob/e5501a30adf15c8fef049729f5e0e3137dbb18ca/3p/integration.js#L252 */ draw3p(function(config, done) { if (config.prebid) { if (typeof window.context.master.prebidAdCalls === 'undefined') { window.context.master.prebidAdCalls = []; } if (window.context && window.context.isMaster) { var prebid = config.prebid; adUnits = prebid.adUnits; requestBidsDuration = prebid.requestBidsDuration; loadPrebidJS(); } window.context.master.prebidAdCalls.push(setTargeting.bind(null, config, done)); } else { // if the slot doesn't have prebid config then fire callback done(config); } }, ['doubleclick', 'taboola'], ['publisher.com']); // the first array contains ad networks used, the second contains domains allowed to load this file in an iframe listenAdRequestFromCreative(); </script> </div> <script>if (window.docEndCallback) window.docEndCallback()</script> </body> </html>