Last active
March 2, 2021 22:51
-
-
Save Heray/277d421bda2b827f43da to your computer and use it in GitHub Desktop.
How to add Amazon as a custom prebid bidder in my site
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
<html> | |
<head> | |
<script> | |
var PREBID_TIMEOUT = 400; | |
var googletag = googletag || {}; | |
googletag.cmd = googletag.cmd || []; | |
function initAdserver() { | |
if (pbjs.initAdserverSet) return; | |
(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); | |
})(); | |
pbjs.initAdserverSet = true; | |
}; | |
setTimeout(initAdserver, PREBID_TIMEOUT); | |
var pbjs = pbjs || {}; | |
pbjs.que = pbjs.que || []; | |
(function() { | |
var pbjsEl = document.createElement("script"); pbjsEl.type = "text/javascript"; | |
pbjsEl.async = true; | |
pbjsEl.src = '//acdn.adnxs.com/prebid/prebid.js'; | |
var pbjsTargetEl = document.getElementsByTagName("head")[0]; | |
pbjsTargetEl.insertBefore(pbjsEl, pbjsTargetEl.firstChild); | |
})(); | |
pbjs.que.push(function() { | |
var adUnits = [{ | |
code: '/19968336/header-bid-tag-0', | |
sizes: [[300, 250], [300, 600]], | |
bids: [ | |
// Add bids of a custom adaptor (in this case Amazon) to inform | |
// Prebid.js that it should call its adaptor (in this case A9Adaptor | |
// as below) | |
{ | |
bidder: 'amazon' | |
}, | |
{ | |
bidder: 'appnexus', | |
params: { | |
placementId: '4799418' | |
} | |
} | |
] | |
}]; | |
pbjs.addAdUnits(adUnits); | |
// A custom bidder adaptor that will run together with other registered bidders in an | |
// unbiased fashion (start at the same time, same timeout, etc) | |
var A9Adaptor = function A9Adaptor() { | |
return { | |
callBids: function(p) { | |
pbjs.loadScript('//c.amazon-adsystem.com/aax2/amzn_ads.js', function() { | |
amznads.getAdsCallback('YOUR AMAZON AD ID', function() { | |
// Indicate this bidder's bid is back here, so that Prebid.js can wait | |
// till this bid is back if all other bidders responded before the timeout. | |
pbjs.bidsAvailableForAdapter('amazon'); | |
}); | |
}); | |
} | |
}; | |
}; | |
pbjs.registerBidAdapter(A9Adaptor, 'amazon'); | |
pbjs.requestBids({ | |
bidsBackHandler: function(bidResponses) { | |
initAdserver(); | |
} | |
}) | |
}); | |
</script> | |
<script> | |
googletag.cmd.push(function() { | |
var rightSlot = googletag.defineSlot('/19968336/header-bid-tag-0', [[300, 250], [300, 600]], 'div-gpt-ad-1438287399331-0').addService(googletag.pubads()); | |
pbjs.que.push(function() { | |
pbjs.setTargetingForGPTAsync(); | |
// Set Amazon Targeting here together when you set targeting for other prebid bidders. | |
try{ amznads.setTargetingForGPTAsync('amznslots');} catch(e){} | |
}); | |
googletag.pubads().enableSingleRequest(); | |
googletag.enableServices(); | |
}); | |
</script> | |
</head> | |
<body style="padding:0;margin:0"> | |
<div id='div-gpt-ad-1438287399331-0'> | |
<script type='text/javascript'> | |
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1438287399331-0'); }); | |
</script> | |
</div> | |
</body> | |
</html> |
hello guys, I was wondering where we as a publisher can sign up to Amazon Header Bidding. Can you share details?
Back @rogerhirano's question
Hey, I just wanted to verify that this code still works. Looks cool!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A challenge with Amazon's header bidding tech is: how to start its ad request at the same time and give it the same timeout as my other header bidding partners. Luckily, prebid.js has a custom bidder adaptor option that can allow me to plug in any custom bidder, including Amazon.
To use this custom bidder adaptor for Amazon, in my ad server I still need to keep all my Amazon line items, creatives, and param targeting (note in Step 3 we still call Amazon's
setTargetingForGPT
directly). But by plugging it into Prebid.js' framework, I can ensure the unbiased auction logic among all my bidder partners including Amazon.Step 1:
Register the Amazon bidder in the adUnits object (any ad unit in the array is fine. It will trigger Prebid.js to call its adaptor which is registered in Step 2):
Step 2
Add the following code snippet after you defined the adUnits, so that Amazon can be called asynchronously together with other bidders.
Step 3
Call Amazon's setTargeting function together with Prebid.js' setTargeting function: