Who's watching?

Now that HTML5 video & .mp4 are well supported across browsers, you don’t need YouTube in order to add video to my web projects. And, for small web projects, storing files on s3 is effectively free.

The only thing you miss out on with YouTube is view counts / metrics. But is that worth an iframe? I don’t think so. I found a JavaScript gist that tracks video in Google Analytics.

Here’s what you’ll see in GA (btw, don’t spend all day looking at your realtime stats, you’ll go bonkers):

The code is pretty easy to understand, so even a total n00b can get this up and running in about 5 minutes. Here are the most important bits:

```javascript // get basic video info var vid = document.getElementById(‘video’) var title = ‘My Video’

//Listen for video events (ended, play, pause)

vid.addEventListener(‘ended’, videoEnd, false); vid.addEventListener(‘play’, videoPlay, false); vid.addEventListener(‘pause’, videoPause, false);

// send events to Google Analytics

function videoEnd () { ga(‘send’, ‘event’, ‘video’, ‘100% video played’, title); }

function videoPlay () { ga(‘send’, ‘event’, ‘video’, ‘video played’, title) setKeyFrames(this.duration); // see the full code to see how this works }

function videoPause () { ga(‘send’, ‘event’, ‘video’, ‘video paused’, title); ```

Comments :)
2017 Neal Shyam