forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_pbjs_dfp_concurrently.html
282 lines (253 loc) · 7.75 KB
/
load_pbjs_dfp_concurrently.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<html>
<head>
<script>
/**
* This is the optimized way of loading prebid.js. It loads both DFP and Prebid concurrently.
* Publishers may wish to tweak the the MAX_RETRIES and polling interval ms as desired to mitigate issue https://github.com/prebid/Prebid.js/issues/419
*/
//timeout to control how long bidders have to respond.
var PREBID_TIMEOUT = 700;
//how many times to attempt to load DFP if services have not been enabled
var MAX_RETRIES = 20;
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
pbjs.retries = 0;
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function () {
googletag.pubads().disableInitialLoad();
});
/* initAdserver will be called either when all bids are back, or
when the timeout is reached.
*/
function initAdserver() {
if (pbjs.initAdserverSet) return;
//we don't want to call DFP before it is ready
if(!googletag.pubadsReady && pbjs.retries <= MAX_RETRIES) {
setTimeout(initAdserver, 50); //poll ms can be adjusted as desired.
pbjs.retries++;
return;
}
googletag.cmd.push(function () {
pbjs.que.push(function () {
pbjs.setTargetingForGPTAsync();
});
googletag.pubads().refresh();
});
pbjs.initAdserverSet = true;
}
//load DFP async.
loadScript('//www.googletagservices.com/tag/js/gpt.js');
// Load the Prebid Javascript Library Async. We recommend loading ASAP in page
loadScript('//acdn.adnxs.com/prebid/not-for-prod/prebid.js');
/**
* Loads the given script async
*/
function loadScript(scriptSrc) {
var script = document.createElement('script');
script.async=true;
script.type = 'text/javascript';
script.src = scriptSrc;
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(script, node);
}
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.
code: Your GPT slot’s ad unit path. If they don’t match, prebid.js
would not be able to set targeting correctly
sizes: All sizes your ad unit accepts. They should match with GPT.
*/
var adUnits = [{
code: 'div-gpt-ad-12345678-1',
sizes: [
[300, 250],
[300, 600]
],
bids: [
// 1 ad unit can be targeted by multiple bids.
{
bidder: 'appnexus',
params: {
placementId: '5626309'
}
}, {
bidder: 'adform',
// available params: [ 'mid', 'inv', 'pdom', 'mname', 'mkw', 'mkv', 'cat', 'bcat', 'bcatrt', 'adv', 'advt', 'cntr', 'cntrt', 'maxp', 'minp', 'sminp', 'w', 'h', 'pb', 'pos', 'cturl', 'iturl', 'cttype', 'hidedomain', 'cdims', 'test' ]
params: {
adxDomain: 'adx.adform.net', //optional
mid: 158989,
test: 1
}
}, {
bidder: 'yieldbot',
params: {
psn: '1234',
slot: 'medrec',
}
}, {
bidder: 'sovrn',
params: {
tagid: '315045' // <String> tag Id
}
}, {
bidder: 'pulsepoint',
params: {
cf: '300X250',
cp: 521732,
ct: 76835
}
}, {
bidder: 'springserve',
params: {
impId: 1234,
supplyPartnerId: 1,
test: true // only include when testing
}
}
]
}, {
code: 'div-gpt-ad-12345678-2',
sizes: [
[728, 90],
[970, 90]
],
bids: [{
bidder: 'appnexus',
params: {
placementId: '5626309'
}
}, {
bidder: 'rubicon',
params: {
accountId: "14062",
siteId: "70608",
zoneId: "335918"
}
}, {
bidder: 'indexExchange',
params: {
id: '1',
siteID: 123456,
timeout: 10000
}
}]
}];
//add the adUnits
pbjs.addAdUnits(adUnits);
/* 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();
},
timeout : PREBID_TIMEOUT
/* You can specify specific `adUnitCodes` to only request bids
for certain ad units.
adUnitCodes: ['code1', 'code2']
*/
/* You can also make one off bid requests for the given `adUnits`.
adUnits: [adUnit2, adUnit1]
*/
/* The bidsBackHandler will be executed either when all bids are
back, or when the timeout is reached.
timeout: 1000
*/
});
/* 2. Configure Ad Server Targeting
The below section defines what key value targeting will be sent to GPT.
For each bidder’s bid, Prebid.js will set the below 4 keys (hb_bidder,
hb_adid, hb_pb, hb_size) with their corresponding values.
Bidders all have different recommended ad server line item targeting and
creative setup. To remove the headache for you, Prebid.js has a default
recommended query string targeting setting for all bidders.
If you’d like to customize the key value pairs, you can overwrite the settings
as the below example shows. Let your ad ops team know about the change, so they
can update the line item targeting accordingly.
*/
pbjs.bidderSettings = {
//standard applies to all bidders
standard: {
adserverTargeting: [{
key: "hb_bidder",
val: function(bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "hb_adid",
val: function(bidResponse) {
return bidResponse.adId;
}
}, {
key: "hb_pb",
val: function(bidResponse) {
return bidResponse.pbMg;
}
}]
},
//You can call out a specific bidder to adjust them only as well.
appnexus: {
bidCpmAdjustment: function(bidCpm) { //adjust the CPM dynamically
return bidCpm * .9;
},
alwaysUseBid: true,
adserverTargeting: [{
key: "custom_bidder",
val: function(bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "custom_adid",
val: function(bidResponse) {
return bidResponse.adId;
}
}, {
key: "custom_pb",
val: function(bidResponse) {
return bidResponse.pbMg;
}
}, {
key: "custom_size",
val: function(bidResponse) {
return bidResponse.getSize();
}
}]
}
};
}); //end push command
</script>
<script>
googletag.cmd.push(function() {
var rightSlot = googletag.defineSlot('/19968336/header-bid-tag-1', [[300, 250], [300, 600]], 'div-gpt-ad-12345678-1').addService(googletag.pubads());
var topSlot = googletag.defineSlot('/19968336/header-bid-tag-2', [[728, 90], [970, 90]], 'div-gpt-ad-12345678-2').addService(googletag.pubads());
pbjs.que.push(function() {
/* Set query string targeting on all GPT ad units. The logic for deciding query strings is described
in the section Configure AdServer Targeting. Note that this function has to be called after all ad units on page are defined. */
pbjs.setTargetingForGPTAsync();
});
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
</head>
<body>
<h2>Prebid.js Test</h2>
<div id='div-gpt-ad-12345678-1'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-12345678-1'); });
</script>
</div>
<div id='div-gpt-ad-12345678-2'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-12345678-2'); });
</script>
</div>
</body>
</html>