Docs

Get started

Add playback tracking to your player.

@chunkify/player-analytics is the playback tracker for Chunkify Player, HTML video, hls.js, Video.js 10, and Shaka Player.

Track watch time, startup delays, buffering, and playback errors. Send events to Chunkify Analytics, PostHog, Datadog RUM, an OpenTelemetry-compatible endpoint, or your own backend. You can send to several destinations at once.

The PostHog and Datadog RUM adapters use your existing browser clients. They do not require a Chunkify Analytics Server. See Send events for destination setup.

Install the tracker

Add the playback tracker to your application:

npm install @chunkify/player-analytics

Use it in browser code alongside your existing player. Your player and its playback URL should already be configured.

Connect your player

Choose the example for your player. Each example logs events to the browser console so you can check collection before configuring a destination.

You can use any stable video identifier as videoId, including your application's own ID. Using a Chunkify Asset ID is optional. When it matches an Asset in your Chunkify Project, the analytics dashboard in the Chunkify web app can also show its title, poster, and timeline thumbnails when available, with a link to the Asset.

Replace the sample Asset ID in these examples with your chosen video ID. The tracker sends this value as video_id in each event.

Follow the Player setup if needed, then add an ID to your player:

<chunkify-player
  id="player"
  src="https://media.example.com/videos/manifest.m3u8"
  crossorigin="anonymous"
  aspect-ratio="landscape"
  controls
  playsinline
></chunkify-player>

In your browser entry file, attach analytics after the component is registered:

import '@chunkify/player';
import { monitor } from '@chunkify/player-analytics';

await customElements.whenDefined('chunkify-player');
const player = document.querySelector('#player');
if (!player) throw new Error('Missing #player element');

const destination = { send: (event) => console.log(event) };
const session = monitor(player, {
    videoId: 'ast_3Ljtoej7c3Y1plabZoElbQwdwLh',
    destinations: [destination],
});

monitor() follows the player's active media element when it switches between HLS and regular video files.

Press Play and check the console. A playback attempt emits chunkify.video.view_started. Playing, pausing, and seeking produce further events.

Cumulative snapshots are sent every 30 seconds of active playing time by default, and when tab visibility changes during an attempt.

Keep the returned session and call session.destroy() when removing the player or disabling analytics. This ends the current view and removes the analytics listeners. The React example handles this through its ref cleanup.

Call session.startAttempt() immediately before an application-initiated play() to include attempts that fail before a browser play event.

When switching to a different video, destroy the current session and attach a new one with that video's videoId.

Configure collection

All player connectors accept the same options:

Option Purpose and default
videoId Required. Identifies the video in each event.
destinations Required. A non-empty array of adapters or objects with a send(event) method.
videoTitle Optional human-readable title shown next to videoId in reports, where you can also filter and group by it. Each view keeps the title it started with. Send the same title everywhere the video plays, such as its Chunkify Asset title, so filtering by title counts all of its views: views sent without a title, or with another one, are only found by videoId. Longer titles are truncated to 256 characters.
userId Optional ID of the signed-in user in your application, 1 to 256 characters. Use a pseudonymous ID, not an email address.
custom Optional custom dimensions, such as a plan or an A/B test variant, sent on each view's start.
persistViewerId Keeps the generated anonymous viewer ID in localStorage across page loads. Defaults to true; set false until the viewer consents to storage.
environment Optional label, such as production, included in events.
snapshotIntervalMs Active playing time between snapshots, in integer milliseconds. Defaults to 30000; range 1000 to 2147483647.
includeEvents Event types to send. All nine types are enabled by default. An empty array sends nothing.

Subtitle language and playback speed are recorded automatically from the media element, including the choices made in Chunkify Player's menus. See Subtitles and playback speed.

Custom dimensions

Use custom to label views with your own dimensions, then filter and group reports by them. Set it once when you start monitoring; it applies to every view in that session.

const session = monitor(player, {
    videoId: 'ast_3Ljtoej7c3Y1plabZoElbQwdwLh',
    destinations: [destination],
    custom: { plan: 'premium', ab_test: 'B', seats: 5, trial: false },
});

custom accepts up to 10 keys. Each key uses 1 to 40 lowercase letters, digits, or underscores. Each value is text of 1 to 256 characters, a finite number, or a boolean. Monitoring throws an error at startup when custom breaks these limits; null or an empty object sends no dimensions.

The tracker sends custom unchanged on view_started only. Other events in the view share its view_id, and reports read each view's dimensions from its start.

Do not put personal data such as names or email addresses in custom values. Use userId with a pseudonymous ID to identify signed-in users.

See Events and metrics for an event selection example, the complete event list, payloads, and available measurements.

Continue with Send events to connect your analytics service, send to multiple destinations, or customize event fields.