trackEvent updating video play duration

Here is some code (inspired by Track when user listens to a sound file from Lukas) that might help you:

document.addEventListener("DOMContentLoaded", function(event) {
  var videos = document.getElementsByTagName("video");
  for (var index = 0; index < videos.length; ++index) {
    var video = videos[index];
    video.addEventListener("playing", function() {
      _paq.push(['trackEvent', 'video', 'play', this.id]);
    }, true);
    video.addEventListener("pause", function() {
      if(this.currentTime < this.duration){
        _paq.push(['trackEvent', 'video', 'pause', this.id, this.currentTime]);
      }
    }, true);
    video.addEventListener("ended", function() {
      _paq.push(['trackEvent', 'video', 'completed', this.id, this.currentTime]);
    }, true);
    video.addEventListener("seeking", function() {
      _paq.push(['trackEvent', 'video', 'seek', this.id, this.currentTime]);
    }, true);
    video.addEventListener("error", function() {
      _paq.push(['trackEvent', 'video', 'error', this.id]);
    }, true);
  }
});

This should add events to your piwik analytics for video play, error, pause, seeking, and completion – with the length played as the event value for the latter three.

1 Like