-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathinitialization.ts
More file actions
389 lines (349 loc) · 13.8 KB
/
initialization.ts
File metadata and controls
389 lines (349 loc) · 13.8 KB
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
import { type BrowserPlugin } from '@snowplow/browser-tracker-core';
import {
MediaEventType,
SnowplowMediaPlugin,
endMediaTracking,
startMediaTracking,
trackMediaBufferEnd,
trackMediaBufferStart,
trackMediaEnd,
trackMediaError,
trackMediaPause,
trackMediaPlay,
trackMediaPlaybackRateChange,
trackMediaQualityChange,
trackMediaReady,
trackMediaSeekEnd,
trackMediaSeekStart,
trackMediaSelfDescribingEvent,
trackMediaVolumeChange,
updateMediaTracking,
} from '@snowplow/browser-plugin-media';
import type { Logger } from '@snowplow/tracker-core';
import { v4 as uuid } from 'uuid';
import { buildPlayerEntity, buildYouTubeEntity } from './entities';
import { addUrlParam, parseUrlParams } from './helperFunctions';
import { trackingOptionsParser } from './options';
import type {
LegacyYouTubeMediaTrackingConfiguration,
TrackedPlayer,
TrackingOptions,
YouTubeMediaTrackingConfiguration,
} from './types';
const YouTubeIFrameAPIURL = 'https://www.youtube.com/iframe_api';
const trackedPlayers: Record<string, TrackedPlayer> = {};
const trackingQueue: Array<TrackingOptions> = [];
const legacyApiSessions: string[] = [];
let LOG: Logger | undefined = undefined;
/**
* Create a YouTubeTrackingPlugin instance.
* @returns The created YouTubeTrackingPlugin instance
*/
export function YouTubeTrackingPlugin(): BrowserPlugin {
return {
...SnowplowMediaPlugin(),
logger: (logger: Logger) => {
LOG = logger;
},
};
}
/**
* Start media tracking for the given player and configuration.
* @param args Tracking configuration
* @returns The media session ID for the player
* @since 4.0.0
*/
export function startYouTubeTracking(args: YouTubeMediaTrackingConfiguration): string | void {
try {
const conf = trackingOptionsParser(args);
addPlayer(conf);
startMediaTracking({
...conf.config,
id: conf.sessionId,
captureEvents: conf.captureEvents,
boundaries: conf.boundaries,
pings: conf.config.pings ?? conf.captureEvents.indexOf(MediaEventType.Ping) !== -1,
context: (args.context ?? []).concat([() => buildYouTubeEntity(conf)]),
timestamp: args.timestamp,
});
return conf.sessionId;
} catch (e) {
if (e instanceof Error) {
LOG?.error(e.message);
}
}
}
/**
* Disable tracking for a previously enabled player's media session.
* @param id The media session ID to disable tracking for
* @since 4.0.0
*/
export function endYouTubeTracking(id: string): void {
endMediaTracking({ id });
const intervalId = trackedPlayers[id].pollTracking.interval;
if (intervalId !== null) clearInterval(intervalId);
delete trackedPlayers[id];
}
/**
* Start media tracking for the given player and configuration.
* This is a legacy v3 API that wraps the newer `startYouTubeTracking` API.
* @param args Tracking configuration
* @returns The media session ID for the player
* @deprecated since v4.0.0; use {@link startYouTubeTracking}
*/
export function enableYouTubeTracking(args: LegacyYouTubeMediaTrackingConfiguration): string | void {
const { id, options, ...rest } = args;
const migrated: YouTubeMediaTrackingConfiguration = {
id: uuid(),
...rest,
...options,
video: id,
};
const sessionId = startYouTubeTracking(migrated);
if (sessionId) {
legacyApiSessions.push(sessionId);
return sessionId;
}
}
/**
* Disable tracking for a previously enabled player's media session.
* This is a legacy v3 API that wraps the newer `endYouTubeTracking` API.
* @param id The media session ID to disable tracking for, or the oldest known session ID if not provided
* @since 4.0.0
* @deprecated since v4.0.0; use {@link endYouTubeTracking}
*/
export function disableYouTubeTracking(id?: string): void {
if (id && legacyApiSessions.indexOf(id) !== -1) {
// id provided, remove from active list
legacyApiSessions.splice(legacyApiSessions.indexOf(id), 1);
} else {
// id was not provided; presume we're ending the earliest session we know of
id = id ?? legacyApiSessions.shift();
}
if (id) {
endYouTubeTracking(id);
}
}
/**
* Track a custom Self Describing Event with the Media plugin's entities included, like the events generated by the plugin directly.
* @param event The tracking options, including the custom Self Describing Event to track and optional media configuration such as `ad` data.
*/
export function trackYouTubeSelfDescribingEvent(
event: Omit<Parameters<typeof trackMediaSelfDescribingEvent>[0], 'player'>
) {
const config = trackedPlayers[event.id];
if (config) {
const yt = buildYouTubeEntity(config.conf);
trackMediaSelfDescribingEvent({
...event,
player: buildPlayerEntity(config.conf),
context: (event.context ?? []).concat(yt ?? []),
});
}
}
function addPlayer(conf: TrackingOptions): void {
const player = conf.video;
if (typeof player === 'string') {
// First check if the API is already active and a player exists with this name
// Creating an additional player will overwrite the existing one and fail
if (typeof YT !== 'undefined' && 'get' in YT) {
const existing: YT.Player | undefined = (YT as any).get(player);
if (existing) {
conf.video = existing;
return addPlayer(conf);
}
}
// May have meant a DOM ID?
const el = document.getElementById(player) as HTMLIFrameElement | null;
if (!el || el.tagName.toUpperCase() !== 'IFRAME') {
// Technically the IFrame API will replace non-iframe elements with an iframe
// In that case we expect the iframe/player to be passed directly instead
throw Error('Cannot find YouTube iframe or player');
}
conf.video = el;
return addPlayer(conf);
} else if ('tagName' in player) {
if (player.tagName.toUpperCase() !== 'IFRAME') throw Error('Only existing YouTube iframe elements are supported');
conf.urlParameters = parseUrlParams(ensureYouTubeIframeAPI(player));
// put them into a queue that will have listeners added once the API is ready
// and start trying to load the iframe API
trackingQueue.push(conf);
installYouTubeIframeAPI();
} else {
conf.urlParameters = parseUrlParams(player.getIframe().src);
conf.player = player;
initializePlayer(conf);
}
}
function ensureYouTubeIframeAPI(el: HTMLIFrameElement) {
// The 'enablejsapi' parameter is required to be '1' for the API to be able to communicate with the player
// It can be a URL parameter or an attribute of the iframe
// We only enable it if it hasn't been explicitly disabled/set
if (el.src.indexOf('enablejsapi') === -1 && el.getAttribute('enablejsapi') == null) {
let apiEnabled = addUrlParam(el.src, 'enablejsapi', '1');
// `origin` is recommended when using `enablejsapi`
if (apiEnabled.indexOf('origin=') === -1 && location.origin) {
apiEnabled = addUrlParam(apiEnabled, 'origin', location.origin);
}
el.src = apiEnabled;
}
return el.src;
}
function installYouTubeIframeAPI(iframeAPIRetryWaitMs: number = 100) {
// Once the API is ready to use, 'YT.Player' will be defined
// 'YT.Player' is not available immediately after 'YT' is defined,
// so we need to wait until 'YT' is defined to then check 'YT.Player'
if (typeof YT === 'undefined' || typeof YT.Player === 'undefined') {
// First we check if the script tag exists in the DOM, and enable the API if not
const scriptTags = Array.prototype.slice.call(document.getElementsByTagName('script'));
if (!scriptTags.some((s) => s.src === YouTubeIFrameAPIURL)) {
// Load the Iframe API
// https://developers.google.com/youtube/iframe_api_reference
const tag: HTMLScriptElement = document.createElement('script');
tag.src = YouTubeIFrameAPIURL;
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode!.insertBefore(tag, firstScriptTag);
}
// The iframe API usually loads a second script, so `onload` is not reliable
// We prefer not to pollute global scope so won't use onYouTubeIframeAPIReady either
// Poll the API to see when it exists
if (iframeAPIRetryWaitMs <= 6400) {
setTimeout(installYouTubeIframeAPI, iframeAPIRetryWaitMs, iframeAPIRetryWaitMs * 2);
} else {
LOG?.error('YouTube iframe API failed to load');
}
} else {
// Once the API is available, listeners are attached to anything sitting in the queue
while (trackingQueue.length) {
initializePlayer(trackingQueue.pop()!);
}
}
}
/**
* Adds a player to the list of tracked players, and sets up the `onReady` callback to attach listeners once the player is ready
*
* @param conf - The configuration for the player
*/
function initializePlayer(conf: TrackingOptions) {
const attach: YT.PlayerEventHandler<YT.PlayerEvent> = ({ target }) => {
conf.player = target;
trackedPlayers[conf.sessionId] = {
player: target,
conf,
// initial state for polling listeners
pollTracking: {
interval: null,
prevTime: target.getCurrentTime() || 0,
prevVolume: target.getVolume() || 0,
},
};
attachListeners(conf);
};
if (!conf.player) {
// Player API not yet initialized
if (typeof conf.video !== 'string' && 'tagName' in conf.video) {
conf.player = new YT.Player(conf.video, {
events: {
onReady: attach,
},
});
} else throw Error('Could not access YouTube Player API');
} else if (typeof conf.player.getCurrentTime === 'function') {
// Player API initialized and already "ready"
attach({ target: conf.player });
} else {
// Player API initialized but not yet "ready"
conf.player.addEventListener('onReady', attach);
}
}
function attachListeners(conf: TrackingOptions) {
const id = conf.sessionId,
player = conf.player!;
let buffering = false;
// object lookup should be faster than captureEvents.indexOf every time
const flags = conf.captureEvents.reduce((a: Partial<Record<MediaEventType, true>>, e) => ((a[e] = true), a), {});
const builtInEvents: Record<
keyof YT.Events,
| YT.PlayerEventHandler<YT.PlayerEvent>
| YT.PlayerEventHandler<YT.OnStateChangeEvent>
| YT.PlayerEventHandler<YT.OnErrorEvent>
| YT.PlayerEventHandler<YT.OnPlaybackQualityChangeEvent>
> = {
onStateChange: (e: YT.OnStateChangeEvent) => {
if (buffering && e.data !== YT.PlayerState.BUFFERING && flags[MediaEventType.BufferEnd]) {
trackMediaBufferEnd({ id });
buffering = false;
}
switch (e.data) {
case YT.PlayerState.CUED:
return;
case YT.PlayerState.UNSTARTED:
return flags[MediaEventType.Ready] && trackMediaReady({ id });
case YT.PlayerState.BUFFERING:
buffering = true;
return flags[MediaEventType.BufferEnd] && trackMediaBufferStart({ id }); // not a typo
case YT.PlayerState.PLAYING:
return flags[MediaEventType.Play] && trackMediaPlay({ id });
case YT.PlayerState.PAUSED:
return flags[MediaEventType.Pause] && trackMediaPause({ id });
case YT.PlayerState.ENDED:
return flags[MediaEventType.End] && trackMediaEnd({ id });
}
},
onPlaybackQualityChange: (e: YT.OnPlaybackQualityChangeEvent) =>
flags[MediaEventType.QualityChange] && trackMediaQualityChange({ id, newQuality: e.data }),
onApiChange: () => {},
onError: (e: YT.OnErrorEvent) =>
flags[MediaEventType.Error] &&
trackMediaError({ id, errorCode: String(e.data), errorName: YT.PlayerError[e.data] }),
onPlaybackRateChange: () =>
flags[MediaEventType.PlaybackRateChange] &&
trackMediaPlaybackRateChange({ id, newRate: player.getPlaybackRate() }),
onReady: () => {},
};
// The 'ready' event is required for modelling purposes, the Out-The-Box YouTube modelling won't work without it
// Ensure you have 'ready' in your 'captureEvents' array if you are using the Out-The-Box YouTube modelling
if (flags[MediaEventType.Ready]) {
// We need to manually trigger the 'ready' event, as it is already in that state and won't call the event listener
// do it in a new task because we probably haven't called startMediaTracking yet
setTimeout(() => trackMediaReady({ id: conf.sessionId, player: buildPlayerEntity(conf) }), 0);
}
conf.youtubeEvents.forEach((youtubeEventName) => {
player.addEventListener(youtubeEventName, (e) => {
updateMediaTracking({ id, player: buildPlayerEntity(conf) });
builtInEvents[youtubeEventName](e as any);
});
});
// there are actually `onVideoProgress` and `onVolumeChange` events but they
// are not documented so we will poll
installPollingListeners(conf, flags);
}
function installPollingListeners(conf: TrackingOptions, flags: Partial<Record<MediaEventType, true>>) {
const pollState = trackedPlayers[conf.sessionId].pollTracking;
const player = conf.player;
const id = conf.sessionId;
const enableSeek = flags[MediaEventType.SeekStart] || flags[MediaEventType.SeekEnd];
const enableVolume = flags[MediaEventType.VolumeChange];
if (!pollState.interval && player) {
pollState.interval = setInterval(() => {
updateMediaTracking({ id, player: buildPlayerEntity(conf) });
// Seek Tracking
if (enableSeek) {
const playerTime = player.getCurrentTime();
if (Math.abs(playerTime - (pollState.prevTime + conf.updateRate / 1000)) > 1) {
trackMediaSeekStart({ id, player: { currentTime: pollState.prevTime } });
trackMediaSeekEnd({ id, player: { currentTime: playerTime } });
}
pollState.prevTime = playerTime;
}
// Volume Tracking
if (enableVolume) {
const playerVolume = player.getVolume();
if (playerVolume !== pollState.prevVolume) {
trackMediaVolumeChange({ id, previousVolume: pollState.prevVolume, newVolume: playerVolume });
}
pollState.prevVolume = playerVolume;
}
}, conf.updateRate);
}
}