Skip to content

Commit

Permalink
feat: cdn analytics code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
olga-jwp committed Jan 12, 2024
1 parent 68d31c4 commit aeef40f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import useOttAnalytics from '#src/hooks/useOttAnalytics';
import { logDev, testId } from '#src/utils/common';
import { useConfigStore } from '#src/stores/ConfigStore';
import type { AdSchedule } from '#types/ad-schedule';
import { useAccountStore } from '#src/stores/AccountStore';
import { useProfileStore } from '#src/stores/ProfileStore';
import { attachAnalyticsParams } from '#src/utils/analytics';

type Props = {
Expand Down Expand Up @@ -60,21 +58,13 @@ const Player: React.FC<Props> = ({
const [libLoaded, setLibLoaded] = useState(!!window.jwplayer);
const startTimeRef = useRef(startTime);

const { config } = useConfigStore((s) => s);
const { user } = useAccountStore((s) => s);
const { profile } = useProfileStore();

const setPlayer = useOttAnalytics(item, feedId);

const { settings } = useConfigStore((s) => s);

const playerId = settings.playerId;
const playerLicenseKey = settings.playerLicenseKey;

const isJwIntegration = !!config?.integrations?.jwp;
const userId = user?.id;
const profileId = profile?.id;

const handleBeforePlay = useEventCallback(onBeforePlay);
const handlePlay = useEventCallback(onPlay);
const handlePause = useEventCallback(onPause);
Expand Down Expand Up @@ -174,7 +164,7 @@ const Player: React.FC<Props> = ({
playerRef.current = window.jwplayer(playerElementRef.current) as JWPlayer;

// Inject user_id and profile_id into the CDN analytics
attachAnalyticsParams(item, isJwIntegration, userId, profileId);
attachAnalyticsParams(item);

// Player options are untyped
const playerOptions: { [key: string]: unknown } = {
Expand Down Expand Up @@ -228,7 +218,7 @@ const Player: React.FC<Props> = ({
if (libLoaded) {
initializePlayer();
}
}, [libLoaded, item, detachEvents, attachEvents, playerId, setPlayer, autostart, adsData, playerLicenseKey, feedId, isJwIntegration, profileId, userId]);
}, [libLoaded, item, detachEvents, attachEvents, playerId, setPlayer, autostart, adsData, playerLicenseKey, feedId]);

useEffect(() => {
return () => {
Expand Down
13 changes: 12 additions & 1 deletion src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { useAccountStore } from '#src/stores/AccountStore';
import { useConfigStore } from '#src/stores/ConfigStore';
import { useProfileStore } from '#src/stores/ProfileStore';
import type { PlaylistItem, Source } from '#types/playlist';

export const attachAnalyticsParams = (item: PlaylistItem, isJwIntegration: boolean, userId?: string, profileId?: string) => {
export const attachAnalyticsParams = (item: PlaylistItem) => {
const { config } = useConfigStore.getState();
const { user } = useAccountStore.getState();
const { profile } = useProfileStore.getState();

const { sources, mediaid } = item;

const userId = user?.id;
const profileId = profile?.id;
const isJwIntegration = !!config?.integrations?.jwp;

return sources.map((source: Source) => {
const url = new URL(source.file);

Expand Down

0 comments on commit aeef40f

Please sign in to comment.