forked from sanderheilbron/vimeo.ga.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimeo.ga.min.js
8 lines (7 loc) · 3.79 KB
/
vimeo.ga.min.js
1
2
3
4
5
6
7
8
/*!
* vimeo.ga.js | v0.5
* Based on modifications by LukasBeaton (https://github.com/LukasBeaton/vimeo.ga.js)
* Copyright (c) 2014 Sander Heilbron (http://www.sanderheilbron.nl)
* MIT licensed
*/
var vimeoGAJS={iframes:[],gaTracker:undefined,eventMarker:{},init:function(){vimeoGAJS.iframes=$("iframe");$.each(vimeoGAJS.iframes,function(e,t){var n=$(t).attr("id");vimeoGAJS.eventMarker[n]={progress25:false,progress50:false,progress75:false,videoPlayed:false,videoPaused:false,videoResumed:false,videoSeeking:false,videoCompleted:false,timePercentComplete:0}});if(typeof ga==="function"){vimeoGAJS.gaTracker="ua"}if(typeof _gaq!=="undefined"&&typeof _gaq.push==="function"){vimeoGAJS.gaTracker="ga"}if(typeof dataLayer!=="undefined"&&typeof dataLayer.push==="function"){vimeoGAJS.gaTracker="gtm"}if(window.addEventListener){window.addEventListener("message",vimeoGAJS.onMessageReceived,false)}else{window.attachEvent("onmessage",vimeoGAJS.onMessageReceived,false)}},onMessageReceived:function(e){if(e.origin.replace("https:","http:")!=="http://player.vimeo.com"||typeof vimeoGAJS.gaTracker==="undefined"){return}var t=JSON.parse(e.data),n=$("#"+t.player_id),r=n.attr("id");switch(t.event){case"ready":vimeoGAJS.onReady();break;case"playProgress":vimeoGAJS.onPlayProgress(t.data,n);break;case"seek":if(n.data("seek")&&!vimeoGAJS.eventMarker[r].videoSeeking){vimeoGAJS.sendEvent(n,"Skipped video forward or backward");vimeoGAJS.eventMarker[r].videoSeeking=true}break;case"play":if(!vimeoGAJS.eventMarker[r].videoPlayed){vimeoGAJS.sendEvent(n,"Started video");vimeoGAJS.eventMarker[r].videoPlayed=true}else if(!vimeoGAJS.eventMarker[r].videoResumed&&vimeoGAJS.eventMarker[r].videoPaused){vimeoGAJS.sendEvent(n,"Resumed video");vimeoGAJS.eventMarker[r].videoResumed=true}break;case"pause":vimeoGAJS.onPause(n);break;case"finish":if(!vimeoGAJS.eventMarker[r].videoCompleted){vimeoGAJS.sendEvent(n,"Completed video");vimeoGAJS.eventMarker[r].videoCompleted=true}break}},getUrl:function(e){var t=document.URL.split(":")[0];if(e.match(/^http/)===null){return t+":"+e}},post:function(e,t,n){var r={method:e};if(t){r.value=t}var i=$(n).attr("src").split("?")[0];n.contentWindow.postMessage(JSON.stringify(r),vimeoGAJS.getUrl(i))},onReady:function(){$.each(vimeoGAJS.iframes,function(e,t){vimeoGAJS.post("addEventListener","play",t);vimeoGAJS.post("addEventListener","seek",t);vimeoGAJS.post("addEventListener","pause",t);vimeoGAJS.post("addEventListener","finish",t);vimeoGAJS.post("addEventListener","playProgress",t)})},onPause:function(e){var t=e.attr("id");if(vimeoGAJS.eventMarker[t].timePercentComplete<99&&!vimeoGAJS.eventMarker[t].videoPaused){vimeoGAJS.sendEvent(e,"Paused video");vimeoGAJS.eventMarker[t].videoPaused=true}},onPlayProgress:function(e,t){var n,r=t.attr("id");vimeoGAJS.eventMarker[r].timePercentComplete=Math.round(e.percent*100);if(!t.data("progress")){return}if(vimeoGAJS.eventMarker[r].timePercentComplete>24&&!vimeoGAJS.eventMarker[r].progress25){n="Played video: 25%";vimeoGAJS.eventMarker[r].progress25=true}if(vimeoGAJS.eventMarker[r].timePercentComplete>49&&!vimeoGAJS.eventMarker[r].progress50){n="Played video: 50%";vimeoGAJS.eventMarker[r].progress50=true}if(vimeoGAJS.eventMarker[r].timePercentComplete>74&&!vimeoGAJS.eventMarker[r].progress75){n="Played video: 75%";vimeoGAJS.eventMarker[r].progress75=true}if(n){vimeoGAJS.sendEvent(t,n)}},sendEvent:function(e,t){var n=e.attr("src").split("?")[0];var r=e.data("bounce");switch(vimeoGAJS.gaTracker){case"gtm":dataLayer.push({event:"Vimeo",eventCategory:"Vimeo",eventAction:t,eventLabel:vimeoGAJS.getUrl(n),eventValue:undefined,eventNonInteraction:r?false:true});break;case"ua":ga("send","event","Vimeo",t,vimeoGAJS.getUrl(n),undefined,{nonInteraction:r?0:1});break;case"ga":_gaq.push(["_trackEvent","Vimeo",t,vimeoGAJS.getUrl(n),undefined,r?false:true]);break}}};$(function(){vimeoGAJS.init()})