---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/segment/connections/sources/plugins/youtube#article
headline: analytics.js YouTube Plugin
url: https://www.twilio.com/docs/segment/connections/sources/plugins/youtube
inLanguage: en
dateModified: 2026-06-25T22:35:15.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# analytics.js YouTube Plugin

The analytics.js YouTube Plugin collects YouTube player events into the Segment ecosystem.

## Prerequisites

The Segment YouTube Plugin requires the YouTube player JavaScript object as an input, so your YouTube player embed must use the [YouTube IFrame player API](https://developers.google.com/youtube/iframe_api_reference#Getting_Started).

To begin, you'll need to generate an API Key for the Segment YouTube plugin, which it uses to access metadata about the video content being played. To do this, create a new project in the Google Developer Console, then create a new API Key in that project for the Segment YouTube plugin. You can read more about this process in the YouTube documentation on [registering an application](https://developers.google.com/youtube/registering_an_application).

## Getting Started

### 1. Enable

Enable a new plugin by navigating to the settings for your Source, and clicking  **Plugins**. You can enable the YouTube plugin from this menu.

![Plugins settings showing YouTube and Vimeo with toggle switches.](https://docs-resources.prod.twilio.com/9fcc541057bf36fb5d6809969cd96a7b497b0018e9eb035d8e3c15f6af88d7a3.png)

**Note: Only JavaScript sources support plugins.**

### 2. Initialize

Initialize the plugin by giving it access to the YouTube video player instance(s) running on your page. Use the initialize method in the YouTube `onYouTubeIframeAPIReady()` function:

```js
var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
    height: '390',
    width: '640',
    videoId: 'M7lc1UVf-VE'
    });
    <b>var ytAnalytics = new window.analyticsPlugins.YT(player, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX0365')
    ytAnalytics.initialize()</b>
}
```

The plugin listens to the YouTube player for events, and fires the corresponding [Segment Video Spec](/docs/segment/connections/spec/video/) events on Analytics.js.

## Supported Events

The following [Segment Video Spec](/docs/segment/connections/spec/video/) events are tracked by this plugin:

* Video Playback Started
  * If playing a single video, this fires when the video starts
  * If playing a playlist, this fires when the first video in the playlist starts
* Video Playback Completed
  * If playing a single video, this fires when the video finishes
  * If playing a playlist, this event fires when the final video in the playlist finishes
* Video Playback Paused/Resumed
* Video Playback Buffer Started/Completed
* Video Playback Seek Started/Completed
* Video Content Started/Completed
  * For playlists, these events get fired for each individual video

## Supported Properties

The following [Segment Video Spec](/docs/segment/connections/spec/video/) properties are automatically attached to the above events:

**'Playback' Events**

* Total Length
* Position
* Quality
* Video Player
* Sound

**'Content' Events**

* Title
* Description
* Keywords
* Channel
* Airdate
* Duration
