Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuhui/2bc291200b6e1f5df52966253281e16f to your computer and use it in GitHub Desktop.
Save yuhui/2bc291200b6e1f5df52966253281e16f to your computer and use it in GitHub Desktop.
Track Brightcove video playback to Adobe Analytics using Adobe Launch
/**
* Track Brightcove media playback using Adobe Launch's Media-related rules.
*
* Brightcove documentation:
* - Player events: https://support.brightcove.com/player-events
* - Player metadata: https://support.brightcove.com/player-metadata-bcinfo
* - Video metadata: https://support.brightcove.com/video-metadata-mediainfo
*
* Brightcove Player API documentation:
* - Current release: https://docs.brightcove.com/brightcove-player/current-release/index.html
*/
/**
* private function to get a media's metadata
*/
function getMediaInfo_(player, nextTryDelay) {
var mediaInfo;
if (!!player.mediainfo) {
mediaInfo = player.mediainfo;
} else if (nextTryDelay < 5000) {
setTimeout(function () {
nextTryDelay = nextTryDelay *= 2;
mediaInfo = getMediaInfo_(player, nextTryDelay);
}, nextTryDelay);
}
return mediaInfo;
}
/** end private function */
var playerId = this.playerId; // "this" refers to the event of the Media rule
var player = videojs.players[playerId]; // "videojs" is Brightcove's JavaScript object
var mediaInfo = getMediaInfo_(player, 500);
var mediaName = mediaInfo.name;
var mediaDuration = mediaInfo.duration;
var mediaCurrentTime = player.currentTime();
var eventType = event.nativeEvent.type; // "event" refers to the Media rule's event
var mediaStatus;
switch (eventType) {
case 'ended':
mediaStatus = 'ended';
break;
case 'loadeddata':
mediaStatus = 'started';
break;
case 'pause':
// a "pause" event is sent with the "ended" event, so don't track the pause in that case
if (Math.ceil(mediaCurrentTime) < Math.ceil(mediaDuration)) {
mediaStatus = 'paused';
}
break;
}
if (!!mediaStatus) {
digitalData.media = digitalData.media || {};
digitalData.media.mediaInfo = digitalData.media.mediaInfo || {};
digitalData.media.mediaInfo.name = mediaName;
// set the currentTime to 0 when the media has started, because Math.ceil() will likely make it 1
// (since this event could be called a few milliseconds after the media has started)
digitalData.media.mediaInfo.currentTime = mediaEvent === 'started' ? 0 : Math.ceil(mediaCurrentTime);
digitalData.media.mediaInfo.duration = Math.ceil(mediaDuration);
digitalData.media.mediaInfo.status = mediaStatus;
/**
* use the data in digitalData.media.mediaInfo to track the playback to Adobe Analytics
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment