diff --git a/README.md b/README.md index a99309e..47b804b 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# videojs-ga +# videojs-analytics -Google Analytics plugin for video.js +Google and Piwik Analytics plugin for video.js ## Getting Started -Download [videojs](http://www.videojs.com/) and [videojs.ga](https://github.com/mickey/videojs-ga) +Download [videojs](http://www.videojs.com/) and [videojs-analytics](https://github.com/azine/videojs-analytics) In your web page: ```html @@ -17,7 +17,7 @@ videojs('video', {}, function() { ``` -_Please note that the Google Analytics script must be loaded before the ga plugin_ +_Please note that the Google Analytics and/or Piwik script must be loaded before the ga plugin_ ## Options @@ -70,12 +70,16 @@ This options goes with the ```percentsPlayed``` event. Every ```percentsPlayedIn If set to false, console logs will be ommited **default:** ```false``` -#### ga.js and analytics.js +#### ga.js, analytics.js and piwik.js -This plugin supports the ga.js and the newer analytics.js Google Analytics libraries. It autodetects the library you use. +This plugin supports the ga.js and the newer analytics.js Google Analytics libraries. It also supports piwik.js. +It autodetects the library you use and if both GoogleAnalytics and Piwik is used by your site, it will log to both systems. ## TODO - [x] track the engine used (html5/flash) along with the source (ogg, mp4, ...) - [ ] track the time to download the video +- [ ] refactor source-code, docs and tests to replace references to "GA" with a more general term that makes sense for GoogleAnalytics and for Piwik. + + diff --git a/demo/index.html b/demo/index.html index 648af8b..887af45 100644 --- a/demo/index.html +++ b/demo/index.html @@ -19,6 +19,7 @@ this.ga(); }); + /** load GoogleAnalytics */ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) @@ -26,6 +27,20 @@ ga('create', 'UA-XXXX-Y', { 'cookieDomain': 'none' }); ga('send', 'pageview'); + + /** load piwik */ + var _paq = _paq || []; + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + (function() { + var host = window.location.hostname; + var u=(("https:" == document.location.protocol) ? "https" : "http") + "://"+host+"/piwik/"; + _paq.push(['setTrackerUrl', u+'piwik.php']); + _paq.push(['setSiteId', 1]); + var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; + g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); + })(); + diff --git a/dist/videojs.ga.js b/dist/videojs.ga.js index dbda0a2..e5c503f 100644 --- a/dist/videojs.ga.js +++ b/dist/videojs.ga.js @@ -1,5 +1,5 @@ /* -* videojs-ga - v0.4.2 - 2015-02-06 +* videojs-ga - v0.4.2 - 2015-09-14 * Copyright (c) 2015 Michael Bensoussan * Licensed MIT */ @@ -115,6 +115,11 @@ } else if (options.debug) { console.log("Google Analytics not detected"); } + if (window._paq) { + _paq.push(['trackEvent', eventCategory, action, eventLabel, value]); + } else if (options.debug) { + console.log("Piwik not detected"); + } }; this.ready(function() { this.on("loadedmetadata", loaded); diff --git a/dist/videojs.ga.min.js b/dist/videojs.ga.min.js index b57c784..55831b7 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 +* videojs-ga - v0.4.2 - 2015-09-14 * Copyright (c) 2015 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;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"),window._paq?_paq.push(["trackEvent",g,a,h,d]):b.debug&&console.log("Piwik 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 diff --git a/src/videojs.ga.coffee b/src/videojs.ga.coffee index 495d4bb..19509e7 100644 --- a/src/videojs.ga.coffee +++ b/src/videojs.ga.coffee @@ -123,6 +123,12 @@ videojs.plugin 'ga', (options = {}) -> _gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction]) else if options.debug console.log("Google Analytics not detected") + + if window._paq + _paq.push(['trackEvent', eventCategory, action, eventLabel, value]) + else if options.debug + console.log("Piwik not detected") + return @ready ->