-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVAST_InBannerVideo_with_fallback_GPT_display.html
257 lines (220 loc) · 7.99 KB
/
VAST_InBannerVideo_with_fallback_GPT_display.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
<html>
<head>
<title>LSVAST Video in Displayer 0.3.4 - 2-14-2017</title>
<!-- Originally made as an in banner video player, but could be adjusted to do whatever. -->
</head>
<style>
#mainContainer {
position: relative;
width: 300px;
height: 250px;
cursor: none;
}
#content,
#adContainer {
position: absolute;
top: 0px;
left: 0px;
width: 300px;
height: 250px;
}
#contentElement {
width: 300px;
height: 250px;
overflow: hidden;
}
</style>
<body>
<div id="mainContainer" onmouseover="mouseOverPlayer(this)" onmouseout="mouseOffPlayer(this)">
<div id="content">
<video id="contentElement">
</video>
</div>
<div id="adContainer"></div>
</div>
<!-- Include GPT and IMA3 javascript files -->
<script type='text/javascript' src='//www.googletagservices.com/tag/js/gpt.js'></script>
<script type="text/javascript" src="//imasdk.googleapis.com/js/sdkloader/ima3.js"></script>
<script type="text/javascript">
// Site to Ad unit Mapping
var siteAdUnits = [{
// Replace with your URL
site: 'www.w3schools.com',
networkType: 'display',
adUnit: '/6355419/travel' //Sample Google DFP Ad Unit Path
}, {
// Replace with your URL
site: 'www.w3schools.com',
networkType: 'VAST',
adUnit: 'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator=[timestamp]'
}]; //Sample Google DFP Ad Unit Path
function determineAdUnit(type) {
for (var i = 0; i < siteAdUnits.length; i++) {
if (siteAdUnits[i].site === window.location.hostname && siteAdUnits[i].networkType === type) {
return siteAdUnits[i].adUnit
console.log(siteAdUnits[i].adUnit)
}
}
};
// VAST Request
var videoRequestAdx = determineAdUnit("VAST");
var videoRequestStartVolume = 0;
var videoRequestHoverVolume = 0.5;
// In-Banner Video Ad Container Sizes
var videoInBannerContainerWidth = 300;
var videoInBannerContainerHeight = 250;
// Video Ad Sizes
var videoRequestlinearAdSlotWidth = 640;
var videoRequestlinearAdSlotHeight = 400;
// Logging Messages
var videoLogLoaded = 'Video Ad Loaded.';
var videoLogPlaying = 'Video Ad Playing.';
var videoLogCompleted = 'Video Ad Completed, rendering fallback display ad.';
var videoLogSkipped = 'Video Ad Skipped, rendering fallback display ad';
var videoLogErrorFallback = 'Video contained any error, rendering fallback display ad';
// IMA3 SDK Vars
var adsManager;
var adsLoader;
var adDisplayContainer;
var intervalTimer;
var playButton;
var videoContent;
// Fallback Display Ad GPT Sync Passback request
function displayFallbackAd() {
// Basic Data
path = determineAdUnit("display");
width = 300;
height = 250;
// GPT Render on page
googletag.pubads().definePassback(path, [
[width, height]
]).setTargeting("test", "responsive").display();
}
// IMA SDK Functions
function init() {
videoContent = document.getElementById('contentElement');
}
function createAdDisplayContainer() {
adDisplayContainer =
new google.ima.AdDisplayContainer(
document.getElementById('adContainer'), videoContent);
}
function requestAds() {
createAdDisplayContainer();
adDisplayContainer.initialize();
videoContent.load();
adsLoader = new google.ima.AdsLoader(adDisplayContainer);
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
onAdsManagerLoaded,
false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError,
false);
var adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = videoRequestAdx;
adsRequest.linearAdSlotWidth = videoRequestlinearAdSlotWidth;
adsRequest.linearAdSlotHeight = videoRequestlinearAdSlotHeight;
// Non Linear Ads not used, left as default.
adsRequest.nonLinearAdSlotWidth = 640;
adsRequest.nonLinearAdSlotHeight = 150;
adsLoader.requestAds(adsRequest);
}
function onAdsManagerLoaded(adsManagerLoadedEvent) {
var adsRenderingSettings = new google.ima.AdsRenderingSettings();
adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
adsManager = adsManagerLoadedEvent.getAdsManager(
videoContent, adsRenderingSettings);
adsManager.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,
onContentPauseRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,
onContentResumeRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.ALL_ADS_COMPLETED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.LOADED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.STARTED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.COMPLETE,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.SKIPPED,
onAdEvent);
try {
adsManager.init(videoInBannerContainerWidth, videoInBannerContainerHeight, google.ima.ViewMode.NORMAL);
// Start at 0 volume for better UX.
adsManager.setVolume(videoRequestStartVolume);
adsManager.start();
} catch (adError) {
videoContent.play();
}
}
function onAdEvent(adEvent) {
var ad = adEvent.getAd();
switch (adEvent.type) {
case google.ima.AdEvent.Type.LOADED:
if (!ad.isLinear()) {
videoContent.play();
console.log(videoLogLoaded)
}
break;
case google.ima.AdEvent.Type.STARTED:
console.log(videoLogPlaying)
if (ad.isLinear()) {
intervalTimer = setInterval(
function() {
var remainingTime = adsManager.getRemainingTime();
},
300); // every 300ms
}
break;
case google.ima.AdEvent.Type.COMPLETE:
console.log(videoLogCompleted)
if (ad.isLinear()) {
clearInterval(intervalTimer);
}
case google.ima.AdEvent.Type.SKIPPED:
console.log(videoLogSkipped)
// Skipped is also called when ad is completed, so for complete or skip = render the fallback display ad.
displayFallbackAd();
break;
}
}
function onAdError(adErrorEvent) {
// Handle the error logging.
console.log(adErrorEvent.getError());
console.log(videoLogErrorFallback);
// Do not destroy on error, we are falling back instead.
//adsManager.destroy();
displayFallbackAd();
}
function onContentPauseRequested() {
videoContent.pause();
// Out of scope for this implementation.
}
function onContentResumeRequested() {
videoContent.play();
// Out of scope for this implementation.
}
// Mouse over volume stuff
function mouseOverPlayer(x) {
adsManager.setVolume(videoRequestHoverVolume);
}
function mouseOffPlayer(x) {
adsManager.setVolume(videoRequestStartVolume);
}
init();
requestAds();
</script>
</body>
</html>