Skip to content

Instantly share code, notes, and snippets.

@jakewhiteley
Last active April 14, 2016 13:47
Show Gist options
  • Save jakewhiteley/a9056b8bcd7747b13820aeb0a78e6671 to your computer and use it in GitHub Desktop.
Save jakewhiteley/a9056b8bcd7747b13820aeb0a78e6671 to your computer and use it in GitHub Desktop.
Some basic html5 video element events for google analytics. Tracks: play, pause, complete events.
// assumes jquery
$('video').on('pause play ended', function ( e ) {
switch( e.type ) {
case 'play':
var percent = Math.round( e.target.currentTime / e.target.duration * 100 );
ga('send', 'event', 'video', 'play', window.location.href, percent)
break;
case 'pause':
var percent = Math.round( e.target.currentTime / e.target.duration * 100 );
ga('send', 'event', 'video', 'pause', window.location.href, percent )
break;
case 'ended':
ga('send', 'event', 'video', 'completed', window.location.href )
break;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment