UNPKG

ga-plugin

Version:

Google Analytics plugin for Brightcove Player in Video Cloud

8 lines 4.76 kB
/* * ga-plugin - v0.5.4 - 2019-01-31 * Based on videojs-ga 0.4.2 * https://github.com/BrightcoveOS/videojs-ga-videocloud * Copyright (c) 2019 Michael Bensoussan * Licensed MIT */ (function(){var $=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};(videojs.registerPlugin||videojs.plugin)("ga",function(a){var t,e,n,r,i,o,l,s,c,d,u,h,v,g,p,f,m,y,_,w,k,b,N,M,T,j,E,P,q,C,z,L,A,S,V,x,B,G,I,O,F;if(null==a&&(a={}),(N=document.createElement("a")).href=document.referrer,self===top||"preview-players.brightcove.net"!==window.location.host||"studio.brightcove.com"!==N.hostname)return n={},(b=this).options_["data-setup"]&&(m=JSON.parse(this.options()["data-setup"])).ga&&(n=m.ga),i=["player_load","video_load","percent_played","start","end","seek","play","pause","resize","volume_change","error","fullscreen"],h=a.eventsToTrack||n.eventsToTrack||i,w=a.percentsPlayedInterval||n.percentsPlayedInterval||10,c=a.eventCategory||n.eventCategory||"Brightcove Player",r=a.eventLabel||n.eventLabel,q=a.sendbeaconOverride||!1,a.debug=a.debug||!1,a.trackerName=a.trackerName||null,S="","string"==typeof a.trackerName&&(S=a.trackerName+"."),j=T=0,E=l=z=!(_=[]),e=d="",u={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"},g=function(e){return a.eventNames&&a.eventNames[e]?a.eventNames[e]:n.eventNames&&n.eventNames[e]?n.eventNames[e]:u[e]?u[e]:e},"players.brightcove.net"!==window.location.host&&"preview-players.brightcove.net"!==window.location.host&&""===S||(A=a.tracker||n.tracker)&&(x=window,B=document,G="script",I="ga",x.GoogleAnalyticsObject=I,x.ga=x.ga||function(){return(x.ga.q=x.ga.q||[]).push(arguments)},x.ga.l=1*new Date,O=B.createElement(G),F=B.getElementsByTagName(G)[0],O.async=1,O.src="//www.google-analytics.com/analytics.js",F.parentNode.insertBefore(O,F),ga("create",A,"auto",a.trackerName),ga(S+"require","displayfeatures")),t=/(\s|^)vjs-ad-(playing|loading)(\s|$)/,p=function(e){return t.test(e.el().className)},f=function(){p(b)||(d=r||(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!==e&&(e=b.mediainfo.id,j=T=0,E=l=z=!(_=[]),0<=$.call(h,"video_load")&&P(g("video_load"),!0)))},L=function(){var e,t,n,a,r;if(!p(b)){for(e=Math.round(this.currentTime()),t=Math.round(this.duration()),a=Math.round(e/t*100),n=r=0;r<=99;n=r+=w)n<=a&&$.call(_,n)<0&&(0<=$.call(h,"percent_played")&&0!==a&&P(g("percent_played"),!0,n),0<a&&_.push(n));0<=$.call(h,"seek")&&(j=T,T=e,1<Math.abs(j-T)&&(E=!0,P(g("seek_start"),!1,j),P(g("seek_end"),!1,T)))}},o=function(){p(b)||l||(P(g("end"),!0),l=!0)},k=function(){var e;p(b)||(e=Math.round(this.currentTime()),P(g("play"),!0,e),E=!1)},C=function(){if(!p(b)&&0<=$.call(h,"start")&&!z)return P(g("start"),!0),z=!0},y=function(){var e;p(b)||(e=Math.round(this.currentTime()))===Math.round(this.duration())||E||P(g("pause"),!0,e)},V=function(){var e;e=!0===this.muted()?0:this.volume(),P(g("volume_change"),!1,e)},M=function(){P(g("resize")+" - "+this.width()+"*"+this.height(),!0)},s=function(){var e;e=Math.round(this.currentTime()),P(g("error"),!0,e)},v=function(){var e;e=Math.round(this.currentTime()),this.isFullscreen()?P(g("fullscreen_enter"),!1,e):P(g("fullscreen_exit"),!1,e)},P=function(e,t,n){q?q(c,e,d,n,t):window.ga?ga(S+"send","event",{eventCategory:c,eventAction:e,eventLabel:d,eventValue:n,nonInteraction:t}):window._gaq?_gaq.push(["_trackEvent",c,e,d,n,t]):a.debug&&videojs.log("Google Analytics not detected")},this.ready(function(){var e,t;if(this.on("loadedmetadata",f),this.on("timeupdate",L),0<=$.call(h,"end")&&this.on("ended",o),0<=$.call(h,"play")&&this.on("play",k),0<=$.call(h,"start")&&this.on("playing",C),0<=$.call(h,"pause")&&this.on("pause",y),0<=$.call(h,"volume_change")&&this.on("volumechange",V),0<=$.call(h,"resize")&&this.on("resize",M),0<=$.call(h,"error")&&this.on("error",s),0<=$.call(h,"fullscreen")&&this.on("fullscreenchange",v),0<=$.call(h,"player_load"))return t=self!==top?(e=document.referrer,1):(e=window.location.href,0),q?q(c,g("player_load"),e,t,!0):window.ga?ga(S+"send","event",{eventCategory:c,eventAction:g("player_load"),eventLabel:e,eventValue:t,nonInteraction:!0}):window._gaq?_gaq.push(["_trackEvent",c,g("player_load"),e,t,!1]):videojs.log("Google Analytics not detected")}),{sendbeacon:P};videojs.log("Google analytics plugin will not track events in Video Cloud Studio")})}).call(this);