JavaScript
Prebid.js Configuration & GAM Integration
// 1. Initialize Prebid and GPT queues
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
// 2. Global Prebid Configuration
pbjs.que.push(function() {
pbjs.setConfig({
bidderTimeout: 1000, // Wait maximum 1000ms for bidder responses
priceGranularity: 'dense', // $0.01 increments up to $3, $0.05 up to $8, etc.
enableSendAllBids: true, // Send all bidder key-values into GAM for granular reporting
consentManagement: {
gdpr: {
cmpApi: 'iab', // Automatically detect IAB TCF v2.2 CMP on page
timeout: 800, // CMP timeout before falling back
defaultGdprScope: true
},
usp: {
cmpApi: 'iab', // US Privacy / CCPA string detection
timeout: 500
}
}
});
// 3. Define Ad Units with Multi-Bidder Parameters
var adUnits = [
{
code: 'div-gpt-ad-leaderboard', // Div ID of the ad slot on page
mediaTypes: {
banner: {
sizes: [[728, 90], [970, 250]] // Multi-size support
}
},
bids: [
{
bidder: 'appnexus',
params: { placementId: 13144370 }
},
{
bidder: 'rubicon',
params: { accountId: 14062, siteId: 70608, zoneId: 344324 }
},
{
bidder: 'openx',
params: { unit: '541234567', delDomain: 'publisher-d.openx.net' }
}
]
}
];
pbjs.addAdUnits(adUnits);
// 4. Request Bids and Send Key-Values to GAM
pbjs.requestBids({
bidsBackHandler: initAdserver
});
});
// 5. Handoff to Google Ad Manager
function initAdserver() {
if (pbjs.initAdserverSet) return;
pbjs.initAdserverSet = true;
googletag.cmd.push(function() {
pbjs.que.push(function() {
pbjs.setTargetingForGPTAsync(); // Sets hb_pb, hb_bidder, hb_adid on GPT slots
googletag.pubads().refresh(); // Fires GAM ad request with targeting
});
});
}