-
Notifications
You must be signed in to change notification settings - Fork 39
/
video-ad-helper.js
209 lines (190 loc) · 6.7 KB
/
video-ad-helper.js
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
// Copyright 2013 Google Inc. All Rights Reserved.
// You may study, modify, and use this example for any purpose.
// Note that this example is provided "as is", WITHOUT WARRANTY
// of any kind either expressed or implied.
let adsManager;
let adsLoader;
let adDisplayContainer;
let intervalTimer;
let isAdPlaying;
let isContentFinished;
let playButton;
let videoContent;
/**
* Initializes IMA setup.
*/
function init() {
videoContent = document.getElementById('contentElement');
playButton = document.getElementById('playButton');
playButton.addEventListener('click', playAds);
console.log('[DEMO] Organic video initialized.')
}
/**
* Sets up IMA ad display container, ads loader, and makes an ad request.
*/
function setUpIMA(adTagUrl) {
if (!adTagUrl) {
return console.log('[DEMO] No VAST XML URL provided.')
}
// Create the ad display container.
createAdDisplayContainer();
// Create ads loader.
adsLoader = new google.ima.AdsLoader(adDisplayContainer);
// Listen and respond to ads loaded and error events.
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
onAdsManagerLoaded, false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR, onAdError, false);
// An event listener to tell the SDK that our content video
// is completed so the SDK can play any post-roll ads.
const contentEndedListener = function() {
// An ad might have been playing in the content element, in which case the
// content has not actually ended.
if (isAdPlaying) return;
isContentFinished = true;
adsLoader.contentComplete();
};
videoContent.onended = contentEndedListener;
// Request video ads.
const adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = adTagUrl;
// Specify the linear and nonlinear slot sizes. This helps the SDK to
// select the correct creative if multiple are returned.
adsRequest.linearAdSlotWidth = 640;
adsRequest.linearAdSlotHeight = 400;
adsRequest.nonLinearAdSlotWidth = 640;
adsRequest.nonLinearAdSlotHeight = 150;
adsLoader.requestAds(adsRequest);
}
/**
* Sets the 'adContainer' div as the IMA ad display container.
*/
function createAdDisplayContainer() {
// We assume the adContainer is the DOM id of the element that will house
// the ads.
adDisplayContainer = new google.ima.AdDisplayContainer(
document.getElementById('adContainer'), videoContent);
}
/**
* Loads the video content and initializes IMA ad playback.
*/
function playAds() {
// Initialize the container. Must be done through a user action on mobile
// devices.
videoContent.load();
if (adDisplayContainer) {
adDisplayContainer.initialize();
}
try {
// Initialize the ads manager. Ad rules playlist will start at this time.
adsManager.init(640, 360, google.ima.ViewMode.NORMAL);
// Call play to start showing the ad. Single video and overlay ads will
// start at this time; the call will be ignored for ad rules.
adsManager.start();
} catch (adError) {
// An error may be thrown if there was a problem with the VAST response.
videoContent.play();
}
}
/**
* Handles the ad manager loading and sets ad event listeners.
* @param {!google.ima.AdsManagerLoadedEvent} adsManagerLoadedEvent
*/
function onAdsManagerLoaded(adsManagerLoadedEvent) {
// Get the ads manager.
const adsRenderingSettings = new google.ima.AdsRenderingSettings();
adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
// videoContent should be set to the content video element.
adsManager =
adsManagerLoadedEvent.getAdsManager(videoContent, adsRenderingSettings);
// Add listeners to the required events.
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);
// Listen to any additional events, if necessary.
adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, onAdEvent);
adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, onAdEvent);
adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, onAdEvent);
console.log('Ads Manager loaded.')
}
/**
* Handles actions taken in response to ad events.
* @param {!google.ima.AdEvent} adEvent
*/
function onAdEvent(adEvent) {
// Retrieve the ad from the event. Some events (for example,
// ALL_ADS_COMPLETED) don't have ad object associated.
const ad = adEvent.getAd();
switch (adEvent.type) {
case google.ima.AdEvent.Type.LOADED:
// This is the first event sent for an ad - it is possible to
// determine whether the ad is a video ad or an overlay.
if (!ad.isLinear()) {
// Position AdDisplayContainer correctly for overlay.
// Use ad.width and ad.height.
videoContent.play();
}
break;
case google.ima.AdEvent.Type.STARTED:
// This event indicates the ad has started - the video player
// can adjust the UI, for example display a pause button and
// remaining time.
if (ad.isLinear()) {
// For a linear ad, a timer can be started to poll for
// the remaining time.
intervalTimer = setInterval(
function() {
// Example: const remainingTime = adsManager.getRemainingTime();
},
300); // every 300ms
}
break;
case google.ima.AdEvent.Type.COMPLETE:
// This event indicates the ad has finished - the video player
// can perform appropriate UI actions, such as removing the timer for
// remaining time detection.
if (ad.isLinear()) {
clearInterval(intervalTimer);
}
break;
}
}
/**
* Handles ad errors.
* @param {!google.ima.AdErrorEvent} adErrorEvent
*/
function onAdError(adErrorEvent) {
// Handle the error logging.
console.log(adErrorEvent.getError());
adsManager.destroy();
}
/**
* Pauses video content and sets up ad UI.
*/
function onContentPauseRequested() {
isAdPlaying = true;
videoContent.pause();
// This function is where you should setup UI for showing ads (for example,
// display ad timer countdown, disable seeking and more.)
// setupUIForAds();
}
/**
* Resumes video content and removes ad UI.
*/
function onContentResumeRequested() {
isAdPlaying = false;
if (!isContentFinished) {
videoContent.play();
}
// This function is where you should ensure that your UI is ready
// to play content. It is the responsibility of the Publisher to
// implement this function when necessary.
// setupUIForContent();
}
init();