aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/plugins/ads.js
blob: e1c188c417d623025951198a6fc8e287c7ba8f74 (plain)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import utils from '../utils';

// Events are different on various devices. We set the correct events, based on userAgent.
const getStartEvents = () => {
    let events = ['click'];

    // TODO: Detecting touch is tricky, we should look at other ways?
    // For mobile users the start event will be one of
    // touchstart, touchend and touchmove.
    if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/Android/i)) {
        events = [
            'touchstart',
            'touchend',
            'touchmove',
        ];
    }

    return events;
};

export default class Ads {
    constructor(player) {
        this.player = player;

        // Check if a tag URL is provided.
        if (!utils.is.url(player.config.ads.tagUrl)) {
            return this;
        }

        // Check if the Google IMA3 SDK is loaded
        if (!utils.is.object(window.google)) {
            utils.loadScript(player.config.urls.googleIMA.api, () => {
                this.ready();
            });
        } else {
            this.ready();
        }
    }

    ready() {
        this.time = Date.now();
        this.startEvents = getStartEvents();
        this.adDisplayContainer = null;
        this.adsDisplayElement = null;
        this.adsManager = null;
        this.adsLoader = null;
        this.adsCuePoints = null;
        this.currentAd = null;
        this.events = {};
        this.safetyTimer = null;
        this.playing = false;

        // Setup a simple promise to resolve if the IMA loader is ready.
        this.adsLoaderResolve = () => {};
        this.adsLoaderPromise = new Promise((resolve) => {
            this.adsLoaderResolve = resolve;
        });
        this.adsLoaderPromise.then(() => {
            this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] adsLoader resolved!`, this.adsLoader);
        });

        // Setup a promise to resolve if the IMA manager is ready.
        this.adsManagerResolve = () => {};
        this.adsManagerPromise = new Promise((resolve) => {
            // Resolve our promise.
            this.adsManagerResolve = resolve;
        });
        this.adsManagerPromise.then(() => {
            // Clear the safety timer.
            this.clearSafetyTimer('onAdsManagerLoaded()');
            this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] adsManager resolved!`, this.adsManager);
        });

        // Start ticking our safety timer. If the whole advertisement
        // thing doesn't resolve within our set time; we bail.
        this.startSafetyTimer(12000, 'ready()');

        // Setup the ad display container.
        this.setupAdDisplayContainer();

        // Setup the IMA SDK.
        this.setupIMA();

        // Set listeners on the Plyr instance.
        this.setupListeners();
    }

    setupIMA() {
        const { container } = this.player.elements;

        // Create ads loader.
        this.adsLoader = new google.ima.AdsLoader(this.adDisplayContainer);

        // Tell the adsLoader we are handling ad breaks manually.
        this.adsLoader.getSettings().setAutoPlayAdBreaks(false);

        // Listen and respond to ads loaded and error events.
        this.adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, event => this.onAdsManagerLoaded(event), false);
        this.adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error), false);

        // Request video ads.
        const adsRequest = new google.ima.AdsRequest();
        adsRequest.adTagUrl = this.player.config.ads.tagUrl;

        // Specify the linear and nonlinear slot sizes. This helps the SDK to
        // select the correct creative if multiple are returned.
        adsRequest.linearAdSlotWidth = container.offsetWidth;
        adsRequest.linearAdSlotHeight = container.offsetHeight;
        adsRequest.nonLinearAdSlotWidth = container.offsetWidth;
        adsRequest.nonLinearAdSlotHeight = container.offsetHeight;

        this.adsLoader.requestAds(adsRequest);

        this.adsLoaderResolve();
    }

    onAdsManagerLoaded(adsManagerLoadedEvent) {

        // Get the ads manager.
        const settings = new google.ima.AdsRenderingSettings();

        // Tell the SDK NOT to save and restore content video state on our behalf.
        settings.restoreCustomPlaybackStateOnAdBreakComplete = false;
        settings.enablePreloading = true;

        // The SDK is polling currentTime on the contentPlayback. And needs a duration
        // so it can determine when to start the mid- and post-roll.
        this.adsManager = adsManagerLoadedEvent.getAdsManager(this.player, settings);

        // Get the cue points for any mid-rolls by filtering out the pre- and post-roll.
        this.adsCuePoints = this.adsManager.getCuePoints().filter(x => x > 0 && x !== -1);

        // Add listeners to the required events.
        this.adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error));
        this.adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, event => this.onAdEvent(event));
        this.adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, event => this.onAdEvent(event));
        this.adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, event => this.onAdEvent(event));
        this.adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY, event => this.onAdEvent(event));

        // Listen to any additional events, if necessary.
        this.adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, event => this.onAdEvent(event));
        this.adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, event => this.onAdEvent(event));
        this.adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, event => this.onAdEvent(event));

        // Resolve our adsManager.
        this.adsManagerResolve();
    }

    onAdEvent(event) {
        const { container } = this.player.elements;

        // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED)
        // don't have ad object associated.
        const ad = event.getAd();

        // Set the currently played ad. This information could be used by callback
        // events.
        this.currentAd = ad;

        // let intervalTimer;

        switch (event.type) {

            case google.ima.AdEvent.Type.AD_BREAK_READY:
                // This event indicates that a mid-roll ad is ready to start.
                // We pause the player and tell the adsManager to start playing the ad.
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] AD_BREAK_READY |`, 'Fired when an ad rule or a VMAP ad break would have played if autoPlayAdBreaks is false.');
                this.handleEventListeners('AD_BREAK_READY');
                this.adsManager.start();
                this.playing = true;
                break;
            case google.ima.AdEvent.Type.AD_METADATA:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] AD_METADATA |`, 'Fired when an ads list is loaded.');
                break;
            case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] ALL_ADS_COMPLETED |`, 'Fired when the ads manager is done playing all the ads.');
                this.handleEventListeners('ALL_ADS_COMPLETED');
                break;
            case google.ima.AdEvent.Type.CLICK:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] CLICK |`, 'Fired when the ad is clicked.');
                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.
                // clearInterval(intervalTimer);
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] COMPLETE |`, 'Fired when the ad completes playing.');
                this.handleEventListeners('COMPLETE');

                this.adsDisplayElement.style.display = 'none';
                this.playing = false;

                if (this.player.currentTime < this.player.duration) {
                    this.player.play();
                }
                break;
            case google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] CONTENT_PAUSE_REQUESTED |`, 'Fired when content should be paused. This usually happens right before an ad is about to cover the content.');
                this.player.pause();
                this.handleEventListeners('CONTENT_PAUSE_REQUESTED');
                break;

            case google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] CONTENT_RESUME_REQUESTED |`, 'Fired when content should be resumed. This usually happens when an ad finishes or collapses.');
                this.handleEventListeners('CONTENT_RESUME_REQUESTED');
                break;
            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.
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] LOADED |`, event.getAd().getContentType());
                this.handleEventListeners('LOADED');

                // Show the ad display element.
                this.adsDisplayElement.style.display = 'block';

                if (!ad.isLinear()) {
                    // Position AdDisplayContainer correctly for overlay.
                    ad.width = container.offsetWidth;
                    ad.height = container.offsetHeight;
                }

                // console.info('Ad type: ' + event.getAd().getAdPodInfo().getPodIndex());
                // console.info('Ad time: ' + event.getAd().getAdPodInfo().getTimeOffset());
                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.
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] STARTED |`, 'Fired when the ad starts playing.');
                this.player.pause();
                this.playing = true;
                this.handleEventListeners('STARTED');
                break;
            case google.ima.AdEvent.Type.DURATION_CHANGE:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] DURATION_CHANGE |`, 'Fired when the ad\'s duration changes.');
                break;
            case google.ima.AdEvent.Type.FIRST_QUARTILE:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] FIRST_QUARTILE |`, 'Fired when the ad playhead crosses first quartile.');
                break;
            case google.ima.AdEvent.Type.IMPRESSION:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] IMPRESSION |`, 'Fired when the impression URL has been pinged.');
                break;
            case google.ima.AdEvent.Type.INTERACTION:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] INTERACTION |`, 'Fired when an ad triggers the interaction callback. Ad interactions contain an interaction ID string in the ad data.');
                break;
            case google.ima.AdEvent.Type.LINEAR_CHANGED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] LINEAR_CHANGED |`, 'Fired when the displayed ad changes from linear to nonlinear, or vice versa.');
                break;
            case google.ima.AdEvent.Type.MIDPOINT:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] MIDPOINT |`, 'Fired when the ad playhead crosses midpoint.');
                break;
            case google.ima.AdEvent.Type.PAUSED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] PAUSED |`, 'Fired when the ad is paused.');
                break;
            case google.ima.AdEvent.Type.RESUMED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] RESUMED |`, 'Fired when the ad is resumed.');
                break;
            case google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] SKIPPABLE_STATE_CHANGED |`, 'Fired when the displayed ads skippable state is changed.');
                break;
            case google.ima.AdEvent.Type.SKIPPED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] SKIPPED |`, 'Fired when the ad is skipped by the user.');
                break;
            case google.ima.AdEvent.Type.THIRD_QUARTILE:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] THIRD_QUARTILE |`, 'Fired when the ad playhead crosses third quartile.');
                break;
            case google.ima.AdEvent.Type.USER_CLOSE:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] USER_CLOSE |`, 'Fired when the ad is closed by the user.');
                break;
            case google.ima.AdEvent.Type.VOLUME_CHANGED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] VOLUME_CHANGED |`, 'Fired when the ad volume has changed.');
                break;
            case google.ima.AdEvent.Type.VOLUME_MUTED:
                this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK] VOLUME_MUTED |`, 'Fired when the ad volume has been muted.');
                break;

            default:
                break;
        }
    }

    onAdError(adErrorEvent) {
        this.cancel();

        if (this.player.debug) {
            throw new Error(adErrorEvent);
        }
    }

    /**
     * Destroy the adsManager so we can grab new ads after this.
     * If we don't then we're not allowed to call new ads based
     * on google policies, as they interpret this as an accidental
     * video requests. https://developers.google.com/interactive-
     * media-ads/docs/sdks/android/faq#8
     */
    cancel() {
        this.player.debug.warn(`[${(Date.now() - this.time) / 1000}s][IMA SDK]`, 'Advertisement cancelled.');

        // Todo: Removing the ad container might be problematic if we were to recreate the adsManager. Think of playlists. Every new video you need to request a new VAST xml and preload the advertisement.
        this.adsDisplayElement.remove();

        // Tell our adsManager to go bye bye.
        this.adsManagerPromise.then(() => {
            if (this.adsManager) {
                this.adsManager.destroy();
            }
        });
    }

    setupAdDisplayContainer() {
        const { container } = this.player.elements;

        // So we can run VPAID2.
        google.ima.settings.setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.ENABLED);

        // Set language.
        // Todo: Could make a config option out of this locale value.
        google.ima.settings.setLocale('en');

        // We assume the adContainer is the video container of the plyr element
        // that will house the ads.
        this.adDisplayContainer = new google.ima.AdDisplayContainer(container);

        this.adsDisplayElement = container.firstChild;

        // The AdDisplayContainer call from google IMA sets the style attribute
        // by default. We remove the inline style and set it through the stylesheet.
        this.adsDisplayElement.removeAttribute('style');

        // Set class name on the adDisplayContainer element.
        this.adsDisplayElement.setAttribute('class', this.player.config.classNames.ads);

        // Play ads when clicked. Wait until the adsManager and adsLoader
        // are both resolved.
        Promise.all([
            this.adsManagerPromise,
            this.adsLoaderPromise,
        ]).then(() => {
            this.setOnClickHandler(this.adsDisplayElement, this.playAds);
        });
    }

    playAds() {
        const { container } = this.player.elements;

        // Play the requested advertisement whenever the adsManager is ready.
        this.adsManagerPromise.then(() => {
            // Initialize the container. Must be done via a user action on mobile devices.
            this.adDisplayContainer.initialize();

            try {
                // Initialize the ads manager. Ad rules playlist will start at this time.
                this.adsManager.init(container.offsetWidth, container.offsetHeight, 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.
                this.adsManager.start();
            } catch (adError) {
                // An error may be thrown if there was a problem with the VAST response.
                this.player.play();
                this.adsDisplayElement.remove();

                if (this.player.debug) {
                    throw new Error(adError);
                }
            }
        });
    }

    isPlaying() {
        return this.playing;
    }

    /**
     * Setup hooks for Plyr and window events. This ensures
     * the mid- and post-roll launch at the correct time. And
     * resize the advertisement when the player resizes.
     */
    setupListeners() {
        const { container } = this.player.elements;
        let time;

        // Add listeners to the required events.
        this.player.on('ended', () => {
            this.adsLoader.contentComplete();
        });

        this.player.on('seeking', event => {
            time = this.player.currentTime;
            return time;
        });

        this.player.on('seeked', event => {
            const seekedTime = this.player.currentTime;

            this.adsCuePoints.forEach((cuePoint, index) => {
                if (time < cuePoint && cuePoint < seekedTime) {
                    this.adsManager.discardAdBreak();
                    this.adsCuePoints.splice(index, 1);
                }
            });
        });

        // Listen to the resizing of the window. And resize ad accordingly.
        window.addEventListener('resize', () => {
            this.adsManager.resize(container.offsetWidth, container.offsetHeight, google.ima.ViewMode.NORMAL);
        });
    }

    /**
     * Handles callbacks after an ad event was invoked.
     */
    handleEventListeners(event) {
        if (typeof this.events[event] !== 'undefined') {
            this.events[event].call(this);
        }
    }

    /**
     * Set start event listener on a DOM element and triggers the
     * callback when clicked.
     * @param {element} element - The element on which to set the listener
     * @param {function} callback - The callback which will be invoked once triggered.
     */
    setOnClickHandler(element, callback) {
        for (let i = 0; i < this.startEvents.length; i += 1) {
            const startEvent = this.startEvents[i];
            element.addEventListener(
                startEvent,
                event => {
                    if ((event.type === 'touchend' && startEvent === 'touchend') || event.type === 'click') {
                        callback.call(this);
                    }
                },
                { once: true },
            );
        }
    }

    /**
     * Add event listeners
     * @param {string} event - Event type
     * @param {function} callback - Callback for when event occurs
     */
    on(event, callback) {
        this.events[event] = callback;
        return this;
    }

    /**
     * startSafetyTimer
     * Setup a safety timer for when the ad network
     * doesn't respond for whatever reason. The advertisement has 12 seconds
     * to get its shit together. We stop this timer when the advertisement
     * is playing, or when a user action is required to start, then we
     * clear the timer on ad ready.
     * @param {Number} time
     * @param {String} from
     * @private
     */
    startSafetyTimer(time, from) {
        this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK]`, `Safety timer invoked timer from: ${from}`);
        this.safetyTimer = window.setTimeout(() => {
            this.cancel();
            this.clearSafetyTimer('startSafetyTimer()');
        }, time);
    }

    /**
     * clearSafetyTimer
     * @param {String} from
     * @private
     */
    clearSafetyTimer(from) {
        if (typeof this.safetyTimer !== 'undefined' && this.safetyTimer !== null) {
            this.player.debug.log(`[${(Date.now() - this.time) / 1000}s][IMA SDK]`, `Safety timer cleared timer from: ${from}`);
            clearTimeout(this.safetyTimer);
            this.safetyTimer = undefined;
        }
    }
}