Skip to content

Instantly share code, notes, and snippets.

@Code-Case
Created July 12, 2016 21:43
Show Gist options
  • Save Code-Case/690fc6aa8340f3260bbd10652b4c5c54 to your computer and use it in GitHub Desktop.
Save Code-Case/690fc6aa8340f3260bbd10652b4c5c54 to your computer and use it in GitHub Desktop.
Google Analytics: HTML5 Video Tracking
document.addEventListener('DOMContentLoaded', init, false)
var videoId = document.getElementById('video')
//var videoTitle = videoId.getAttribute('data-description')
var videoTitle = '宝宝身边有怪物'
function init () {
videoId.addEventListener('ended', videoEnd, false)
videoId.addEventListener('timeupdate', videoTimeUpdate, false)
videoId.addEventListener('play', videoPlay, false)
videoId.addEventListener('pause', videoPause, false)
}
function setKeyFrames (duration) {
var quarter = (duration / 4).toFixed(1)
sessionStorage.setItem('one', quarter)
sessionStorage.setItem('two', (quarter * 2).toFixed(1))
sessionStorage.setItem('three', (quarter * 3).toFixed(1))
}
function videoTimeUpdate () {
var curTime = videoId.currentTime.toFixed(1)
switch (curTime) {
case sessionStorage.getItem('one'):
ga('send', 'event', 'video', '25% video played', videoTitle)
sessionStorage.setItem('one', null)
case sessionStorage.getItem('two'):
ga('send', 'event', 'video', '50% video played', videoTitle)
sessionStorage.setItem('two', null)
case sessionStorage.getItem('three'):
ga('send', 'event', 'video', '75% video played', videoTitle)
sessionStorage.setItem('three', null)
}
}
function videoEnd () {
ga('send', 'event', 'video', '100% video played', videoTitle)
}
function videoPlay () {
ga('send', 'event', 'video', 'video played', videoTitle)
setKeyFrames(this.duration)
}
function videoPause () {
ga('send', 'event', 'video', 'video paused', videoTitle)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment