-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusage.ts
More file actions
47 lines (41 loc) · 1.22 KB
/
usage.ts
File metadata and controls
47 lines (41 loc) · 1.22 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
import type { PlayerWith, Player } from "@flowplayer/player";
import flowplayer from "@flowplayer/player";
type SampleRate = 1.0 | 0.1 | 0.001;
const PACKAGE_NAME = "react-flowplayer";
type Behavior = "flowplayer-component-mounted";
type UsageEventDetail = {
feature_name: typeof PACKAGE_NAME;
behavior: Behavior;
sample_rate: SampleRate;
};
type PlayerWithUsage = PlayerWith<{
// internal API
emit(event: "flowplayer:feature", detail: UsageEventDetail): void;
opts: Player["opts"] & {
metadata?: {
media_id: string;
stream_target_id: string;
};
};
}>;
export function trackBehaviorUsage(
player: PlayerWithUsage,
behavior: Behavior,
sample_rate: SampleRate = 1.0
) {
let reportedOnce = false;
// analytics backend only handles events with media_id, so we need to ensure that it is set
player.on(flowplayer.events.SOURCE, () => {
const hasMediaIdLike =
typeof player.opts.metadata?.media_id === "string" ||
typeof player.opts.metadata?.stream_target_id === "string";
if (!reportedOnce && hasMediaIdLike) {
player.emit("flowplayer:feature", {
feature_name: PACKAGE_NAME,
behavior,
sample_rate,
});
reportedOnce = true;
}
});
}