Tracking Videos

Below are examples of using the YouTube and Vimeo JavaScript Libraries in conjunction with Kissmetrics to record certain video events.

Events Recorded

  • Played Video
  • Paused Video
  • Finished Video

Properties Recorded

  • Played Video Name
  • Paused Video Name
  • Finished Video Name

Vimeo

Vimeo’s Embed Code

  1. Add the embed code to add the Vimeo video in an iFrame. Add api=1 as a query string to the URL of the iframe.

  2. Implement the Player library for tracking Vimeo Videos.

    <script src="https://player.vimeo.com/api/player.js"></script>
    

Add KM Tracking

var iframe = document.getElementById('player'); // replace with your iframe id
var player = new Vimeo.Player(iframe);
var videoName = "Sample Video"; 

player.on('play', function() {
  _kmq.push(['record', 'Played Video', {'Played Video Name': videoName}]);
});

player.on('pause', function() {
  _kmq.push(['record', 'Paused Video', {'Paused Video Name': videoName}]);
});

player.on('ended', function() {
  _kmq.push(['record', 'Finished Video', {'Finished Video Name': videoName}]);
});

Wistia

Wistia’s Embed Code

Please review JavaScript Player API for reference

<div class="wistia_embed wistia_async_YOUR_VIDEO_ID" style="width:640px;height:360px;"></div>
<script src="https://fast.wistia.net/assets/external/E-v1.js" async></script>

Replace YOUR_VIDEO_ID with actual video id

Add KM Tracking

Now below this, let’s add our tracking calls.

<script type="text/javascript">
    // Initialize the Wistia queue (window._wq) if it doesn't already exist
    window._wq = window._wq || [];

    // Push an object into the Wistia queue that contains the video ID and an onReady function
    _wq.push({
        id: YOUR_VIDEO_ID, // Replace YOUR_VIDEO_ID with the actual ID of your Wistia video

        // This function will run once the video is ready
        onReady: function (video) {
            // Call the function to set up Kissmetrics tracking for the video
            loadKMTrackableVideo(video, "Sample Wistia Video"); // The second parameter is the name of the video being tracked
        }
    });

    // Function to bind Kissmetrics events to the Wistia video
    function loadKMTrackableVideo(wistia_object, videoName) {
        // Bind a function to the "play" event to record a Kissmetrics event when the video starts playing
        wistia_object.bind("play", function () {
            _kmq.push(['record', 'Played Video', {'Played Video Name': videoName}]);
        });

        // Bind a function to the "pause" event to record a Kissmetrics event when the video is paused
        wistia_object.bind("pause", function () {
            _kmq.push(['record', 'Paused Video', {'Paused Video Name': videoName}]);
        });

        // Bind a function to the "end" event to record a Kissmetrics event when the video finishes playing
        wistia_object.bind("end", function () {
            _kmq.push(['record', 'Finished Video', {'Finished Video Name': videoName}]);
        });
    }
</script>

Youtube

YouTube’s Embed Code

First, you’ll need to embed your YouTube video using their iFrame API (which has the best compatibility with mobile devices). The code looks like this (remember to change out the videoId with the video you want to embed:

//The <iframe> (and video player) will replace this <div> tag.
<div id="player"></div> 
  <script type='text/javascript'>
    // This code loads the IFrame Player API code asynchronously.
    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    // This function creates an <iframe> (and YouTube player) after the API code downloads.
    var player;
    function onYouTubeIframeAPIReady() {
      player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'M7lc1UVf-VE', // Replace with the desired video ID
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
      });
    }

    function onPlayerReady(event) {
      // Add hooks for what you want to happen when the player has loaded
    }
  </script>

Add KM Tracking

Once that’s done, you can add this block below the embed code.

<script type="text/javascript">
var _kmq = _kmq || [];
function onPlayerStateChange(event) {
  switch(event.data) {
    case YT.PlayerState.PLAYING:
      _kmq.push(['record', 'Played Video', 
                {'Played Video Name':player.getVideoData().title}]);
      break;
    case YT.PlayerState.PAUSED:
      _kmq.push(['record', 'Paused Video', 
                {'Paused Video Name':player.getVideoData().title}]);
      break;
    case YT.PlayerState.ENDED:
      _kmq.push(['record', 'Finished Video', 
                {'Finished Video Name':player.getVideoData().title}]);
      break;
    default:
      return;
  }
}
</script>