Last active
April 14, 2016 13:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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