forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
322 lines (285 loc) · 12.1 KB
/
Copy pathindex.ts
File metadata and controls
322 lines (285 loc) · 12.1 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
/*
* Copyright (c) 2022 Snowplow Analytics Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import { addUrlParam, parseUrlParams } from './helperFunctions';
import {
CaptureEventToYouTubeEvent,
YouTubeIFrameAPIURL,
YTError,
YTPlayerEvent,
YTState,
YTStateEvent,
} from './constants';
import { EventData, EventGroup, MediaTrackingOptions, TrackedPlayer, TrackingOptions } from './types';
import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core';
import { buildSelfDescribingEvent, CommonEventProperties, Logger, SelfDescribingJson } from '@snowplow/tracker-core';
import { SnowplowEvent } from './snowplowEvents';
import { MediaPlayerEvent } from './contexts';
import { buildYouTubeEvent } from './buildYouTubeEvent';
import { YTEvent } from './youtubeEvents';
import { DefaultEvents, EventGroups, AllEvents } from './eventGroups';
const _trackers: Record<string, BrowserTracker> = {};
const trackedPlayers: Record<string, TrackedPlayer> = {};
const trackingQueue: Array<TrackingOptions> = [];
let LOG: Logger;
export function YouTubeTrackingPlugin(): BrowserPlugin {
return {
activateBrowserPlugin: (tracker: BrowserTracker) => {
_trackers[tracker.id] = tracker;
},
logger: (logger) => {
LOG = logger;
},
};
}
function trackEvent(
event: SelfDescribingJson<MediaPlayerEvent> & CommonEventProperties,
trackers: Array<string> = Object.keys(_trackers)
): void {
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
t.core.track(buildSelfDescribingEvent({ event }), event.context, event.timestamp);
});
}
export function trackingOptionsParser(mediaId: string, conf?: MediaTrackingOptions): TrackingOptions {
const defaults: TrackingOptions = {
mediaId: mediaId,
captureEvents: DefaultEvents,
youtubeEvents: [
YTPlayerEvent.ONSTATECHANGE,
YTPlayerEvent.ONPLAYBACKQUALITYCHANGE,
YTPlayerEvent.ONERROR,
YTPlayerEvent.ONPLAYBACKRATECHANGE,
],
updateRate: 500,
progress: {
boundaries: [10, 25, 50, 75],
boundaryTimeoutIds: [],
},
};
if (!conf) return defaults;
if (conf.label) defaults.label = conf.label;
defaults.updateRate = conf.updateRate || defaults.updateRate;
defaults.captureEvents = conf.captureEvents || defaults.captureEvents;
let parsedEvents: EventGroup = [];
for (let ev of defaults.captureEvents) {
// If an event is an EventGroup, get the events from that group
if (EventGroups.hasOwnProperty(ev)) {
parsedEvents = parsedEvents.concat(EventGroups[ev]);
} else if (!Object.keys(AllEvents).filter((k) => k === ev)) {
LOG.warn(`'${ev}' is not a valid event.`);
} else {
parsedEvents.push(ev);
}
}
defaults.captureEvents = parsedEvents;
for (let ev of defaults.captureEvents) {
if (
CaptureEventToYouTubeEvent.hasOwnProperty(ev) &&
defaults.youtubeEvents.indexOf(CaptureEventToYouTubeEvent[ev]) === -1
) {
defaults.youtubeEvents.push(CaptureEventToYouTubeEvent[ev]);
}
}
if (defaults.captureEvents.indexOf(SnowplowEvent.PERCENTPROGRESS) !== -1) {
defaults.progress = {
boundaries: conf.boundaries || defaults.progress!.boundaries,
boundaryTimeoutIds: [],
};
}
return defaults;
}
export function enableYouTubeTracking(args: { id: string; options?: MediaTrackingOptions }) {
const conf: TrackingOptions = trackingOptionsParser(args.id, args.options);
const el: HTMLIFrameElement = document.getElementById(args.id) as HTMLIFrameElement;
if (!el) {
LOG.error('Cannot find YouTube iframe');
return;
}
// The 'enablejsapi' parameter is required to be '1' for the API to be able to communicate with the player
if (el.src.indexOf('enablejsapi') === -1) {
el.src = addUrlParam(el.src, 'enablejsapi', '1');
}
conf.urlParameters = parseUrlParams(el.src);
// If the API is ready, we can immediately add the listeners
if (typeof YT !== 'undefined' && typeof YT.Player !== 'undefined') {
initialisePlayer(conf);
} else {
// If not, we 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);
handleYouTubeIframeAPI(conf);
}
}
let iframeAPIRetryWait = 100;
function handleYouTubeIframeAPI(conf: TrackingOptions) {
// 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);
}
// 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') {
if (iframeAPIRetryWait <= 6400) {
setTimeout(() => {
handleYouTubeIframeAPI(conf);
}, iframeAPIRetryWait);
iframeAPIRetryWait *= 2;
} else {
LOG.error('YouTube iframe API failed to load.');
}
} else {
// Once the API is avaliable, listeners are attached to anything sitting in the queue
while (trackingQueue.length) {
initialisePlayer(trackingQueue.pop()!);
}
}
}
function initialisePlayer(conf: TrackingOptions) {
trackedPlayers[conf.mediaId] = {
player: new YT.Player(conf.mediaId, { events: { onReady: (e: YT.PlayerEvent) => playerReady(e, conf) } }),
conf: conf,
seekTracking: {
prevTime: 0,
enabled: false,
},
volumeTracking: {
prevVolume: 0,
enabled: false,
},
};
}
function playerReady(event: YT.PlayerEvent, conf: TrackingOptions) {
const player: YT.Player = event.target;
const builtInEvents: Record<string, Function> = {
[YTPlayerEvent.ONSTATECHANGE]: (e: YT.OnStateChangeEvent) => {
if (conf.captureEvents.indexOf(YTStateEvent[e.data.toString()]) !== -1) {
youtubeEvent(trackedPlayers[conf.mediaId].player, YTStateEvent[e.data], conf);
}
},
[YTPlayerEvent.ONPLAYBACKQUALITYCHANGE]: () =>
youtubeEvent(trackedPlayers[conf.mediaId].player, YTEvent.PLAYBACKQUALITYCHANGE, conf),
[YTPlayerEvent.ONAPICHANGE]: () => youtubeEvent(trackedPlayers[conf.mediaId].player, YTEvent.APICHANGE, conf),
[YTPlayerEvent.ONERROR]: (e: YT.OnErrorEvent) =>
youtubeEvent(trackedPlayers[conf.mediaId].player, YTEvent.ERROR, conf, { error: YTError[e.data] }),
[YTPlayerEvent.ONPLAYBACKRATECHANGE]: () =>
youtubeEvent(trackedPlayers[conf.mediaId].player, YTEvent.PLAYBACKRATECHANGE, conf),
};
conf.youtubeEvents.forEach((youtubeEventName) => {
player.addEventListener(youtubeEventName, (eventData: YT.PlayerEvent) =>
builtInEvents[youtubeEventName](eventData)
);
});
if (conf.captureEvents.indexOf(YTEvent.READY) !== -1) {
youtubeEvent(player, YTEvent.READY, conf);
}
}
function youtubeEvent(player: YT.Player, eventName: string, conf: TrackingOptions, eventData?: EventData) {
const playerInstance = trackedPlayers[conf.mediaId];
if (!playerInstance.seekTracking.enabled && conf.captureEvents.indexOf(SnowplowEvent.SEEK) !== -1) {
enableSeekTracking(player, conf, eventData);
}
if (!playerInstance.volumeTracking.enabled && conf.captureEvents.indexOf(SnowplowEvent.VOLUMECHANGE) !== -1) {
enableVolumeTracking(player, conf, eventData);
}
if (conf.captureEvents.indexOf(SnowplowEvent.PERCENTPROGRESS) !== -1) {
progressHandler(player, eventName, conf);
}
const event = buildYouTubeEvent(player, eventName, conf, eventData);
trackEvent(event);
}
// Progress Tracking
function progressHandler(player: YT.Player, eventName: string, conf: TrackingOptions) {
const timeoutIds = conf.progress!.boundaryTimeoutIds;
if (eventName === YTState.PAUSED) {
timeoutIds.forEach((id) => clearTimeout(id));
timeoutIds.length = 0;
}
if (eventName === YTState.PLAYING) {
setPercentageBoundTimeouts(player, conf);
}
}
function setPercentageBoundTimeouts(player: YT.Player, conf: TrackingOptions) {
conf.progress?.boundaries!.forEach((boundary) => {
const absoluteBoundaryTimeMs = player.getDuration() * (boundary / 100) * 1000;
const timeUntilBoundaryEvent = absoluteBoundaryTimeMs - player.getCurrentTime() * 1000;
if (0 < timeUntilBoundaryEvent) {
conf.progress!.boundaryTimeoutIds.push(
setTimeout(
() => waitAnyRemainingTimeAfterTimeout(player, conf, absoluteBoundaryTimeMs, boundary),
timeUntilBoundaryEvent
)
);
}
});
}
// The timeout in setPercentageBoundTimeouts fires ~100 - 300ms early
// waitAnyRemainingTimeAfterTimeout ensures the event is fired accurately
function waitAnyRemainingTimeAfterTimeout(player: YT.Player, conf: TrackingOptions, percentTime: number, p: number) {
if (player.getCurrentTime() * 1000 < percentTime) {
setTimeout(() => waitAnyRemainingTimeAfterTimeout(player, conf, percentTime, p), 10);
} else {
youtubeEvent(player, SnowplowEvent.PERCENTPROGRESS, conf, {
percentThrough: p,
});
}
}
// Seek Tracking
function enableSeekTracking(player: YT.Player, conf: TrackingOptions, eventData?: EventData) {
trackedPlayers[conf.mediaId].seekTracking.enabled = true;
setInterval(() => seekEventTracker(player, conf, eventData), conf.updateRate);
}
function seekEventTracker(player: YT.Player, conf: TrackingOptions, eventData?: EventData) {
const playerInstance = trackedPlayers[conf.mediaId];
const playerTime = player.getCurrentTime();
if (Math.abs(playerTime - (playerInstance.seekTracking.prevTime + 0.5)) > 1) {
youtubeEvent(player, SnowplowEvent.SEEK, conf, eventData);
}
playerInstance.seekTracking.prevTime = playerTime;
}
// Volume Tracking
function enableVolumeTracking(player: YT.Player, conf: TrackingOptions, eventData?: EventData) {
trackedPlayers[conf.mediaId].volumeTracking.enabled = true;
trackedPlayers[conf.mediaId].volumeTracking.prevVolume = player.getVolume();
setInterval(() => volumeEventTracker(player, conf, eventData), conf.updateRate);
}
function volumeEventTracker(player: YT.Player, conf: TrackingOptions, eventData?: EventData) {
const playerVolumeTracking = trackedPlayers[conf.mediaId].volumeTracking;
const playerVolume = player.getVolume();
if (playerVolume !== playerVolumeTracking.prevVolume) {
youtubeEvent(player, SnowplowEvent.VOLUMECHANGE, conf, eventData);
}
playerVolumeTracking.prevVolume = playerVolume;
}