Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/usage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PlayerWith } from "@flowplayer/player";
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 = {
Expand All @@ -13,17 +13,35 @@ type UsageEventDetail = {
};

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
) {
player.emit("flowplayer:feature", {
feature_name: PACKAGE_NAME,
behavior,
sample_rate,
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;
}
});
}
Loading