-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathpbjs_partial_refresh_gpt.html
296 lines (241 loc) · 9.73 KB
/
pbjs_partial_refresh_gpt.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<html>
<head>
<script>
var PREBID_TIMEOUT = 700;
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function () {
googletag.pubads().disableInitialLoad();
});
/* $$PREBID_GLOBAL$$.initAdserver will be called either when all bids are back, or
when the timeout is reached.
*/
function initAdserver() {
if ($$PREBID_GLOBAL$$.initAdserverSet) return;
googletag.cmd.push(function () {
$$PREBID_GLOBAL$$.cmd.push(function () {
$$PREBID_GLOBAL$$.setTargetingForGPTAsync();
googletag.pubads().refresh();
});
});
$$PREBID_GLOBAL$$.initAdserverSet = true;
}
// Load GPT when timeout is reached.
setTimeout(initAdserver, PREBID_TIMEOUT);
var $$PREBID_GLOBAL$$ = $$PREBID_GLOBAL$$ || {};
$$PREBID_GLOBAL$$.cmd = $$PREBID_GLOBAL$$.cmd || [];
// Load the Prebid Javascript Library Async. We recommend loading it immediately after
// the initAdserver() and setTimeout functions.
(function () {
var d = document, pbs = d.createElement("script"), pro = d.location.protocal;
pbs.type = "text/javascript";
pbs.src = '/build/dist/prebid.js';
var target = document.getElementsByTagName("head")[0];
target.insertBefore(pbs, target.firstChild);
})();
$$PREBID_GLOBAL$$.cmd.push(function () {
/* 1. Register bidder tag Ids
Registers the bidder tags for your ad units. Once the prebid.js
library loads, it reads the $$PREBID_GLOBAL$$.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: '/9968336/header-bid-tag-0',
sizes: [[300, 250], [300, 600]],
bids: [
// 1 ad unit can be targeted by multiple bids.
{
bidder: 'appnexus',
params: {
placementId: 'TO ADD'
}
},
// 1 ad unit can also be targeted by multiple bids from 1 bidder
{
bidder: 'pubmatic',
params: {
publisherId: 'TO ADD',
adSlot: 'TO ADD@300x250'
}
}, {
bidder: 'pubmatic',
params: {
publisherId: 'TO ADD',
adSlot: 'TO ADD@300x600'
}
}
]
}, {
code: '/9968336/header-bid-tag1',
sizes: [[728, 90], [970, 90]],
bids: [
{
bidder: 'appnexus',
params: {
placementId: 'TO ADD'
}
}, {
bidder: 'openx',
params: {
pgid: 'TO ADD',
unit: 'TO ADD',
jstag_url: 'TO ADD'
}
}, {
bidder: 'rubicon',
params: {
accountId: "1234", //String - required
siteId: "1234", //String - required
zoneId: "1234", //String - required
sizes: [15], //Array[Number] - optional
userId: "12345", //String - optional
keywords: ["a", "b", "c"], //Array[String] - optional
inventory: { //Object - optional
rating: "5-star",
prodtype: "tech"
},
visitor: { //Object - optional
ucat: "new",
search: "iphone"
}
}
}, {
bidder: 'yieldbot',
params: {
pub: 'TO ADD',
name: 'TO ADD'
}
},
{
bidder: 'casale',
params: {
slotId: 'TO ADD', // number
casaleUrl: 'TO ADD'
}
}
]
}
];
//add the adUnits
$$PREBID_GLOBAL$$.addAdUnits(adUnits);
//register a callback handler
$$PREBID_GLOBAL$$.addCallback('adUnitBidsBack', function (adUnitCode) {
console.log('ad unit bids back for : ' + adUnitCode);
});
/* Request bids for the added ad units. If adUnits or adUnitCodes are
not specified, the function will request bids for all added ad units.
*/
$$PREBID_GLOBAL$$.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();
},
/* 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.
*/
$$PREBID_GLOBAL$$.bidderSettings = {
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.pbLg;
}
}
]
}
};
});
</script>
<script>
googletag.cmd.push(function () {
var rightSlot = googletag.defineSlot('/9968336/header-bid-tag-0', [[300, 250],[300, 600]], 'div-gpt-ad-12345678-0').addService(googletag.pubads());
var topSlot = googletag.defineSlot('/9968336/header-bid-tag1', [[728, 90],[970, 90]], 'div-gpt-ad-12345678-1').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
function refreshBid1() {
$$PREBID_GLOBAL$$.cmd.push(function () {
$$PREBID_GLOBAL$$.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: ['/9968336/header-bid-tag-0'],
bidsBackHandler: function () {
$$PREBID_GLOBAL$$.setTargetingForGPTAsync(['/9968336/header-bid-tag-0']);
googletag.pubads().refresh([rightSlot]);
}
});
});
}
function refreshBid2() {
$$PREBID_GLOBAL$$.cmd.push(function () {
$$PREBID_GLOBAL$$.requestBids({
timeout: PREBID_TIMEOUT,
adUnitCodes: ['/9968336/header-bid-tag1'],
bidsBackHandler: function () {
$$PREBID_GLOBAL$$.setTargetingForGPTAsync(['/9968336/header-bid-tag1']);
googletag.pubads().refresh([topSlot]);
}
});
});
}
</script>
</head>
<body>
<h2>Prebid.js Test</h2>
<h5>Div-1, 300x250 or 300x600</h5>
<button onclick="refreshBid1()">Refresh 300x250 Ad Unit</button>
<div id='div-gpt-ad-12345678-0'>
<script type='text/javascript'>
googletag.cmd.push(function () {
googletag.display('div-gpt-ad-12345678-0');
});
</script>
</div>
<h5>Div-2, 728x90 or 970x90</h5>
<button onclick="refreshBid2()">Refresh 728x90 Ad Unit</button>
<div id='div-gpt-ad-12345678-1'>
<script type='text/javascript'>
googletag.cmd.push(function () {
googletag.display('div-gpt-ad-12345678-1');
});
</script>
</div>
</body>
</html>