diff --git a/dist/videojs.ga.js b/dist/videojs.ga.js index dbda0a2..f0c9807 100644 --- a/dist/videojs.ga.js +++ b/dist/videojs.ga.js @@ -1,13 +1,13 @@ /* -* videojs-ga - v0.4.2 - 2015-02-06 -* Copyright (c) 2015 Michael Bensoussan +* videojs-ga - v0.4.2 - 2016-02-10 +* Copyright (c) 2016 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; + var dataSetupOptions, defaultsEventsToTrack, disabletracking, enabletracking, end, error, eventCategory, eventLabel, eventsToTrack, fullscreen, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, resize, seekEnd, seekStart, seeking, sendbeacon, setoptions, timeupdate, tracking, volumeChange; if (options == null) { options = {}; } @@ -19,14 +19,22 @@ } } 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; - options.debug = options.debug || false; + eventsToTrack = null; + percentsPlayedInterval = null; + eventCategory = null; + eventLabel = null; percentsAlreadyTracked = []; seekStart = seekEnd = 0; seeking = false; + tracking = true; + setoptions = function(newoptions) { + options = newoptions; + eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack; + percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10; + eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video'; + eventLabel = options.eventLabel || dataSetupOptions.eventLabel; + return options.debug = options.debug || false; + }; loaded = function() { if (!eventLabel) { eventLabel = this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i, ''); @@ -102,6 +110,9 @@ } }; sendbeacon = function(action, nonInteraction, value) { + if (!tracking) { + return; + } if (window.ga) { ga('send', 'event', { 'eventCategory': eventCategory, @@ -116,6 +127,12 @@ console.log("Google Analytics not detected"); } }; + disabletracking = function() { + return tracking = false; + }; + enabletracking = function() { + return tracking = true; + }; this.ready(function() { this.on("loadedmetadata", loaded); this.on("timeupdate", timeupdate); @@ -141,8 +158,12 @@ return this.on("fullscreenchange", fullscreen); } }); + setoptions(options); return { - 'sendbeacon': sendbeacon + 'sendbeacon': sendbeacon, + 'setoptions': setoptions, + 'enabletracking': enabletracking, + 'disabletracking': disabletracking }; }); diff --git a/dist/videojs.ga.min.js b/dist/videojs.ga.min.js index b57c784..fb104d2 100644 --- a/dist/videojs.ga.min.js +++ b/dist/videojs.ga.min.js @@ -1,6 +1,6 @@ /* -* videojs-ga - v0.4.2 - 2015-02-06 -* Copyright (c) 2015 Michael Bensoussan +* videojs-ga - v0.4.2 - 2016-02-10 +* Copyright (c) 2016 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;return 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,b.debug=b.debug||!1,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,c,d){window.ga?ga("send","event",{eventCategory:g,eventAction:a,eventLabel:h,eventValue:d,nonInteraction:c}):window._gaq?_gaq.push(["_trackEvent",g,a,h,d,c]):b.debug&&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}),{sendbeacon:u}})}).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};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,x,y,z,A;return null==b&&(b={}),c={},this.options()["data-setup"]&&(n=JSON.parse(this.options()["data-setup"]),n.ga&&(c=n.ga)),d=["loaded","percentsPlayed","start","end","seek","play","pause","resize","volumeChange","error","fullscreen"],k=null,q=null,i=null,j=null,p=[],u=t=0,v=!1,z=!0,x=function(a){return b=a,k=b.eventsToTrack||c.eventsToTrack||d,q=b.percentsPlayedInterval||c.percentsPlayedInterval||10,i=b.eventCategory||c.eventCategory||"Video",j=b.eventLabel||c.eventLabel,b.debug=b.debug||!1},m=function(){j||(j=this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,"")),a.call(k,"loadedmetadata")>=0&&w("loadedmetadata",!0)},y=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+=q)e>=d&&a.call(p,d)<0&&(a.call(k,"start")>=0&&0===d&&e>0?w("start",!0):a.call(k,"percentsPlayed")>=0&&0!==e&&w("percent played",!0,d),e>0&&p.push(d));a.call(k,"seek")>=0&&(u=t,t=b,Math.abs(u-t)>1&&(v=!0,w("seek start",!1,u),w("seek end",!1,t)))},g=function(){w("end",!0)},r=function(){var a;a=Math.round(this.currentTime()),w("play",!0,a),v=!1},o=function(){var a,b;a=Math.round(this.currentTime()),b=Math.round(this.duration()),a===b||v||w("pause",!1,a)},A=function(){var a;a=this.muted()===!0?0:this.volume(),w("volume change",!1,a)},s=function(){w("resize - "+this.width()+"*"+this.height(),!0)},h=function(){var a;a=Math.round(this.currentTime()),w("error",!0,a)},l=function(){var a;a=Math.round(this.currentTime()),("function"==typeof this.isFullscreen?this.isFullscreen():void 0)||("function"==typeof this.isFullScreen?this.isFullScreen():void 0)?w("enter fullscreen",!1,a):w("exit fullscreen",!1,a)},w=function(a,c,d){z&&(window.ga?ga("send","event",{eventCategory:i,eventAction:a,eventLabel:j,eventValue:d,nonInteraction:c}):window._gaq?_gaq.push(["_trackEvent",i,a,j,d,c]):b.debug&&console.log("Google Analytics not detected"))},e=function(){return z=!1},f=function(){return z=!0},this.ready(function(){return this.on("loadedmetadata",m),this.on("timeupdate",y),a.call(k,"end")>=0&&this.on("ended",g),a.call(k,"play")>=0&&this.on("play",r),a.call(k,"pause")>=0&&this.on("pause",o),a.call(k,"volumeChange")>=0&&this.on("volumechange",A),a.call(k,"resize")>=0&&this.on("resize",s),a.call(k,"error")>=0&&this.on("error",h),a.call(k,"fullscreen")>=0?this.on("fullscreenchange",l):void 0}),x(b),{sendbeacon:w,setoptions:x,enabletracking:f,disabletracking:e}})}).call(this); \ No newline at end of file diff --git a/src/videojs.ga.coffee b/src/videojs.ga.coffee index 495d4bb..d5678c9 100644 --- a/src/videojs.ga.coffee +++ b/src/videojs.ga.coffee @@ -18,20 +18,29 @@ videojs.plugin 'ga', (options = {}) -> '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 - - # if debug isn't specified - options.debug = options.debug || false + eventsToTrack = null + percentsPlayedInterval = null + eventCategory = null + eventLabel = null # init a few variables percentsAlreadyTracked = [] seekStart = seekEnd = 0 seeking = false + tracking = true + + setoptions = (newoptions) -> + options = newoptions + 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 + + # if debug isn't specified + options.debug = options.debug || false loaded = -> unless eventLabel @@ -111,20 +120,29 @@ videojs.plugin 'ga', (options = {}) -> return sendbeacon = ( action, nonInteraction, value ) -> + unless tracking + return + # console.log action, " ", nonInteraction, " ", value if window.ga ga 'send', 'event', - 'eventCategory' : eventCategory - 'eventAction' : action - 'eventLabel' : eventLabel + 'eventCategory' : eventCategory + 'eventAction' : action + 'eventLabel' : eventLabel 'eventValue' : value - 'nonInteraction' : nonInteraction + 'nonInteraction' : nonInteraction else if window._gaq _gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction]) else if options.debug console.log("Google Analytics not detected") return + disabletracking = -> + tracking = false + + enabletracking = -> + tracking = true + @ready -> @on("loadedmetadata", loaded) @on("timeupdate", timeupdate) @@ -136,4 +154,11 @@ videojs.plugin 'ga', (options = {}) -> @on("error", error) if "error" in eventsToTrack @on("fullscreenchange", fullscreen) if "fullscreen" in eventsToTrack - return 'sendbeacon': sendbeacon + setoptions(options) + + return { + 'sendbeacon': sendbeacon + 'setoptions': setoptions + 'enabletracking': enabletracking + 'disabletracking': disabletracking + }