0.1.3 • Published 5 months ago

react-video-analytics v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

GitHub package.json version (subfolder of monorepo) npm downloads code style: prettier GitHub

React Video Analytics

Easily generate and post video player metrics

Table of contents

Installation

To install and set up the library, run:

$ npm install --save react-video-analytics

Or if you prefer using Yarn:

$ yarn add react-video-analytics

Usage

Setup

Begin by wrapping your app with the AnalyticsProvider.

import { AnalyticsProvider } from 'react-video-analytics'

...

return (
  <AnalyticsProvider>
    <App />
  </AnalyticsProvider>
)

Attach

Using the useAnalytics hook, attach a reference to your video player.

import { useAnalytics } from 'react-video-analytics'

const MyComponent = () => {
  const videoRef = useRef<HTMLVideoElement>(null)
  
  useAnalytics(videoRef)
  
  return (
    <video ref={videoRef} />
  )
}

Send

Implement the send option to send metrics to your analytics service. The send function will be called every time the video player emits a ReportAction which you can reference via metrics.action. The following example uses axios to post the metrics payload to an API endpoint:

import axios from 'axios'
import { useAnalytics } from 'react-video-analytics'

const MyComponent = () => {
  const videoRef = useRef<HTMLVideoElement>(null)
  
  useAnalytics(videoRef, {
    send: (metrics) => {
      // Send metrics to your analytics service
      axios.post('https://my-api.com/video-analytics', metrics)
    }
  })
  
  return (
    <video ref={videoRef} />
  )
}

API

AnalyticsProvider

Use the AnalyticsProvider to create custom video player configurations. By default, react-video-analytics supports a standard HTML video player. It also ships with an optional VimePlayerConfig that you can use instead if your project uses a Vime video player.

Props

PropTypeDefault valueDescription
defaultPlayerConfig (optional)PlayerConfigVideoPlayerConfigProvides the default player configuration to use.
defaultTimeInterval (optional)number30000The default interval, in milliseconds, to call send when the timeupdate event is emitted
viewerIdKey (optional)string'react-video-analytics-viewer-id'The storage key name to use for storing the viewer's unique identifier.

Types

PlayerConfig<Player = unknown>
PropTypeDescription
getVideoElement<P extends Player>(player: P) => Promise<HTMLVideoElement>Defines how to retreive the html video element from a generic video player component

Examples

Using a custom player component:

import { PlayerConfig } from 'react-video-analytics'

...

return (
  <AnayticsProvider
    defaultPlayerConfig={{ 
      getVideoElement: (player: SomeCustomPlayer) => {
        // Write logic to return the html video element from your custom player
      } 
    } as PlayerConfig<SomeCustomPlayer> }
  >
    <App/>
  </AnayticsProvider>
)

...

Using the Vime video player component:

import { VimePlayerConfig } from 'react-video-analytics'

...

return (
  <AnayticsProvider
    defaultPlayerConfig={VimePlayerConfig}
  >
    <App/>
  </AnayticsProvider>
)

...

useAnalytics

The useAnalytics hook requires a reference to your video player component. It also accepts an optional options object that allows you to customize how metrics are handled and sent to your analytics service.

Options

PropTypeDefaultDescription
send (optional)(metrics: ReportMetrics) => void-Describes how to post metrics to your analytics service
videoId (optional)string-Optionally supply a unique identifier for the video source being played. When this changes, a new viewId is generated
maxAttempts (optional)number5Maximum number of times to attempt to send metrics before calling onFail
playerConfig (optional)PlayerConfigVideoPlayerConfigThe player configuration to use corresponding to the player component reference passed to useAnalytics
timeInterval (optional)number30000The interval, in milliseconds, to call send when the timeupdate event is emitted
onQueue (optional)(metrics: ReportMetrics) => void-Called when metrics are queued to be sent
onComplete (optional)(metrics: ReportMetrics) => void-Called when metrics are successfully sent
onError (optional)(metrics: ReportMetrics) => void-Called when metrics fail to be sent
onRequeue (optional)(metrics: ReportMetrics) => void-Called when metrics are requeued to be sent
onFail (optional)(metrics: ReportMetrics) => void-Called when metrics fail to be sent after maxAttempts

Types

ReportMetrics
PropTypeDescription
timestampstringThe timestamp when the metric was created
hourOfDaynumberThe hour of day when the metric was created
dayOfWeeknumberThe day of the week when the metric was created
actionReportActionThe action that generated the metric
positionnumberThe current time (position), in seconds, of the video player
durationnumberThe total duration, in seconds, of the video being played
durationBufferingnumberThe time spent buffering, in seconds, whenever the video finishes buffering
browserBrowserStateDetails about the browser being used to watch the video
headersReportHeadersThe view and viewer ID of the video session
error (optional)ReportErrorError details. Particularly useful when onError, onRequeue, or onFail are called
__attempts (optional)numberThe total number of attempts to send metrics. Particularly useful when onRequeue is called
ReportAction
ValueDescription
completeThe video completed playing
pauseThe video player was paused
playThe video player was played
qualityThe video quality setting was changed
seekThe video player began seeking
bufferThe video player began buffering
timeThe video player's current time was updated. By default this action occurs every 30 seconds.
setupThe initial report action
errorA playback error occurred
BrowserState
PropTypeDescription
host (optional)stringThe host domain that the video is being watched on.
os (optional)stringThe operating system that the video is being watched on.
name (optional)stringThe name of the browser that the video is being watched on.
version (optional)stringThe browser version that the video is being watched on.
ReportHeaders
PropTypeDescription
viewIdstringAn identifier for the video's current view (session). Use this for tracking the number of views on your video.
viewerIdstringAn identifier for the unique viewer (user) watching the video. Use this to track the number of unique viewers of your video.
ReportError
PropTypeDescription
messagestringA message describing the error that occurred.
codestringA code associated to the error that occurred.
dataobjectA object containing additional details about the error that occurred.
source (optional)unknownA potential reference to the error's source.

Authors

See also the list of contributors who participated in this project.

License

MIT License © oos

0.1.3

5 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago