Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2016 03:25
Show Gist options
  • Save nealrs/35554cc4bcb58df95e21 to your computer and use it in GitHub Desktop.
Save nealrs/35554cc4bcb58df95e21 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)
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