Skip to content

Commit b720c00

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

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/usage.ts

Lines changed: 24 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,35 @@ 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+
// analytics backed only handles events with media_id, so we need to ensure that it is set
33+
player.on(flowplayer.events.SOURCE, () => {
34+
const hasMediaIdLike =
35+
typeof player.opts.metadata?.media_id === "string" ||
36+
typeof player.opts.metadata?.stream_target_id === "string";
37+
38+
if (!reportedOnce && hasMediaIdLike) {
39+
player.emit("flowplayer:feature", {
40+
feature_name: PACKAGE_NAME,
41+
behavior,
42+
sample_rate,
43+
});
44+
reportedOnce = true;
45+
}
2846
});
2947
}

0 commit comments

Comments
 (0)