forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
723 lines (681 loc) · 22.1 KB
/
Copy pathapi.ts
File metadata and controls
723 lines (681 loc) · 22.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core';
import { buildSelfDescribingEvent, LOG, SelfDescribingJson } from '@snowplow/tracker-core';
import { MediaTracking } from './mediaTracking';
import { MediaPingInterval } from './pingInterval';
import { MediaSessionTracking } from './sessionTracking';
import {
CommonMediaEventProperties,
MediaEventType,
MediaAdBreakType,
MediaTrackArguments,
MediaTrackAdBreakArguments,
MediaTrackAdArguments,
MediaTrackingConfiguration,
MediaTrackPlaybackRateChangeArguments,
MediaEvent,
MediaTrackVolumeChangeArguments,
MediaTrackFullscreenChangeArguments,
MediaTrackPictureInPictureChangeArguments,
MediaTrackAdPercentProgressArguments,
MediaTrackQualityChangeArguments,
MediaTrackErrorArguments,
MediaTrackSelfDescribingEventArguments,
EventWithContext,
} from './types';
export { MediaAdBreakType as MediaPlayerAdBreakType };
const _trackers: Record<string, BrowserTracker> = {};
const _context: Record<string, SelfDescribingJson[]> = {};
/**
* Adds media tracking
*/
export function SnowplowMediaPlugin(): BrowserPlugin {
let trackerId: string;
return {
activateBrowserPlugin: (tracker) => {
trackerId = tracker.id;
_trackers[trackerId] = tracker;
_context[trackerId] = [];
},
contexts: () => {
return _context[trackerId] || [];
},
};
}
const activeMedias: { [key: string]: MediaTracking } = {};
/**
* Starts media tracking for a single media content tracked in a media player.
* The tracking instance is uniquely identified by a given ID.
* All subsequent media track calls will be processed within this media tracking if given the same ID.
*
* @param config Configuration for setting up media tracking
* @param trackers The tracker identifiers which ping events will be sent to
*/
export function startMediaTracking(
config: MediaTrackingConfiguration & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const pingInterval = typeof config.pings === 'boolean' ? undefined : config.pings?.pingInterval;
const maxPausedPings = typeof config.pings === 'boolean' ? undefined : config.pings?.maxPausedPings;
const pings =
config.pings === false || config.pings === undefined
? undefined
: new MediaPingInterval(pingInterval, maxPausedPings, () => {
track({ mediaEvent: { type: MediaEventType.Ping } }, { id: config.id }, trackers);
});
const sessionTracking: MediaSessionTracking | undefined =
config.session === false ? undefined : new MediaSessionTracking(config.id, config.session?.startedAt, pingInterval);
const mediaTracking = new MediaTracking(
config.id,
config.player,
sessionTracking,
pings,
config.boundaries,
config.captureEvents,
config.updatePageActivityWhilePlaying,
config.filterOutRepeatedEvents,
config.context
);
activeMedias[mediaTracking.id] = mediaTracking;
}
/**
* Ends media tracking with the given ID if previously started.
* Clears local state for the media tracking and sends any events waiting to be sent.
*
* @param configuration Configuration with the media tracking ID
*/
export function endMediaTracking(configuration: { id: string }) {
if (activeMedias[configuration.id]) {
activeMedias[configuration.id].flushAndStop();
delete activeMedias[configuration.id];
}
}
/**
* Updates stored attributes of the media tracking such as the current playback.
* Use this function to continually update the player attributes so that they
* can be sent in the background ping events.
*
* @param args The attributes for the media entities
* @param trackers The tracker identifiers which any resulting event will be sent to
*/
export function updateMediaTracking(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({}, args, trackers);
}
/**
* Tracks a media player ready event that is fired when the media tracking is successfully
* attached to the player and can track events.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaReady(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.Ready } }, args, trackers);
}
/**
* Tracks a media player play event sent when the player changes state to playing from
* previously being paused.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaPlay(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track(
{ mediaEvent: { type: MediaEventType.Play } },
{
...args,
player: {
...args.player,
paused: false,
},
},
trackers
);
}
/**
* Tracks a media player pause event sent when the user pauses the playback.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaPause(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track(
{ mediaEvent: { type: MediaEventType.Pause } },
{
...args,
player: {
...args.player,
paused: true,
},
},
trackers
);
}
/**
* Tracks a media player end event sent when playback stops when end of the media
* is reached or because no further data is available.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaEnd(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track(
{ mediaEvent: { type: MediaEventType.End } },
{
...args,
player: {
...args.player,
ended: true,
paused: true,
},
},
trackers
);
}
/**
* Tracks a media player seek start event sent when a seek operation begins.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaSeekStart(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.SeekStart } }, args, trackers);
}
/**
* Tracks a media player seek end event sent when a seek operation completes.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaSeekEnd(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.SeekEnd } }, args, trackers);
}
/**
* Tracks a media player playback rate change event sent when the playback rate has changed.
*
* If not passed here, the previous rate is taken from the last setting in media player.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaPlaybackRateChange(
args: MediaTrackArguments & MediaTrackPlaybackRateChangeArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { previousRate, newRate } = args;
track(
{
mediaEvent: {
type: MediaEventType.PlaybackRateChange,
eventBody: {
previousRate: previousRate ?? getMedia(args.id)?.player.playbackRate,
newRate,
},
},
},
{
...args,
player: { ...args.player, playbackRate: newRate },
},
trackers
);
}
/**
* Tracks a media player volume change event sent when the volume has changed.
*
* If not passed here, the previous volume is taken from the last setting in media player.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaVolumeChange(
args: MediaTrackArguments & MediaTrackVolumeChangeArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { previousVolume, newVolume } = args;
track(
{
mediaEvent: {
type: MediaEventType.VolumeChange,
eventBody: {
previousVolume: previousVolume ?? getMedia(args.id)?.player.volume,
newVolume,
},
},
},
{
...args,
player: { ...args.player, volume: newVolume },
},
trackers
);
}
/**
* Tracks a media player fullscreen change event fired immediately after
* the browser switches into or out of full-screen mode.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaFullscreenChange(
args: MediaTrackArguments & MediaTrackFullscreenChangeArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { fullscreen } = args;
track(
{
mediaEvent: {
type: MediaEventType.FullscreenChange,
eventBody: { fullscreen },
},
},
{
...args,
player: { ...args.player, fullscreen },
},
trackers
);
}
/**
* Tracks a media player picture-in-picture change event fired immediately
* after the browser switches into or out of picture-in-picture mode.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaPictureInPictureChange(
args: MediaTrackArguments & MediaTrackPictureInPictureChangeArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { pictureInPicture } = args;
track(
{
mediaEvent: {
type: MediaEventType.PictureInPictureChange,
eventBody: { pictureInPicture },
},
},
{
...args,
player: { ...args.player, pictureInPicture },
},
trackers
);
}
/**
* Tracks a media player ad break start event that signals the start of an ad break.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdBreakStart(
args: MediaTrackArguments & MediaTrackAdBreakArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.AdBreakStart } }, args, trackers);
}
/**
* Tracks a media player ad break end event that signals the end of an ad break.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdBreakEnd(
args: MediaTrackArguments & MediaTrackAdBreakArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.AdBreakEnd } }, args, trackers);
}
/**
* Tracks a media player ad start event that signals the start of an ad.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdStart(
args: MediaTrackArguments & MediaTrackAdBreakArguments & MediaTrackAdArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.AdStart } }, args, trackers);
}
/**
* Tracks a media player ad skip event fired when the user activated
* a skip control to skip the ad creative.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdSkip(
args: MediaTrackArguments &
MediaTrackAdPercentProgressArguments &
MediaTrackAdBreakArguments &
MediaTrackAdArguments &
CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
track(
{
mediaEvent: {
type: MediaEventType.AdSkip,
eventBody: { percentProgress },
},
},
args,
trackers
);
}
/**
* Tracks a media player ad first quartile played event fired when
* a quartile of ad is reached after continuous ad playback at normal speed.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdFirstQuartile(
args: MediaTrackArguments & MediaTrackAdBreakArguments & MediaTrackAdArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track(
{
mediaEvent: {
type: MediaEventType.AdFirstQuartile,
eventBody: { percentProgress: 25 },
},
},
args,
trackers
);
}
/**
* Tracks a media player ad midpoint played event fired when a midpoint of ad is
* reached after continuous ad playback at normal speed.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdMidpoint(
args: MediaTrackArguments & MediaTrackAdBreakArguments & MediaTrackAdArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track(
{
mediaEvent: {
type: MediaEventType.AdMidpoint,
eventBody: { percentProgress: 50 },
},
},
args,
trackers
);
}
/**
* Tracks media player ad third quartile played event fired when a quartile
* of ad is reached after continuous ad playback at normal speed.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdThirdQuartile(
args: MediaTrackArguments & MediaTrackAdBreakArguments & MediaTrackAdArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track(
{
mediaEvent: {
type: MediaEventType.AdThirdQuartile,
eventBody: { percentProgress: 75 },
},
},
args,
trackers
);
}
/**
* Tracks a media player ad complete event that signals the ad creative
* was played to the end at normal speed.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdComplete(
args: MediaTrackArguments & MediaTrackAdBreakArguments & MediaTrackAdArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.AdComplete } }, args, trackers);
}
/**
* Tracks a media player ad click event fired when the user clicked on the ad.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdClick(
args: MediaTrackArguments &
MediaTrackAdPercentProgressArguments &
MediaTrackAdBreakArguments &
CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
track(
{
mediaEvent: {
type: MediaEventType.AdClick,
eventBody: { percentProgress },
},
},
args,
trackers
);
}
/**
* Tracks a media player ad pause event fired when the user clicked the pause
* control and stopped the ad creative.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdPause(
args: MediaTrackArguments &
MediaTrackAdPercentProgressArguments &
MediaTrackAdBreakArguments &
CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
track(
{
mediaEvent: {
type: MediaEventType.AdPause,
eventBody: { percentProgress },
},
},
args,
trackers
);
}
/**
* Tracks a media player ad resume event fired when the user resumed playing the
* ad creative after it had been stopped or paused.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaAdResume(
args: MediaTrackArguments &
MediaTrackAdPercentProgressArguments &
MediaTrackAdBreakArguments &
MediaTrackAdArguments &
CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
track(
{
mediaEvent: {
type: MediaEventType.AdResume,
eventBody: { percentProgress: percentProgress },
},
},
args,
trackers
);
}
/**
* Tracks a media player buffering start event fired when the player goes
* into the buffering state and begins to buffer content.
*
* End of buffering can be tracked using `trackMediaBufferEnd` or `trackMediaPlay` or
* by updating the `currentTime` property.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaBufferStart(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.BufferStart } }, args, trackers);
}
/**
* Tracks a media player buffering end event fired when the the player
* finishes buffering content and resumes playback.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaBufferEnd(
args: MediaTrackArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
track({ mediaEvent: { type: MediaEventType.BufferEnd } }, args, trackers);
}
/**
* Tracks a media player quality change event tracked when the video
* playback quality changes automatically.
*
* If not passed here, the previous quality is taken from the last setting in media player.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaQualityChange(
args: MediaTrackArguments & MediaTrackQualityChangeArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { previousQuality, newQuality, bitrate, framesPerSecond, automatic, id } = args;
track(
{
mediaEvent: {
type: MediaEventType.QualityChange,
eventBody: {
previousQuality: previousQuality ?? getMedia(id)?.player.quality,
newQuality,
bitrate,
framesPerSecond,
automatic,
},
},
},
{
...args,
player: { ...args.player, quality: newQuality },
},
trackers
);
}
/**
* Tracks a media player error event tracked when the resource could not be loaded due to an error.
*
* @param args The attributes for the media player event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaError(
args: MediaTrackArguments & MediaTrackErrorArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { errorCode, errorName, errorDescription } = args;
track(
{
mediaEvent: {
type: MediaEventType.Error,
eventBody: { errorCode, errorName, errorDescription },
},
},
args,
trackers
);
}
/**
* Tracks a custom self-describing event within the context of the media tracking.
* It will attach the context entities managed by the media tracking to the event (e.g. player, session, ad).
*
* @param args The attributes for the event and entities
* @param trackers The tracker identifiers which the event will be sent to
*/
export function trackMediaSelfDescribingEvent(
args: MediaTrackSelfDescribingEventArguments &
MediaTrackArguments &
MediaTrackAdArguments &
MediaTrackAdBreakArguments &
CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { event } = args;
track({ customEvent: event }, args, trackers);
}
function getMedia(id: string): MediaTracking | undefined {
if (activeMedias[id] === undefined) {
LOG.error(`Media tracking ${id} not started.`);
return undefined;
}
return activeMedias[id];
}
function track(
event: { mediaEvent?: MediaEvent; customEvent?: SelfDescribingJson },
args: MediaTrackArguments & MediaTrackAdArguments & MediaTrackAdBreakArguments & CommonMediaEventProperties,
trackers: Array<string> = Object.keys(_trackers)
) {
const { context = [], timestamp, player, ad, adBreak, id } = args;
const { mediaEvent, customEvent } = event;
const mediaTracking = getMedia(id);
if (mediaTracking === undefined) {
return;
}
const trackEvent = (event: EventWithContext) => {
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
t.core.track(buildSelfDescribingEvent(event), (event.context ?? []).concat(context), timestamp);
});
};
mediaTracking.update(trackEvent, mediaEvent, customEvent, player, ad, adBreak);
// Update page activity in order to keep sending page pings if needed
if (mediaTracking.shouldUpdatePageActivity()) {
dispatchToTrackersInCollection(trackers, _trackers, (t) => {
t.updatePageActivity();
});
}
}