From 63e417de699ad5490363e7d53feb49d234be8994 Mon Sep 17 00:00:00 2001 From: Chad Harter Date: Tue, 22 Nov 2016 09:34:19 -0500 Subject: [PATCH] GA commands now prefix their command name with the name of the GA tracker to handle cases when developers assign specific names to their trackers --- README.md | 6 +++++ dist/videojs.ga.videocloud.js | 37 ++++++++++++++++++++++++++----- dist/videojs.ga.videocloud.min.js | 6 ++--- package.json | 2 +- src/videojs.ga.coffee | 27 ++++++++++++++++++++-- 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 049f813..1ab8df4 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,12 @@ If set, this tracker code will be used for iframe embeds and the direct player U **default:** Not set +####trackerName + +If set, the tracker with this name will be used for in-page embeds. This should match the ```name``` field sent in the GA ```create``` command for the tracker you want associated with the player. If you don't know what it is please check [GA's doc](https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers) + +**default:** Not set + ####eventNames Override or localise the names of the event actions. diff --git a/dist/videojs.ga.videocloud.js b/dist/videojs.ga.videocloud.js index f65cd0a..6507a78 100644 --- a/dist/videojs.ga.videocloud.js +++ b/dist/videojs.ga.videocloud.js @@ -1,13 +1,13 @@ /* -* videojs-ga - v0.4.1 - 2015-08-10 -* Copyright (c) 2015 Michael Bensoussan +* videojs-ga - v0.4.2 - 2016-11-22 +* 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 adStateRegex, currentVideo, dataSetupOptions, defaultLabel, defaultsEventsToTrack, end, endTracked, error, eventCategory, eventLabel, eventNames, eventsToTrack, fullscreen, getEventName, href, iframe, isInAdState, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, player, referrer, resize, seekEnd, seekStart, seeking, sendbeacon, sendbeaconOverride, start, startTracked, timeupdate, tracker, volumeChange, + var adStateRegex, currentVideo, dataSetupOptions, defaultLabel, defaultsEventsToTrack, end, endTracked, error, eventCategory, eventLabel, eventNames, eventsToTrack, fullscreen, getCommandName, getEventName, getTrackerName, href, iframe, isInAdState, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, player, referrer, resize, seekEnd, seekStart, seeking, sendbeacon, sendbeaconOverride, start, startTracked, timeupdate, tracker, volumeChange, _this = this; if (options == null) { options = {}; @@ -67,6 +67,33 @@ } return name; }; + getCommandName = function(name) { + var trackerName; + if (!window.ga || name === 'create') { + return name; + } + trackerName = getTrackerName(); + if (trackerName) { + return trackerName + '.' + name; + } + return name; + }; + getTrackerName = function() { + var trackers; + if (options.trackerName) { + return options.trackerName; + } + if (dataSetupOptions.trackerName) { + return dataSetupOptions.trackerName; + } + if (window.ga && window.ga.getAll) { + trackers = ga.getAll(); + if (trackers.length > 0) { + return trackers[0].get('name'); + } + } + return null; + }; if (window.location.host === 'players.brightcove.net' || window.location.host === 'preview-players.brightcove.net') { tracker = options.tracker || dataSetupOptions.tracker; if (tracker) { @@ -199,7 +226,7 @@ if (sendbeaconOverride) { sendbeaconOverride(eventCategory, action, eventLabel, value, nonInteraction); } else if (window.ga) { - ga('send', 'event', { + ga(getCommandName('send'), 'event', { 'eventCategory': eventCategory, 'eventAction': action, 'eventLabel': eventLabel, @@ -224,7 +251,7 @@ if (sendbeaconOverride) { sendbeaconOverride(eventCategory, getEventName('player_load'), href, iframe, true); } else if (window.ga) { - ga('send', 'event', { + ga(getCommandName('send'), 'event', { 'eventCategory': eventCategory, 'eventAction': getEventName('player_load'), 'eventLabel': href, diff --git a/dist/videojs.ga.videocloud.min.js b/dist/videojs.ga.videocloud.min.js index dc96646..f545b18 100644 --- a/dist/videojs.ga.videocloud.min.js +++ b/dist/videojs.ga.videocloud.min.js @@ -1,6 +1,6 @@ /* -* videojs-ga - v0.4.1 - 2015-08-10 -* Copyright (c) 2015 Michael Bensoussan +* videojs-ga - v0.4.2 - 2016-11-22 +* 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,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L;return null==b&&(b={}),A=document.createElement("a"),A.href=document.referrer,self!==top&&"preview-players.brightcove.net"===window.location.host&&(A.hostname="studio.brightcove.com")?void videojs.log("Google analytics plugin will not track events in Video Cloud Studio"):(z=this,e={},this.options()["data-setup"]&&(u=JSON.parse(this.options()["data-setup"]),u.ga&&(e=u.ga)),g=["player_load","video_load","percent_played","start","end","seek","play","pause","resize","volume_change","error","fullscreen"],n=b.eventsToTrack||e.eventsToTrack||g,x=b.percentsPlayedInterval||e.percentsPlayedInterval||10,k=b.eventCategory||e.eventCategory||"Brightcove Player",f=b.eventLabel||e.eventLabel,G=b.sendbeaconOverride||!1,w=[],I=!1,i=!1,D=C=0,E=!1,l="",d="",m={video_load:"Video Load",percent_played:"Percent played",start:"Media Begin",seek_start:"Seek start",seek_end:"Seek end",play:"Media Play",pause:"Media Pause",error:"Error",fullscreen_exit:"Fullscreen Exited",fullscreen_enter:"Fullscreen Entered",resize:"Resize",volume_change:"Volume Change",player_load:"Player Load",end:"Media Complete"},p=function(a){return b.eventNames&&b.eventNames[a]?b.eventNames[a]:e.eventNames&&e.eventNames[a]?e.eventNames[a]:m[a]?m[a]:a},("players.brightcove.net"===window.location.host||"preview-players.brightcove.net"===window.location.host)&&(K=b.tracker||e.tracker,K&&(!function(a,b,c,d,e,f,g){return a.GoogleAnalyticsObject=e,a[e]=a[e]||function(){return(a[e].q=a[e].q||[]).push(arguments)},a[e].l=1*new Date,f=b.createElement(c),g=b.getElementsByTagName(c)[0],f.async=1,f.src=d,g.parentNode.insertBefore(f,g)}(window,document,"script","//www.google-analytics.com/analytics.js","ga"),ga("create",K,"auto"),ga("require","displayfeatures"))),c=/(\s|^)vjs-ad-(playing|loading)(\s|$)/,s=function(a){return c.test(a.el().className)},t=function(){s(z)||(l=f?f:z.mediainfo&&z.mediainfo.id?z.mediainfo.id+" | "+z.mediainfo.name:this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,""),z.mediainfo&&z.mediainfo.id&&z.mediainfo.id!==d&&(d=z.mediainfo.id,w=[],I=!1,i=!1,D=C=0,E=!1,a.call(n,"video_load")>=0&&F(p("video_load"),!0)))},J=function(){var b,c,d,e,f;if(!s(z)){for(b=Math.round(this.currentTime()),c=Math.round(this.duration()),e=Math.round(b/c*100),d=f=0;99>=f;d=f+=x)e>=d&&a.call(w,d)<0&&(a.call(n,"percent_played")>=0&&0!==e&&F(p("percent_played"),!0,d),e>0&&w.push(d));a.call(n,"seek")>=0&&(D=C,C=b,Math.abs(D-C)>1&&(E=!0,F(p("seek_start"),!1,D),F(p("seek_end"),!1,C)))}},h=function(){s(z)||i||(F(p("end"),!0),i=!0)},y=function(){var a;s(z)||(a=Math.round(this.currentTime()),F(p("play"),!0,a),E=!1)},H=function(){return!s(z)&&a.call(n,"start")>=0&&!I?(F(p("start"),!0),I=!0):void 0},v=function(){var a,b;s(z)||(a=Math.round(this.currentTime()),b=Math.round(this.duration()),a===b||E||F(p("pause"),!0,a))},L=function(){var a;a=this.muted()===!0?0:this.volume(),F(p("volume_change"),!1,a)},B=function(){F(p("resize")+" - "+this.width()+"*"+this.height(),!0)},j=function(){var a;a=Math.round(this.currentTime()),F(p("error"),!0,a)},o=function(){var a;a=Math.round(this.currentTime()),("function"==typeof this.isFullscreen?this.isFullscreen():void 0)||("function"==typeof this.isFullScreen?this.isFullScreen():void 0)?F(p("fullscreen_enter"),!1,a):F(p("fullscreen_exit"),!1,a)},F=function(a,b,c){G?G(k,a,l,c,b):window.ga?ga("send","event",{eventCategory:k,eventAction:a,eventLabel:l,eventValue:c,nonInteraction:b}):window._gaq?_gaq.push(["_trackEvent",k,a,l,c,b]):videojs.log("Google Analytics not detected")},a.call(n,"player_load")>=0&&(self!==top?(q=document.referrer,r=1):(q=window.location.href,r=0)),G?G(k,p("player_load"),q,r,!0):window.ga?ga("send","event",{eventCategory:k,eventAction:p("player_load"),eventLabel:q,eventValue:r,nonInteraction:!0}):window._gaq?_gaq.push(["_trackEvent",k,p("player_load"),q,r,!1]):videojs.log("Google Analytics not detected"),void this.ready(function(){return this.on("loadedmetadata",t),this.on("timeupdate",J),a.call(n,"end")>=0&&this.on("ended",h),a.call(n,"play")>=0&&this.on("play",y),a.call(n,"start")>=0&&this.on("playing",H),a.call(n,"pause")>=0&&this.on("pause",v),a.call(n,"volume_change")>=0&&this.on("volumechange",L),a.call(n,"resize")>=0&&this.on("resize",B),a.call(n,"error")>=0&&this.on("error",j),a.call(n,"fullscreen")>=0?this.on("fullscreenchange",o):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};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,B,C,D,E,F,G,H,I,J,K,L,M,N;return null==b&&(b={}),C=document.createElement("a"),C.href=document.referrer,self!==top&&"preview-players.brightcove.net"===window.location.host&&(C.hostname="studio.brightcove.com")?void videojs.log("Google analytics plugin will not track events in Video Cloud Studio"):(B=this,e={},this.options()["data-setup"]&&(w=JSON.parse(this.options()["data-setup"]),w.ga&&(e=w.ga)),g=["player_load","video_load","percent_played","start","end","seek","play","pause","resize","volume_change","error","fullscreen"],n=b.eventsToTrack||e.eventsToTrack||g,z=b.percentsPlayedInterval||e.percentsPlayedInterval||10,k=b.eventCategory||e.eventCategory||"Brightcove Player",f=b.eventLabel||e.eventLabel,I=b.sendbeaconOverride||!1,y=[],K=!1,i=!1,F=E=0,G=!1,l="",d="",m={video_load:"Video Load",percent_played:"Percent played",start:"Media Begin",seek_start:"Seek start",seek_end:"Seek end",play:"Media Play",pause:"Media Pause",error:"Error",fullscreen_exit:"Fullscreen Exited",fullscreen_enter:"Fullscreen Entered",resize:"Resize",volume_change:"Volume Change",player_load:"Player Load",end:"Media Complete"},q=function(a){return b.eventNames&&b.eventNames[a]?b.eventNames[a]:e.eventNames&&e.eventNames[a]?e.eventNames[a]:m[a]?m[a]:a},p=function(a){var b;return window.ga&&"create"!==a?(b=r(),b?b+"."+a:a):a},r=function(){var a;return b.trackerName?b.trackerName:e.trackerName?e.trackerName:window.ga&&window.ga.getAll&&(a=ga.getAll(),a.length>0)?a[0].get("name"):null},("players.brightcove.net"===window.location.host||"preview-players.brightcove.net"===window.location.host)&&(M=b.tracker||e.tracker,M&&(!function(a,b,c,d,e,f,g){return a.GoogleAnalyticsObject=e,a[e]=a[e]||function(){return(a[e].q=a[e].q||[]).push(arguments)},a[e].l=1*new Date,f=b.createElement(c),g=b.getElementsByTagName(c)[0],f.async=1,f.src=d,g.parentNode.insertBefore(f,g)}(window,document,"script","//www.google-analytics.com/analytics.js","ga"),ga("create",M,"auto"),ga("require","displayfeatures"))),c=/(\s|^)vjs-ad-(playing|loading)(\s|$)/,u=function(a){return c.test(a.el().className)},v=function(){u(B)||(l=f?f:B.mediainfo&&B.mediainfo.id?B.mediainfo.id+" | "+B.mediainfo.name:this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i,""),B.mediainfo&&B.mediainfo.id&&B.mediainfo.id!==d&&(d=B.mediainfo.id,y=[],K=!1,i=!1,F=E=0,G=!1,a.call(n,"video_load")>=0&&H(q("video_load"),!0)))},L=function(){var b,c,d,e,f;if(!u(B)){for(b=Math.round(this.currentTime()),c=Math.round(this.duration()),e=Math.round(b/c*100),d=f=0;99>=f;d=f+=z)e>=d&&a.call(y,d)<0&&(a.call(n,"percent_played")>=0&&0!==e&&H(q("percent_played"),!0,d),e>0&&y.push(d));a.call(n,"seek")>=0&&(F=E,E=b,Math.abs(F-E)>1&&(G=!0,H(q("seek_start"),!1,F),H(q("seek_end"),!1,E)))}},h=function(){u(B)||i||(H(q("end"),!0),i=!0)},A=function(){var a;u(B)||(a=Math.round(this.currentTime()),H(q("play"),!0,a),G=!1)},J=function(){return!u(B)&&a.call(n,"start")>=0&&!K?(H(q("start"),!0),K=!0):void 0},x=function(){var a,b;u(B)||(a=Math.round(this.currentTime()),b=Math.round(this.duration()),a===b||G||H(q("pause"),!0,a))},N=function(){var a;a=this.muted()===!0?0:this.volume(),H(q("volume_change"),!1,a)},D=function(){H(q("resize")+" - "+this.width()+"*"+this.height(),!0)},j=function(){var a;a=Math.round(this.currentTime()),H(q("error"),!0,a)},o=function(){var a;a=Math.round(this.currentTime()),("function"==typeof this.isFullscreen?this.isFullscreen():void 0)||("function"==typeof this.isFullScreen?this.isFullScreen():void 0)?H(q("fullscreen_enter"),!1,a):H(q("fullscreen_exit"),!1,a)},H=function(a,b,c){I?I(k,a,l,c,b):window.ga?ga(p("send"),"event",{eventCategory:k,eventAction:a,eventLabel:l,eventValue:c,nonInteraction:b}):window._gaq?_gaq.push(["_trackEvent",k,a,l,c,b]):videojs.log("Google Analytics not detected")},a.call(n,"player_load")>=0&&(self!==top?(s=document.referrer,t=1):(s=window.location.href,t=0)),I?I(k,q("player_load"),s,t,!0):window.ga?ga(p("send"),"event",{eventCategory:k,eventAction:q("player_load"),eventLabel:s,eventValue:t,nonInteraction:!0}):window._gaq?_gaq.push(["_trackEvent",k,q("player_load"),s,t,!1]):videojs.log("Google Analytics not detected"),void this.ready(function(){return this.on("loadedmetadata",v),this.on("timeupdate",L),a.call(n,"end")>=0&&this.on("ended",h),a.call(n,"play")>=0&&this.on("play",A),a.call(n,"start")>=0&&this.on("playing",J),a.call(n,"pause")>=0&&this.on("pause",x),a.call(n,"volume_change")>=0&&this.on("volumechange",N),a.call(n,"resize")>=0&&this.on("resize",D),a.call(n,"error")>=0&&this.on("error",j),a.call(n,"fullscreen")>=0?this.on("fullscreenchange",o):void 0}))})}).call(this); \ No newline at end of file diff --git a/package.json b/package.json index 264a221..3ca1fd4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "videojs-ga", - "version": "0.4.1", + "version": "0.4.2", "license": "MIT", "author": { "name": "Michael Bensoussan", diff --git a/src/videojs.ga.coffee b/src/videojs.ga.coffee index bc45f6f..d847b53 100644 --- a/src/videojs.ga.coffee +++ b/src/videojs.ga.coffee @@ -72,6 +72,29 @@ videojs.plugin 'ga', (options = {}) -> return eventNames[name] return name + getCommandName = ( name ) -> + if !window.ga || name == 'create' + return name + + trackerName = getTrackerName() + if trackerName + return trackerName + '.' + name + + return name + + getTrackerName = () -> + if options.trackerName + return options.trackerName + if dataSetupOptions.trackerName + return dataSetupOptions.trackerName + + if window.ga && window.ga.getAll + trackers = ga.getAll() + if trackers.length > 0 + return trackers[0].get('name') + + return null + # load ga script if in iframe and tracker option is set if window.location.host == 'players.brightcove.net' || window.location.host == 'preview-players.brightcove.net' tracker = options.tracker || dataSetupOptions.tracker @@ -203,7 +226,7 @@ videojs.plugin 'ga', (options = {}) -> if sendbeaconOverride sendbeaconOverride(eventCategory, action, eventLabel, value, nonInteraction) else if window.ga - ga 'send', 'event', + ga getCommandName('send'), 'event', 'eventCategory' : eventCategory 'eventAction' : action 'eventLabel' : eventLabel @@ -226,7 +249,7 @@ videojs.plugin 'ga', (options = {}) -> if sendbeaconOverride sendbeaconOverride(eventCategory, getEventName('player_load'), href, iframe, true) else if window.ga - ga 'send', 'event', + ga getCommandName('send'), 'event', 'eventCategory' : eventCategory 'eventAction' : getEventName('player_load') 'eventLabel' : href