Skip to content

Commit

Permalink
feat: add user_id and profile_id for CDN analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
olga-jwp committed Jan 11, 2024
1 parent 0bacf37 commit 114150e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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/player';

type Props = {
feedId?: string;
Expand Down Expand Up @@ -70,7 +71,7 @@ const Player: React.FC<Props> = ({
const playerId = settings.playerId;
const playerLicenseKey = settings.playerLicenseKey;

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

Expand Down Expand Up @@ -172,22 +173,9 @@ const Player: React.FC<Props> = ({

playerRef.current = window.jwplayer(playerElementRef.current) as JWPlayer;

// Add user_id and profile_id for CDN analytics
const { sources } = item;

sources?.map((source) => {
const url = new URL(source.file);
const isVOD = url.pathname.includes('manifests');
const isBCL = url.pathname.includes('broadcast');
const isDRM = url.searchParams.has('exp') || url.searchParams.has('sig');

if ((isVOD || isBCL) && !isDRM && userId) {
url.searchParams.set('user_id', userId);
if (isJwIntegration && profileId) url.searchParams.append('profile_id', profileId);
}

source.file = url.toString();
});
// Inject user_id and profile_id into the CDN analytics
const { sources, mediaid } = item;
attachAnalyticsParams(sources, mediaid, isJwIntegration, userId, profileId);

// Player options are untyped
const playerOptions: { [key: string]: unknown } = {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Source } from '#types/playlist';

export const attachAnalyticsParams = (sources: Source[], mediaid: string, isJwIntegration: boolean, userId?: string, profileId?: string) => {
return sources.map((source: Source) => {
const url = new URL(source.file);

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

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

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

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

0 comments on commit 114150e

Please sign in to comment.