Skip to content

Commit f46bbc6

Browse files
fix: send usage analytics only when media_id is loaded
1 parent 7ed6cad commit f46bbc6

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/usage.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { PlayerWith } from "@flowplayer/player";
1+
import type { PlayerWith, Player } from "@flowplayer/player";
2+
import flowplayer from "@flowplayer/player";
23

34
type SampleRate = 1.0 | 0.1 | 0.001;
45

56
const PACKAGE_NAME = "react-flowplayer";
6-
77
type Behavior = "flowplayer-component-mounted";
88

99
type UsageEventDetail = {
@@ -13,17 +13,34 @@ type UsageEventDetail = {
1313
};
1414

1515
type PlayerWithUsage = PlayerWith<{
16+
// internal API
1617
emit(event: "flowplayer:feature", detail: UsageEventDetail): void;
18+
opts: Player["opts"] & {
19+
metadata?: {
20+
media_id: string;
21+
stream_target_id: string;
22+
};
23+
};
1724
}>;
1825

1926
export function trackBehaviorUsage(
2027
player: PlayerWithUsage,
2128
behavior: Behavior,
2229
sample_rate: SampleRate = 1.0
2330
) {
24-
player.emit("flowplayer:feature", {
25-
feature_name: PACKAGE_NAME,
26-
behavior,
27-
sample_rate,
31+
let reportedOnce = false;
32+
player.on(flowplayer.events.SOURCE, () => {
33+
const hasMediaIdLike =
34+
typeof player.opts.metadata?.media_id === "string" ||
35+
typeof player.opts.metadata?.stream_target_id === "string";
36+
37+
if (!reportedOnce && hasMediaIdLike) {
38+
player.emit("flowplayer:feature", {
39+
feature_name: PACKAGE_NAME,
40+
behavior,
41+
sample_rate,
42+
});
43+
reportedOnce = true;
44+
}
2845
});
2946
}

0 commit comments

Comments
 (0)