This preview of pull request 1639 is meant for internal use only.

analytics.js YouTube Plugin


With the analytics.js YouTube Plugin you can easily collect 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.

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.

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.

the plugins setting screen

Note: At this time, 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 to register and initialize the plugin with the player instance and your API key:

var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
    height: '390',
    width: '640',
    videoId: 'M7lc1UVf-VE'
    });
    var ytAnalytics = new window.analytics.plugins.YouTubeAnalytics(player, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX0365')
    ytAnalytics.initialize()
}

That’s it! The plugin listens to the YouTube player for events, and fires the corresponding Segment Video Spec events on analytics.js.

Supported Events

The following Segment Video Spec 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 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

This page was last modified: 18 Sep 2020



Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
or
Create free account