Skip to content

Commit 6ff2cd2

Browse files
chore: send analytics on Flowplayer component mounts
1 parent baa803f commit 6ff2cd2

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/flowplayer.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Config, ConfigWith } from "@flowplayer/player";
33

44
import React, { useEffect, forwardRef, useRef, useImperativeHandle } from "react";
55
import flowplayer from "@flowplayer/player";
6+
import { trackBehaviorUsage } from "./usage";
67

78
type KeyValue = Record<string, any>;
89

@@ -29,6 +30,8 @@ const Flowplayer = (props: Props, receivedRef: ForwardedRef<HTMLDivElement>) =>
2930
if (!ref.current) return;
3031
if (!token) return;
3132
const api = flowplayer(ref?.current, { token, ...opts });
33+
trackBehaviorUsage(api, "flowplayer-component-mounted");
34+
3235
return () => {
3336
api.destroy();
3437
if (ref?.current) ref.current.innerHTML = "";

src/usage.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Player, PlayerWith } from "@flowplayer/player";
2+
3+
type SampleRate = 1.0 | 0.1 | 0.001;
4+
5+
const PACKAGE_NAME = "react-flowplayer";
6+
7+
type Behavior = "flowplayer-component-mounted";
8+
9+
type UsageEventDetail = {
10+
feature_name: typeof PACKAGE_NAME;
11+
behavior: Behavior;
12+
sample_rate: SampleRate;
13+
};
14+
15+
type PlayerWithUsage = PlayerWith<{
16+
emit(event: "flowplayer:feature", detail: UsageEventDetail): void;
17+
}>;
18+
19+
// Adopted from https://github.com/flowplayer/spins/pull/37/files#diff-d534c1d78c3c3b042c010d7f84feb943c03ae27bc8d7e774e19ecd96c79fbdd5R3
20+
export function trackBehaviorUsage(
21+
player: PlayerWithUsage,
22+
behavior: Behavior,
23+
sample_rate: SampleRate = 1.0
24+
) {
25+
player.emit("flowplayer:feature", {
26+
feature_name: PACKAGE_NAME,
27+
behavior,
28+
sample_rate,
29+
});
30+
}

0 commit comments

Comments
 (0)