forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionTracking.ts
More file actions
35 lines (31 loc) · 1.1 KB
/
Copy pathsessionTracking.ts
File metadata and controls
35 lines (31 loc) · 1.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
import { SelfDescribingJson } from '@snowplow/tracker-core';
import { buildMediaSessionEntity } from './core';
import { MediaSessionTrackingStats } from './sessionStats';
import { MediaAdBreak, MediaPlayer, MediaEventType } from './types';
/**
* Manages the media player session that is optionally added as a context entity
* in media tracking.
*/
export class MediaSessionTracking {
/// Same as the ID of the media tracking.
private id: string;
private startedAt: Date;
private pingInterval?: number;
private stats = new MediaSessionTrackingStats();
constructor(id: string, startedAt?: Date, pingInterval?: number) {
this.id = id;
this.pingInterval = pingInterval;
this.startedAt = startedAt ?? new Date();
}
update(eventType: MediaEventType | undefined, player: MediaPlayer, adBreak?: MediaAdBreak) {
this.stats.update(eventType, player, adBreak);
}
getContext(): SelfDescribingJson {
return buildMediaSessionEntity({
mediaSessionId: this.id,
startedAt: this.startedAt,
pingInterval: this.pingInterval,
...this.stats.toSessionContextEntity(),
});
}
}