diff --git a/dist/videojs.ga.js b/dist/videojs.ga.js index 0a6b607..acf182d 100644 --- a/dist/videojs.ga.js +++ b/dist/videojs.ga.js @@ -1,144 +1,156 @@ /* -* videojs-ga - v0.4.1 - 2014-06-06 +* videojs-ga - v0.4.1 - 2014-11-11 * Copyright (c) 2014 Michael Bensoussan * Licensed MIT */ (function() { var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; - videojs.plugin('ga', function(options) { - var dataSetupOptions, defaultsEventsToTrack, end, error, eventCategory, eventLabel, eventsToTrack, fullscreen, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, resize, seekEnd, seekStart, seeking, sendbeacon, timeupdate, volumeChange; - if (options == null) { - options = {}; + (function(factory) { + if (typeof exports === "object" && typeof require === "function") { + return module.exports = factory(require("video")); + } else if (typeof define === "function" && define.amd) { + return define(["./video"], function(vjs) { + return factory(vjs); + }); + } else { + return factory(window.videojs); } - dataSetupOptions = {}; - if (this.options()["data-setup"]) { - parsedOptions = JSON.parse(this.options()["data-setup"]); - if (parsedOptions.ga) { - dataSetupOptions = parsedOptions.ga; + })(function(videojs) { + return videojs.plugin('ga', function(options) { + var dataSetupOptions, defaultsEventsToTrack, end, error, eventCategory, eventLabel, eventsToTrack, fullscreen, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, resize, seekEnd, seekStart, seeking, sendbeacon, timeupdate, volumeChange; + if (options == null) { + options = {}; } - } - defaultsEventsToTrack = ['loaded', 'percentsPlayed', 'start', 'end', 'seek', 'play', 'pause', 'resize', 'volumeChange', 'error', 'fullscreen']; - eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack; - percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10; - eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video'; - eventLabel = options.eventLabel || dataSetupOptions.eventLabel; - percentsAlreadyTracked = []; - seekStart = seekEnd = 0; - seeking = false; - loaded = function() { - if (!eventLabel) { - eventLabel = this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i, ''); - } - if (__indexOf.call(eventsToTrack, "loadedmetadata") >= 0) { - sendbeacon('loadedmetadata', true); + dataSetupOptions = {}; + if (this.options()["data-setup"]) { + parsedOptions = JSON.parse(this.options()["data-setup"]); + if (parsedOptions.ga) { + dataSetupOptions = parsedOptions.ga; + } } - }; - timeupdate = function() { - var currentTime, duration, percent, percentPlayed, _i; - currentTime = Math.round(this.currentTime()); - duration = Math.round(this.duration()); - percentPlayed = Math.round(currentTime / duration * 100); - for (percent = _i = 0; _i <= 99; percent = _i += percentsPlayedInterval) { - if (percentPlayed >= percent && __indexOf.call(percentsAlreadyTracked, percent) < 0) { - if (__indexOf.call(eventsToTrack, "start") >= 0 && percent === 0 && percentPlayed > 0) { - sendbeacon('start', true); - } else if (__indexOf.call(eventsToTrack, "percentsPlayed") >= 0 && percentPlayed !== 0) { - sendbeacon('percent played', true, percent); + defaultsEventsToTrack = ['loaded', 'percentsPlayed', 'start', 'end', 'seek', 'play', 'pause', 'resize', 'volumeChange', 'error', 'fullscreen']; + eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack; + percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10; + eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video'; + eventLabel = options.eventLabel || dataSetupOptions.eventLabel; + percentsAlreadyTracked = []; + seekStart = seekEnd = 0; + seeking = false; + loaded = function() { + if (!eventLabel) { + eventLabel = this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i, ''); + } + if (__indexOf.call(eventsToTrack, "loadedmetadata") >= 0) { + sendbeacon('loadedmetadata', true); + } + }; + timeupdate = function() { + var currentTime, duration, percent, percentPlayed, _i; + currentTime = Math.round(this.currentTime()); + duration = Math.round(this.duration()); + percentPlayed = Math.round(currentTime / duration * 100); + for (percent = _i = 0; _i <= 99; percent = _i += percentsPlayedInterval) { + if (percentPlayed >= percent && __indexOf.call(percentsAlreadyTracked, percent) < 0) { + if (__indexOf.call(eventsToTrack, "start") >= 0 && percent === 0 && percentPlayed > 0) { + sendbeacon('start', true); + } else if (__indexOf.call(eventsToTrack, "percentsPlayed") >= 0 && percentPlayed !== 0) { + sendbeacon('percent played', true, percent); + } + if (percentPlayed > 0) { + percentsAlreadyTracked.push(percent); + } } - if (percentPlayed > 0) { - percentsAlreadyTracked.push(percent); + } + if (__indexOf.call(eventsToTrack, "seek") >= 0) { + seekStart = seekEnd; + seekEnd = currentTime; + if (Math.abs(seekStart - seekEnd) > 1) { + seeking = true; + sendbeacon('seek start', false, seekStart); + sendbeacon('seek end', false, seekEnd); } } - } - if (__indexOf.call(eventsToTrack, "seek") >= 0) { - seekStart = seekEnd; - seekEnd = currentTime; - if (Math.abs(seekStart - seekEnd) > 1) { - seeking = true; - sendbeacon('seek start', false, seekStart); - sendbeacon('seek end', false, seekEnd); + }; + end = function() { + sendbeacon('end', true); + }; + play = function() { + var currentTime; + currentTime = Math.round(this.currentTime()); + sendbeacon('play', true, currentTime); + seeking = false; + }; + pause = function() { + var currentTime, duration; + currentTime = Math.round(this.currentTime()); + duration = Math.round(this.duration()); + if (currentTime !== duration && !seeking) { + sendbeacon('pause', false, currentTime); } - } - }; - end = function() { - sendbeacon('end', true); - }; - play = function() { - var currentTime; - currentTime = Math.round(this.currentTime()); - sendbeacon('play', true, currentTime); - seeking = false; - }; - pause = function() { - var currentTime, duration; - currentTime = Math.round(this.currentTime()); - duration = Math.round(this.duration()); - if (currentTime !== duration && !seeking) { - sendbeacon('pause', false, currentTime); - } - }; - volumeChange = function() { - var volume; - volume = this.muted() === true ? 0 : this.volume(); - sendbeacon('volume change', false, volume); - }; - resize = function() { - sendbeacon('resize - ' + this.width() + "*" + this.height(), true); - }; - error = function() { - var currentTime; - currentTime = Math.round(this.currentTime()); - sendbeacon('error', true, currentTime); - }; - fullscreen = function() { - var currentTime; - currentTime = Math.round(this.currentTime()); - if ((typeof this.isFullscreen === "function" ? this.isFullscreen() : void 0) || (typeof this.isFullScreen === "function" ? this.isFullScreen() : void 0)) { - sendbeacon('enter fullscreen', false, currentTime); - } else { - sendbeacon('exit fullscreen', false, currentTime); - } - }; - sendbeacon = function(action, nonInteraction, value) { - if (window.ga) { - ga('send', 'event', { - 'eventCategory': eventCategory, - 'eventAction': action, - 'eventLabel': eventLabel, - 'eventValue': value, - 'nonInteraction': nonInteraction - }); - } else if (window._gaq) { - _gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction]); - } else { - console.log("Google Analytics not detected"); - } - }; - this.ready(function() { - this.on("loadedmetadata", loaded); - this.on("timeupdate", timeupdate); - if (__indexOf.call(eventsToTrack, "end") >= 0) { - this.on("ended", end); - } - if (__indexOf.call(eventsToTrack, "play") >= 0) { - this.on("play", play); - } - if (__indexOf.call(eventsToTrack, "pause") >= 0) { - this.on("pause", pause); - } - if (__indexOf.call(eventsToTrack, "volumeChange") >= 0) { - this.on("volumechange", volumeChange); - } - if (__indexOf.call(eventsToTrack, "resize") >= 0) { - this.on("resize", resize); - } - if (__indexOf.call(eventsToTrack, "error") >= 0) { - this.on("error", error); - } - if (__indexOf.call(eventsToTrack, "fullscreen") >= 0) { - return this.on("fullscreenchange", fullscreen); - } + }; + volumeChange = function() { + var volume; + volume = this.muted() === true ? 0 : this.volume(); + sendbeacon('volume change', false, volume); + }; + resize = function() { + sendbeacon('resize - ' + this.width() + "*" + this.height(), true); + }; + error = function() { + var currentTime; + currentTime = Math.round(this.currentTime()); + sendbeacon('error', true, currentTime); + }; + fullscreen = function() { + var currentTime; + currentTime = Math.round(this.currentTime()); + if ((typeof this.isFullscreen === "function" ? this.isFullscreen() : void 0) || (typeof this.isFullScreen === "function" ? this.isFullScreen() : void 0)) { + sendbeacon('enter fullscreen', false, currentTime); + } else { + sendbeacon('exit fullscreen', false, currentTime); + } + }; + sendbeacon = function(action, nonInteraction, value) { + if (window.ga) { + ga('send', 'event', { + 'eventCategory': eventCategory, + 'eventAction': action, + 'eventLabel': eventLabel, + 'eventValue': value, + 'nonInteraction': nonInteraction + }); + } else if (window._gaq) { + _gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction]); + } else { + console.log("Google Analytics not detected"); + } + }; + this.ready(function() { + this.on("loadedmetadata", loaded); + this.on("timeupdate", timeupdate); + if (__indexOf.call(eventsToTrack, "end") >= 0) { + this.on("ended", end); + } + if (__indexOf.call(eventsToTrack, "play") >= 0) { + this.on("play", play); + } + if (__indexOf.call(eventsToTrack, "pause") >= 0) { + this.on("pause", pause); + } + if (__indexOf.call(eventsToTrack, "volumeChange") >= 0) { + this.on("volumechange", volumeChange); + } + if (__indexOf.call(eventsToTrack, "resize") >= 0) { + this.on("resize", resize); + } + if (__indexOf.call(eventsToTrack, "error") >= 0) { + this.on("error", error); + } + if (__indexOf.call(eventsToTrack, "fullscreen") >= 0) { + return this.on("fullscreenchange", fullscreen); + } + }); }); }); diff --git a/dist/videojs.ga.min.js b/dist/videojs.ga.min.js index d4ff524..2bac430 100644 --- a/dist/videojs.ga.min.js +++ b/dist/videojs.ga.min.js @@ -1,6 +1,6 @@ /* -* videojs-ga - v0.4.1 - 2014-06-06 +* videojs-ga - v0.4.1 - 2014-11-11 * Copyright (c) 2014 Michael Bensoussan * Licensed MIT */ -(function(){var a=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};videojs.plugin("ga",function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;null==b&&(b={}),c={},this.options()["data-setup"]&&(l=JSON.parse(this.options()["data-setup"]),l.ga&&(c=l.ga)),d=["loaded","percentsPlayed","start","end","seek","play","pause","resize","volumeChange","error","fullscreen"],i=b.eventsToTrack||c.eventsToTrack||d,o=b.percentsPlayedInterval||c.percentsPlayedInterval||10,g=b.eventCategory||c.eventCategory||"Video",h=b.eventLabel||c.eventLabel,n=[],s=r=0,t=!1,k=function(){h||(h=this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,"")),a.call(i,"loadedmetadata")>=0&&u("loadedmetadata",!0)},v=function(){var b,c,d,e,f;for(b=Math.round(this.currentTime()),c=Math.round(this.duration()),e=Math.round(b/c*100),d=f=0;99>=f;d=f+=o)e>=d&&a.call(n,d)<0&&(a.call(i,"start")>=0&&0===d&&e>0?u("start",!0):a.call(i,"percentsPlayed")>=0&&0!==e&&u("percent played",!0,d),e>0&&n.push(d));a.call(i,"seek")>=0&&(s=r,r=b,Math.abs(s-r)>1&&(t=!0,u("seek start",!1,s),u("seek end",!1,r)))},e=function(){u("end",!0)},p=function(){var a;a=Math.round(this.currentTime()),u("play",!0,a),t=!1},m=function(){var a,b;a=Math.round(this.currentTime()),b=Math.round(this.duration()),a===b||t||u("pause",!1,a)},w=function(){var a;a=this.muted()===!0?0:this.volume(),u("volume change",!1,a)},q=function(){u("resize - "+this.width()+"*"+this.height(),!0)},f=function(){var a;a=Math.round(this.currentTime()),u("error",!0,a)},j=function(){var a;a=Math.round(this.currentTime()),("function"==typeof this.isFullscreen?this.isFullscreen():void 0)||("function"==typeof this.isFullScreen?this.isFullScreen():void 0)?u("enter fullscreen",!1,a):u("exit fullscreen",!1,a)},u=function(a,b,c){window.ga?ga("send","event",{eventCategory:g,eventAction:a,eventLabel:h,eventValue:c,nonInteraction:b}):window._gaq?_gaq.push(["_trackEvent",g,a,h,c,b]):console.log("Google Analytics not detected")},this.ready(function(){return this.on("loadedmetadata",k),this.on("timeupdate",v),a.call(i,"end")>=0&&this.on("ended",e),a.call(i,"play")>=0&&this.on("play",p),a.call(i,"pause")>=0&&this.on("pause",m),a.call(i,"volumeChange")>=0&&this.on("volumechange",w),a.call(i,"resize")>=0&&this.on("resize",q),a.call(i,"error")>=0&&this.on("error",f),a.call(i,"fullscreen")>=0?this.on("fullscreenchange",j):void 0})})}).call(this); \ No newline at end of file +(function(){var a=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};!function(a){return"object"==typeof exports&&"function"==typeof require?module.exports=a(require("video")):"function"==typeof define&&define.amd?define(["./video"],function(b){return a(b)}):a(window.videojs)}(function(b){return b.plugin("ga",function(b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;null==b&&(b={}),c={},this.options()["data-setup"]&&(l=JSON.parse(this.options()["data-setup"]),l.ga&&(c=l.ga)),d=["loaded","percentsPlayed","start","end","seek","play","pause","resize","volumeChange","error","fullscreen"],i=b.eventsToTrack||c.eventsToTrack||d,o=b.percentsPlayedInterval||c.percentsPlayedInterval||10,g=b.eventCategory||c.eventCategory||"Video",h=b.eventLabel||c.eventLabel,n=[],s=r=0,t=!1,k=function(){h||(h=this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,"")),a.call(i,"loadedmetadata")>=0&&u("loadedmetadata",!0)},v=function(){var b,c,d,e,f;for(b=Math.round(this.currentTime()),c=Math.round(this.duration()),e=Math.round(b/c*100),d=f=0;99>=f;d=f+=o)e>=d&&a.call(n,d)<0&&(a.call(i,"start")>=0&&0===d&&e>0?u("start",!0):a.call(i,"percentsPlayed")>=0&&0!==e&&u("percent played",!0,d),e>0&&n.push(d));a.call(i,"seek")>=0&&(s=r,r=b,Math.abs(s-r)>1&&(t=!0,u("seek start",!1,s),u("seek end",!1,r)))},e=function(){u("end",!0)},p=function(){var a;a=Math.round(this.currentTime()),u("play",!0,a),t=!1},m=function(){var a,b;a=Math.round(this.currentTime()),b=Math.round(this.duration()),a===b||t||u("pause",!1,a)},w=function(){var a;a=this.muted()===!0?0:this.volume(),u("volume change",!1,a)},q=function(){u("resize - "+this.width()+"*"+this.height(),!0)},f=function(){var a;a=Math.round(this.currentTime()),u("error",!0,a)},j=function(){var a;a=Math.round(this.currentTime()),("function"==typeof this.isFullscreen?this.isFullscreen():void 0)||("function"==typeof this.isFullScreen?this.isFullScreen():void 0)?u("enter fullscreen",!1,a):u("exit fullscreen",!1,a)},u=function(a,b,c){window.ga?ga("send","event",{eventCategory:g,eventAction:a,eventLabel:h,eventValue:c,nonInteraction:b}):window._gaq?_gaq.push(["_trackEvent",g,a,h,c,b]):console.log("Google Analytics not detected")},this.ready(function(){return this.on("loadedmetadata",k),this.on("timeupdate",v),a.call(i,"end")>=0&&this.on("ended",e),a.call(i,"play")>=0&&this.on("play",p),a.call(i,"pause")>=0&&this.on("pause",m),a.call(i,"volumeChange")>=0&&this.on("volumechange",w),a.call(i,"resize")>=0&&this.on("resize",q),a.call(i,"error")>=0&&this.on("error",f),a.call(i,"fullscreen")>=0?this.on("fullscreenchange",j):void 0})})})}).call(this); \ No newline at end of file diff --git a/src/videojs.ga.coffee b/src/videojs.ga.coffee index c8dca7a..879a0ef 100644 --- a/src/videojs.ga.coffee +++ b/src/videojs.ga.coffee @@ -5,131 +5,143 @@ # Copyright (c) 2013 Michael Bensoussan # Licensed under the MIT license. ## - -videojs.plugin 'ga', (options = {}) -> - # this loads options from the data-setup attribute of the video tag - dataSetupOptions = {} - if @options()["data-setup"] - parsedOptions = JSON.parse(@options()["data-setup"]) - dataSetupOptions = parsedOptions.ga if parsedOptions.ga - - defaultsEventsToTrack = [ - 'loaded', 'percentsPlayed', 'start', - 'end', 'seek', 'play', 'pause', 'resize', - 'volumeChange', 'error', 'fullscreen' - ] - eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack - percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10 - - eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video' - # if you didn't specify a name, it will be 'guessed' from the video src after metadatas are loaded - eventLabel = options.eventLabel || dataSetupOptions.eventLabel - - # init a few variables - percentsAlreadyTracked = [] - seekStart = seekEnd = 0 - seeking = false - - loaded = -> - unless eventLabel - eventLabel = @currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,'') - - if "loadedmetadata" in eventsToTrack - sendbeacon( 'loadedmetadata', true ) - - return - - timeupdate = -> - currentTime = Math.round(@currentTime()) - duration = Math.round(@duration()) - percentPlayed = Math.round(currentTime/duration*100) - - for percent in [0..99] by percentsPlayedInterval - if percentPlayed >= percent && percent not in percentsAlreadyTracked - - if "start" in eventsToTrack && percent == 0 && percentPlayed > 0 - sendbeacon( 'start', true ) - else if "percentsPlayed" in eventsToTrack && percentPlayed != 0 - sendbeacon( 'percent played', true, percent ) - - if percentPlayed > 0 - percentsAlreadyTracked.push(percent) - - if "seek" in eventsToTrack - seekStart = seekEnd - seekEnd = currentTime - # if the difference between the start and the end are greater than 1 it's a seek. - if Math.abs(seekStart - seekEnd) > 1 - seeking = true - sendbeacon( 'seek start', false, seekStart ) - sendbeacon( 'seek end', false, seekEnd ) - - return - - end = -> - sendbeacon( 'end', true ) - return - - play = -> - currentTime = Math.round(@currentTime()) - sendbeacon( 'play', true, currentTime ) +((factory) -> + if typeof exports is "object" and typeof require is "function" + # CommonJS + module.exports = factory(require("video")) + else if typeof define is "function" and define.amd + # AMD + define ["./video"], (vjs) -> + factory vjs + else + # Browser globals + factory window.videojs +)((videojs) -> + videojs.plugin 'ga', (options = {}) -> + # this loads options from the data-setup attribute of the video tag + dataSetupOptions = {} + if @options()["data-setup"] + parsedOptions = JSON.parse(@options()["data-setup"]) + dataSetupOptions = parsedOptions.ga if parsedOptions.ga + + defaultsEventsToTrack = [ + 'loaded', 'percentsPlayed', 'start', + 'end', 'seek', 'play', 'pause', 'resize', + 'volumeChange', 'error', 'fullscreen' + ] + eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack + percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10 + + eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video' + # if you didn't specify a name, it will be 'guessed' from the video src after metadatas are loaded + eventLabel = options.eventLabel || dataSetupOptions.eventLabel + + # init a few variables + percentsAlreadyTracked = [] + seekStart = seekEnd = 0 seeking = false - return - - pause = -> - currentTime = Math.round(@currentTime()) - duration = Math.round(@duration()) - if currentTime != duration && !seeking - sendbeacon( 'pause', false, currentTime ) - return - - # value between 0 (muted) and 1 - volumeChange = -> - volume = if @muted() == true then 0 else @volume() - sendbeacon( 'volume change', false, volume ) - return - - resize = -> - sendbeacon( 'resize - ' + @width() + "*" + @height(), true ) - return - - error = -> - currentTime = Math.round(@currentTime()) - # XXX: Is there some informations about the error somewhere ? - sendbeacon( 'error', true, currentTime ) - return - fullscreen = -> - currentTime = Math.round(@currentTime()) - if @isFullscreen?() || @isFullScreen?() - sendbeacon( 'enter fullscreen', false, currentTime ) - else - sendbeacon( 'exit fullscreen', false, currentTime ) + loaded = -> + unless eventLabel + eventLabel = @currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,'') + + if "loadedmetadata" in eventsToTrack + sendbeacon( 'loadedmetadata', true ) + + return + + timeupdate = -> + currentTime = Math.round(@currentTime()) + duration = Math.round(@duration()) + percentPlayed = Math.round(currentTime/duration*100) + + for percent in [0..99] by percentsPlayedInterval + if percentPlayed >= percent && percent not in percentsAlreadyTracked + + if "start" in eventsToTrack && percent == 0 && percentPlayed > 0 + sendbeacon( 'start', true ) + else if "percentsPlayed" in eventsToTrack && percentPlayed != 0 + sendbeacon( 'percent played', true, percent ) + + if percentPlayed > 0 + percentsAlreadyTracked.push(percent) + + if "seek" in eventsToTrack + seekStart = seekEnd + seekEnd = currentTime + # if the difference between the start and the end are greater than 1 it's a seek. + if Math.abs(seekStart - seekEnd) > 1 + seeking = true + sendbeacon( 'seek start', false, seekStart ) + sendbeacon( 'seek end', false, seekEnd ) + + return + + end = -> + sendbeacon( 'end', true ) + return + + play = -> + currentTime = Math.round(@currentTime()) + sendbeacon( 'play', true, currentTime ) + seeking = false + return + + pause = -> + currentTime = Math.round(@currentTime()) + duration = Math.round(@duration()) + if currentTime != duration && !seeking + sendbeacon( 'pause', false, currentTime ) + return + + # value between 0 (muted) and 1 + volumeChange = -> + volume = if @muted() == true then 0 else @volume() + sendbeacon( 'volume change', false, volume ) + return + + resize = -> + sendbeacon( 'resize - ' + @width() + "*" + @height(), true ) + return + + error = -> + currentTime = Math.round(@currentTime()) + # XXX: Is there some informations about the error somewhere ? + sendbeacon( 'error', true, currentTime ) + return + + fullscreen = -> + currentTime = Math.round(@currentTime()) + if @isFullscreen?() || @isFullScreen?() + sendbeacon( 'enter fullscreen', false, currentTime ) + else + sendbeacon( 'exit fullscreen', false, currentTime ) + return + + sendbeacon = ( action, nonInteraction, value ) -> + # console.log action, " ", nonInteraction, " ", value + if window.ga + ga 'send', 'event', + 'eventCategory' : eventCategory + 'eventAction' : action + 'eventLabel' : eventLabel + 'eventValue' : value + 'nonInteraction' : nonInteraction + else if window._gaq + _gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction]) + else + console.log("Google Analytics not detected") + return + + @ready -> + @on("loadedmetadata", loaded) + @on("timeupdate", timeupdate) + @on("ended", end) if "end" in eventsToTrack + @on("play", play) if "play" in eventsToTrack + @on("pause", pause) if "pause" in eventsToTrack + @on("volumechange", volumeChange) if "volumeChange" in eventsToTrack + @on("resize", resize) if "resize" in eventsToTrack + @on("error", error) if "error" in eventsToTrack + @on("fullscreenchange", fullscreen) if "fullscreen" in eventsToTrack return - - sendbeacon = ( action, nonInteraction, value ) -> - # console.log action, " ", nonInteraction, " ", value - if window.ga - ga 'send', 'event', - 'eventCategory' : eventCategory - 'eventAction' : action - 'eventLabel' : eventLabel - 'eventValue' : value - 'nonInteraction' : nonInteraction - else if window._gaq - _gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction]) - else - console.log("Google Analytics not detected") - return - - @ready -> - @on("loadedmetadata", loaded) - @on("timeupdate", timeupdate) - @on("ended", end) if "end" in eventsToTrack - @on("play", play) if "play" in eventsToTrack - @on("pause", pause) if "pause" in eventsToTrack - @on("volumechange", volumeChange) if "volumeChange" in eventsToTrack - @on("resize", resize) if "resize" in eventsToTrack - @on("error", error) if "error" in eventsToTrack - @on("fullscreenchange", fullscreen) if "fullscreen" in eventsToTrack - return +) \ No newline at end of file