Created
May 15, 2023 19:02
-
-
Save gkobilansky/07d74d70feedd219e9ded11e9f609754 to your computer and use it in GitHub Desktop.
mux data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable import/prefer-default-export */ | |
import getConfig from 'next/config'; | |
import mux from 'mux-embed'; | |
import Hls from 'hls.js'; | |
const { publicRuntimeConfig } = getConfig(); | |
export const getMonitoring = context => { | |
let metadata = {}; | |
let element = null; | |
const getVideoTitle = video => { | |
const { program, session, exercise, fps } = context; | |
let title = 'Praxis Video'; | |
const fpsTitle = '5PS'; | |
if (program) { | |
title = 'Program Introduction'; | |
if (fps) { | |
title = `${title}: ${fpsTitle}`; | |
} | |
} else if (session && exercise) { | |
const { name } = exercise; | |
const suffixes = { | |
REST_VIDEO_ID: 'Rest', | |
[exercise.fullVideo.id]: 'Tutorial', | |
[exercise.video.id]: 'Demo', | |
}; | |
const suffix = suffixes[video.id]; | |
title = suffix ? `${name} (${suffix})` : name; | |
} else if (session) { | |
title = 'Session Introduction'; | |
} else if (fps) { | |
title = fpsTitle; | |
} | |
return title; | |
}; | |
const getVideoSeries = () => { | |
const { session } = context; | |
let { program } = context; | |
if (session) { | |
program = session.program; | |
} | |
return program ? program.name : 'Praxis Video Series'; | |
}; | |
const init = (videoDOM, video, data) => { | |
const { user = {} } = context; | |
element = videoDOM; | |
metadata = { | |
...data, | |
player_init_time: Date.now(), | |
video_id: video.id, | |
video_duration: video.duration, | |
video_series: getVideoSeries(), | |
video_title: getVideoTitle(video), | |
viewer_user_id: user.id, | |
}; | |
}; | |
const isEnabled = () => { | |
return element && publicRuntimeConfig.muxEnvKey; | |
}; | |
const start = (hls = null) => { | |
if (isEnabled()) { | |
mux.monitor(element, { | |
hlsjs: hls, | |
Hls, | |
data: { | |
env_key: publicRuntimeConfig.muxEnvKey, | |
...metadata, | |
}, | |
}); | |
} | |
}; | |
return { | |
init, | |
start, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment