Skip to content

Commit

Permalink
Merge pull request #420 from jwplayer/OWA-52-add-user-profile-id-for-…
Browse files Browse the repository at this point in the history
…cdn-analytics

feat: add user_id and profile_id for CDN analytics
  • Loading branch information
olga-jwp authored Jan 18, 2024
2 parents 48bd95b + 63bc329 commit da6c07c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 { attachAnalyticsParams } from '#src/utils/analytics';

type Props = {
feedId?: string;
Expand Down Expand Up @@ -56,6 +57,7 @@ const Player: React.FC<Props> = ({
const loadingRef = useRef(false);
const [libLoaded, setLibLoaded] = useState(!!window.jwplayer);
const startTimeRef = useRef(startTime);

const setPlayer = useOttAnalytics(item, feedId);

const { settings } = useConfigStore((s) => s);
Expand Down Expand Up @@ -161,6 +163,9 @@ 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);

// Player options are untyped
const playerOptions: { [key: string]: unknown } = {
advertising: {
Expand Down
37 changes: 37 additions & 0 deletions src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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) => {
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);

const mediaId = mediaid.toLowerCase();
const sourceUrl = url.href.toLowerCase();

// Attach user_id and profile_id only for VOD and BCL SaaS Live Streams
const isVOD = sourceUrl === `https://cdn.jwplayer.com/manifests/${mediaId}.m3u8`;
const isBCL = sourceUrl === `https://content.jwplatform.com/live/broadcast/${mediaId}.m3u8`;

if ((isVOD || isBCL) && userId) {
url.searchParams.set('user_id', userId);

if (isJwIntegration && profileId) {
url.searchParams.set('profile_id', profileId);
}
}

source.file = url.toString();
});
};

0 comments on commit da6c07c

Please sign in to comment.