# Snowplow media tracking on web
> Manually track media events from any video player using flexible event tracking methods with custom entities and ad support.
> Source: https://docs.snowplow.io/docs/sources/web-trackers/tracking-events/media/snowplow/
> Documentation index: https://docs.snowplow.io/llms.txt

This plugin is the recommended way to manually track media events from video players. This plugin allows you to implement media tracking for any player.

See the [media tracking overview page](/docs/events/ootb-data/media-events/) for more details on schemas and using the plugin.

> **Info: Example app**
>
> To illustrate the tracked [events](/docs/fundamentals/events/) and [entities](/docs/fundamentals/entities/), visit our [example app](https://snowplow-industry-solutions.github.io/snowplow-javascript-tracker-examples/media) that showcases the tracked media events and entities live as you watch a video.
>
> Source code for the app is [available here](https://github.com/snowplow-industry-solutions/snowplow-javascript-tracker-examples).

> **Note:** The plugin is available since version 3.12 of the tracker.

Snowplow media events and entities must be **manually tracked**.

## Install plugin

**JavaScript (tag):**

| Tracker Distribution | Included |
| -------------------- | -------- |
| `sp.js`              | ❌        |
| `sp.lite.js`         | ❌        |

**Download:**

|                                             |                                                                                                       |
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| Download from GitHub Releases (Recommended) | [Github Releases (plugins.umd.zip)](https://github.com/snowplow/snowplow-javascript-tracker/releases) |
| Available on jsDelivr                       | [jsDelivr](https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-media@4.10.2/dist/index.umd.min.js)  |
| Available on unpkg                          | [unpkg](https://unpkg.com/@snowplow/browser-plugin-media@4.10.2/dist/index.umd.min.js)                |

```javascript
window.snowplow(
    'addPlugin',
    'https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-media@4.10.2/dist/index.umd.min.js',
    ['snowplowMedia', 'SnowplowMediaPlugin']
);
```

**Browser (npm):**

- `npm install @snowplow/browser-plugin-media`
- `yarn add @snowplow/browser-plugin-media`
- `pnpm add @snowplow/browser-plugin-media`

```javascript
import { newTracker, trackPageView } from '@snowplow/browser-tracker';
import { SnowplowMediaPlugin } from '@snowplow/browser-plugin-media';

newTracker('sp1', '{{collector_url}}', {
   appId: 'my-app-id',
   plugins: [ SnowplowMediaPlugin() ],
});
```

***

## Usage

The Snowplow media tracking APIs enable you to track events from media playback on the web and in mobile apps. Track changes in playback state (play, pause, seek), playback position (ping and percentage progress events), and ad playback (ad breaks, ad progress, ad clicks).

For details on the events and entities tracked, see the [media tracking overview](/docs/events/ootb-data/media-events/).

> **Info: Example app**
>
> See the tracked media events and entities live as you watch a video in our [React example app](https://snowplow-industry-solutions.github.io/snowplow-javascript-tracker-examples/media). [View the source code](https://github.com/snowplow-industry-solutions/snowplow-javascript-tracker-examples/tree/master/react).

## Quick start

A media tracking session follows this lifecycle:

1. **Start tracking** when the player loads (before playback begins)
2. **Update player state** every second during playback
3. **Track events** as they occur (play, pause, seek, etc.)
4. **End tracking** when the user leaves

**JavaScript (tag):**

```javascript
// 1. Start tracking when player loads
const id = crypto.randomUUID();
window.snowplow('startMediaTracking', {
  id,
  player: {
      duration: 300,
      label: 'My Video Title',
      mediaType: 'video',
      playerType: 'html5'
  }
});

// 2. Update player state every second
setInterval(() => {
  window.snowplow('updateMediaTracking', {
      id,
      player: { currentTime: player.currentTime }
  });
}, 1000);

// 3. Track events as they occur
window.snowplow('trackMediaPlay', { id });
window.snowplow('trackMediaPause', { id });
window.snowplow('trackMediaSeekStart', { id });
window.snowplow('trackMediaSeekEnd', { id });
window.snowplow('trackMediaEnd', { id });

// 4. End tracking when done
window.snowplow('endMediaTracking', { id });
```

**Browser (npm):**

```javascript
import {
  startMediaTracking,
  updateMediaTracking,
  endMediaTracking,
  trackMediaPlay,
  trackMediaPause,
  trackMediaSeekStart,
  trackMediaSeekEnd,
  trackMediaEnd,
  MediaType
} from "@snowplow/browser-plugin-media";

// 1. Start tracking when player loads
const id = crypto.randomUUID();
startMediaTracking({
  id,
  player: {
      duration: 300,
      label: 'My Video Title',
      mediaType: MediaType.Video,
      playerType: 'html5'
  }
});

// 2. Update player state every second
setInterval(() => {
  updateMediaTracking({
      id,
      player: { currentTime: player.currentTime }
  });
}, 1000);

// 3. Track events as they occur
trackMediaPlay({ id });
trackMediaPause({ id });
trackMediaSeekStart({ id });
trackMediaSeekEnd({ id });
trackMediaEnd({ id });

// 4. End tracking when done
endMediaTracking({ id });
```

***

## Configuration

Configure media tracking when you call `startMediaTracking`. All configuration is optional.

### Player properties

Set initial player properties to populate the [media player entity](/docs/events/ootb-data/media-events/#media-player). The `label` property is recommended as it helps identify content during analysis.

**JavaScript (tag):**

```javascript
window.snowplow('startMediaTracking', {
  id,
  player: {
      currentTime: 0,          // Current playback position in seconds
      duration: 300,           // Total duration in seconds
      ended: false,            // Whether playback has ended
      livestream: false,       // Whether this is a live stream
      label: 'My Video',       // Human-readable title (recommended)
      loop: false,             // Whether to restart after ending
      mediaType: 'video',      // 'video' or 'audio'
      muted: false,            // Whether audio is muted
      paused: true,            // Whether playback is paused
      pictureInPicture: false, // Whether in picture-in-picture mode
      playerType: 'html5',     // Player type identifier
      playbackRate: 1.0,       // Playback speed (1 = normal)
      quality: '1080p',        // Quality level
      volume: 100              // Volume percentage (0-100)
  }
});
```

**Browser (npm):**

```javascript
import { startMediaTracking, MediaType } from "@snowplow/browser-plugin-media";

startMediaTracking({
  id,
  player: {
      currentTime: 0,          // Current playback position in seconds
      duration: 300,           // Total duration in seconds
      ended: false,            // Whether playback has ended
      livestream: false,       // Whether this is a live stream
      label: 'My Video',       // Human-readable title (recommended)
      loop: false,             // Whether to restart after ending
      mediaType: MediaType.Video, // MediaType.Video or MediaType.Audio
      muted: false,            // Whether audio is muted
      paused: true,            // Whether playback is paused
      pictureInPicture: false, // Whether in picture-in-picture mode
      playerType: 'html5',     // Player type identifier
      playbackRate: 1.0,       // Playback speed (1 = normal)
      quality: '1080p',        // Quality level
      volume: 100              // Volume percentage (0-100)
  }
});
```

***

### Ping events

Media ping events are sent at regular intervals (default: 30 seconds) to report playback position. You can configure ping behavior in the starting configuration.

**JavaScript (tag):**

```javascript
window.snowplow('startMediaTracking', {
  id,
  pings: {
      pingInterval: 30,   // Seconds between pings (default: 30)
      maxPausedPings: 1   // Max pings while paused (default: 1)
  }
});

// Or turn off pings entirely
window.snowplow('startMediaTracking', { id, pings: false });
```

**Browser (npm):**

```javascript
startMediaTracking({
  id,
  pings: {
      pingInterval: 30,   // Seconds between pings (default: 30)
      maxPausedPings: 1   // Max pings while paused (default: 1)
  }
});

// Or turn off pings entirely
startMediaTracking({ id, pings: false });
```

***

> **Info: Media pings vs page pings**
>
> Media ping events are separate from page ping events tracked by [activity tracking](/docs/events/ootb-data/page-activity-tracking). Media pings contain playback information (player and session entities), while page pings do not.

### Percentage progress

Track events when playback reaches specified percentage boundaries.

**JavaScript (tag):**

```javascript
window.snowplow('startMediaTracking', {
  id,
  boundaries: [10, 25, 50, 75, 90]  // Fire events at these percentages
});
```

**Browser (npm):**

```javascript
startMediaTracking({
  id,
  boundaries: [10, 25, 50, 75, 90]  // Fire events at these percentages
});
```

***

### Session tracking

The [media session entity](/docs/events/ootb-data/media-events/#media-session) is attached to all media events by default. It's optional for the [Media Player](/docs/modeling-your-data/modeling-your-data-with-dbt/dbt-models/dbt-media-player-data-model/) data model, so you could choose not to include it.

We recommend tracking this entity.

**JavaScript (tag):**

```javascript
window.snowplow('startMediaTracking', { id, session: true });
```

**Browser (npm):**

```javascript
startMediaTracking({ id, session: true });
```

***

To set a custom session start time, pass an object with a `startedAt` property instead of a boolean:

```javascript
session: { startedAt: new Date('2024-01-15T10:30:00Z') }
```

### Custom entities

You can attach custom entities to all events for this media tracking instance.

**JavaScript (tag):**

```javascript
window.snowplow('startMediaTracking', {
  id,
  context: [
      {
          schema: 'iglu:com.example/video_metadata/jsonschema/1-0-0',
          data: { contentId: 'abc123', category: 'tutorial' }
      }
  ]
});
```

**Browser (npm):**

```javascript
startMediaTracking({
  id,
  context: [
      {
          schema: 'iglu:com.example/video_metadata/jsonschema/1-0-0',
          data: { contentId: 'abc123', category: 'tutorial' }
      }
  ]
});
```

***

### Filter events

**JavaScript (tag):**

Only track specific event types by providing an allowlist:

```javascript
window.snowplow('startMediaTracking', {
  id,
  captureEvents: ['play', 'pause', 'end']  // Only track these events
});
```

When users scrub through video, media players can generate many rapid seek or volume change events. The tracker automatically coalesces these into just the first and last event. This behavior is optional:

```javascript
// Turn off filtering entirely
window.snowplow('startMediaTracking', { id, filterOutRepeatedEvents: false });

// Or configure separately for seek and volume events
window.snowplow('startMediaTracking', {
  id,
  filterOutRepeatedEvents: {
      seekEvents: true,
      volumeChangeEvents: false,
      flushTimeoutMs: 5000  // Time before flushing buffered events (default: 5000)
  }
});
```

**Browser (npm):**

Only track specific event types by providing an allowlist:

```javascript
import { MediaEventType } from "@snowplow/browser-plugin-media";

startMediaTracking({
  id,
  captureEvents: [MediaEventType.Play, MediaEventType.Pause, MediaEventType.End]
});
```

When users scrub through video, media players can generate many rapid seek or volume change events. The tracker automatically coalesces these into just the first and last event. This behavior is optional:

```javascript
// Turn off filtering entirely
startMediaTracking({ id, filterOutRepeatedEvents: false });

// Or configure separately for seek and volume events
startMediaTracking({
  id,
  filterOutRepeatedEvents: {
      seekEvents: true,
      volumeChangeEvents: false,
      flushTimeoutMs: 5000  // Time before flushing buffered events (default: 5000)
  }
});
```

***

## Tracking events

Track events by calling the appropriate function when player events occur. For a complete list of available events and their properties, see the [media events reference](/docs/events/ootb-data/media-events/#playback-lifecycle-events).

### Playback events

**JavaScript (tag):**

```javascript
window.snowplow('trackMediaReady', { id });        // Player is ready
window.snowplow('trackMediaPlay', { id });          // Playback started
window.snowplow('trackMediaPause', { id });         // Playback paused
window.snowplow('trackMediaEnd', { id });           // Playback ended
window.snowplow('trackMediaSeekStart', { id });     // Seek operation started
window.snowplow('trackMediaSeekEnd', { id });       // Seek operation ended
window.snowplow('trackMediaBufferStart', { id });   // Buffering started
window.snowplow('trackMediaBufferEnd', { id });     // Buffering ended
```

**Browser (npm):**

```javascript
import {
  trackMediaReady, trackMediaPlay, trackMediaPause, trackMediaEnd,
  trackMediaSeekStart, trackMediaSeekEnd, trackMediaBufferStart, trackMediaBufferEnd
} from "@snowplow/browser-plugin-media";

trackMediaReady({ id });        // Player is ready
trackMediaPlay({ id });         // Playback started
trackMediaPause({ id });        // Playback paused
trackMediaEnd({ id });          // Playback ended
trackMediaSeekStart({ id });    // Seek operation started
trackMediaSeekEnd({ id });      // Seek operation ended
trackMediaBufferStart({ id });  // Buffering started
trackMediaBufferEnd({ id });    // Buffering ended
```

***

### Player state events

**JavaScript (tag):**

```javascript
window.snowplow('trackMediaPlaybackRateChange', { id, newRate: 1.5 });
window.snowplow('trackMediaVolumeChange', { id, newVolume: 80 });
window.snowplow('trackMediaFullscreenChange', { id, fullscreen: true });
window.snowplow('trackMediaPictureInPictureChange', { id, pictureInPicture: true });
window.snowplow('trackMediaQualityChange', {
  id,
  newQuality: '1080p',
  bitrate: 5000,
  framesPerSecond: 30,
  automatic: true
});
window.snowplow('trackMediaError', {
  id,
  errorCode: '500',
  errorName: 'NetworkError',
  errorDescription: 'Failed to load media'
});
```

**Browser (npm):**

```javascript
import {
  trackMediaPlaybackRateChange, trackMediaVolumeChange, trackMediaFullscreenChange,
  trackMediaPictureInPictureChange, trackMediaQualityChange, trackMediaError
} from "@snowplow/browser-plugin-media";

trackMediaPlaybackRateChange({ id, newRate: 1.5 });
trackMediaVolumeChange({ id, newVolume: 80 });
trackMediaFullscreenChange({ id, fullscreen: true });
trackMediaPictureInPictureChange({ id, pictureInPicture: true });
trackMediaQualityChange({
  id,
  newQuality: '1080p',
  bitrate: 5000,
  framesPerSecond: 30,
  automatic: true
});
trackMediaError({
  id,
  errorCode: '500',
  errorName: 'NetworkError',
  errorDescription: 'Failed to load media'
});
```

***

### Ad events

Track advertising events with ad and ad break information. See the [media ad](/docs/events/ootb-data/media-events/#media-ad) and [media ad break](/docs/events/ootb-data/media-events/#media-ad-break) entity schemas for property details.

**JavaScript (tag):**

```javascript
// Ad break events
window.snowplow('trackMediaAdBreakStart', {
  id,
  adBreak: {
      breakId: 'break-1',
      name: 'pre-roll',
      breakType: 'linear',
      podSize: 3
  }
});
window.snowplow('trackMediaAdBreakEnd', { id });

// Ad events
window.snowplow('trackMediaAdStart', {
  id,
  ad: {
      adId: 'ad-1',
      name: 'Product Ad',
      creativeId: 'creative-1',
      duration: 30,
      skippable: true
  }
});
window.snowplow('trackMediaAdFirstQuartile', { id });
window.snowplow('trackMediaAdMidpoint', { id });
window.snowplow('trackMediaAdThirdQuartile', { id });
window.snowplow('trackMediaAdComplete', { id });
window.snowplow('trackMediaAdSkip', { id });
window.snowplow('trackMediaAdClick', { id });
window.snowplow('trackMediaAdPause', { id });
window.snowplow('trackMediaAdResume', { id });
```

**Browser (npm):**

```javascript
import {
  trackMediaAdBreakStart, trackMediaAdBreakEnd, trackMediaAdStart,
  trackMediaAdFirstQuartile, trackMediaAdMidpoint, trackMediaAdThirdQuartile,
  trackMediaAdComplete, trackMediaAdSkip, trackMediaAdClick,
  trackMediaAdPause, trackMediaAdResume, MediaPlayerAdBreakType
} from "@snowplow/browser-plugin-media";

// Ad break events
trackMediaAdBreakStart({
  id,
  adBreak: {
      breakId: 'break-1',
      name: 'pre-roll',
      breakType: MediaPlayerAdBreakType.Linear,
      podSize: 3
  }
});
trackMediaAdBreakEnd({ id });

// Ad events
trackMediaAdStart({
  id,
  ad: {
      adId: 'ad-1',
      name: 'Product Ad',
      creativeId: 'creative-1',
      duration: 30,
      skippable: true
  }
});
trackMediaAdFirstQuartile({ id });
trackMediaAdMidpoint({ id });
trackMediaAdThirdQuartile({ id });
trackMediaAdComplete({ id });
trackMediaAdSkip({ id });
trackMediaAdClick({ id });
trackMediaAdPause({ id });
trackMediaAdResume({ id });
```

***

### Custom events

Track custom self-describing events within the media session. The tracker automatically attaches media entities.

**JavaScript (tag):**

```javascript
window.snowplow('trackMediaSelfDescribingEvent', {
  id,
  event: {
      schema: 'iglu:com.example/video_interaction/jsonschema/1-0-0',
      data: { action: 'share', platform: 'twitter' }
  }
});
```

**Browser (npm):**

```javascript
import { trackMediaSelfDescribingEvent } from "@snowplow/browser-plugin-media";

trackMediaSelfDescribingEvent({
  id,
  event: {
      schema: 'iglu:com.example/video_interaction/jsonschema/1-0-0',
      data: { action: 'share', platform: 'twitter' }
  }
});
```

***

## Update player state

**JavaScript (tag):**

Update the player state every second during playback. This ensures accurate metrics in ping events and the session entity. This function does not send any events.

```javascript
window.snowplow('updateMediaTracking', {
  id,
  player: { currentTime: player.currentTime }
});
```

**Browser (npm):**

Update the player state every second during playback. This ensures accurate metrics in ping events and the session entity. This function does not send any events.

```javascript
import { updateMediaTracking } from "@snowplow/browser-plugin-media";

updateMediaTracking({
  id,
  player: { currentTime: player.currentTime }
});
```

***

## Page activity during playback

When users watch video, they may not interact with the page, causing page pings to stop. The media plugin automatically keeps page activity alive during playback by calling `updatePageActivity` whenever media events are tracked.

You can turn off this behavior if not needed:

**JavaScript (tag):**

```javascript
window.snowplow('startMediaTracking', {
    id,
    updatePageActivityWhilePlaying: false
});
```

**Browser (npm):**

```javascript
startMediaTracking({
    id,
    updatePageActivityWhilePlaying: false
});
```

***
