Skip to content

Instantly share code, notes, and snippets.

@Heray
Last active March 2, 2021 22:51
Show Gist options
  • Save Heray/277d421bda2b827f43da to your computer and use it in GitHub Desktop.
Save Heray/277d421bda2b827f43da to your computer and use it in GitHub Desktop.
How to add Amazon as a custom prebid bidder in my site
<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>
@Heray
Copy link
Author

Heray commented Nov 2, 2015

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):

var adUnits = [{
            code: '...',
            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'
                },
                ...

Step 2

Add the following code snippet after you defined the adUnits, so that Amazon can be called asynchronously together with other bidders.

        // 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');

Step 3

Call Amazon's setTargeting function together with Prebid.js' setTargeting function:

pbjs.setTargetingForGPTAsync();
try{ amznads.setTargetingForGPTAsync('amznslots');} catch(e){}

Copy link

ghost commented Aug 7, 2016

hello guys, I was wondering where we as a publisher can sign up to Amazon Header Bidding. Can you share details?

@ffeast
Copy link

ffeast commented Dec 26, 2016

Back @rogerhirano's question

@tedrand
Copy link

tedrand commented Mar 7, 2017

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