dusanmiha

YouTube Video Tracking for Publishers - Nova

Aug 19th, 2020 (edited)
34
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     (function(){var _AS_GAID_PROPERTY="UA-156843553-1";var _asCalledEmbedScript=false;var _asEmbedGtagScript=function(){var _asHead=document.getElementsByTagName("head")[0];var _asAddedGtagScript=document.createElement("script");_asAddedGtagScript.type="text/javascript";_asAddedGtagScript.src="https://www.googletagmanager.com/gtag/js";_asAddedGtagScript.defer=true;_asHead.appendChild(_asAddedGtagScript);_asCalledEmbedScript=true;window.dataLayer=window.dataLayer||[];var gtag=
  3.     function(){dataLayer.push(arguments)};window.gtag=gtag;gtag("js",new Date)};var _asCheckForTrackers=setInterval(function(){var _asTrackerLoaded=window.gtag;if(_asTrackerLoaded){clearInterval(_asCheckForTrackers);_asInitiateVideoTracking()}else if(!_asCalledEmbedScript)_asEmbedGtagScript()},200);setTimeout(function(){clearInterval(_asCheckForTrackers)},1E4);var _asInitiateVideoTracking=function(){var _asSendHit=function(){var _asDestintationProperty=_AS_GAID_PROPERTY;var _asTrackerNameSet=false;if(window.dataLayer&&
  4.    window.dataLayer.length>0){var _asConfigSet=window.dataLayer.filter(function(dl){return dl[0]==="config"&&dl[1]===_asDestintationProperty});_asTrackerNameSet=_asConfigSet[0]!==undefined}if(!_asTrackerNameSet)gtag("config",_asDestintationProperty,{"send_page_view":false});return function(_asData){if(_asData.videoTitle==="not set")return;var _asThereIsHitValue=_asData.hitValue;var _asHitObject;if(_asThereIsHitValue)_asHitObject={"event_category":"NTG Video","event_label":_asData.videoTitle,"value":_asData.hitValue,
  5.     "send_to":_asDestintationProperty,"non_interaction":_asData.nonInteraction};else _asHitObject={"event_category":"NTG Video","event_label":_asData.videoTitle,"send_to":_asDestintationProperty,"non_interaction":_asData.nonInteraction};gtag("event",_asData.eventType,_asHitObject)}}();function _asExtendFunctions(_asObj,_asMethods){for(var _asKey in _asMethods)_asObj[_asKey]=_asMethods[_asKey]}function _asGetYouTubeIframes(){_asYtIframes=Array.prototype.filter.call(document.getElementsByTagName("iframe"),
  6.     function(iframe){return/youtube.com\/embed/.test(iframe.src)})}function _asEmbedYouTubeAPI(){var YT=window.YT;if(_asYtIframes.length>0){var _asYtApiExists=typeof YT!=="undefined"&&typeof YT.Player!=="undefined";if(!_asYtApiExists){var _asCreatedScript=document.createElement("script");var _asFirstScript=document.getElementsByTagName("script")[0];_asCreatedScript.async=true;_asCreatedScript.type="text/javascript";_asCreatedScript.src="//www.youtube.com/iframe_api";_asFirstScript.parentNode.insertBefore(_asCreatedScript,
  7.     _asFirstScript)}_asApiInterval=window.setInterval(_asCheckForIframeAPI,1E3)}}function _asCheckForIframeAPI(){var YT=window.YT;if(typeof YT!=="undefined"&&typeof YT.Player!=="undefined"){window.clearInterval(_asApiInterval);_asYtIframes.map(_asProcessIframeToVideo);_asSetFullScreenListeners();return}if(_asApiCurrentTime<=5)_asApiCurrentTime++;else window.clearInterval(_asApiInterval)}var _asVideoObjectAdditions={setMilestones:function(){var _asVidLength=this.getVideoLength();this._asData.milestones=
  8.    this._asData.milestones.map(function(milestone){return milestone/100*_asVidLength})},setVideoTitle:function(){this._asData.videoTitle=this.getVideoTitle()},getVideoTitle:function(){return this.getVideoData()&&this.getVideoData().title?this.getVideoData().title:decodeURIComponent(this.getVideoUrl().split("/")[this.currentSrc.split("/").length-1])},getVideoLength:function(){return Math.round(this.getDuration())},setVideoLength:function(){this._asData.videoLength=this.getVideoLength()},_getCurrentTime:function(){return Math.round(this.getCurrentTime())},
  9.    isVideo:function(){return this&&this.nodeName==="VIDEO"},_vidLoaded:function(){if(this._asData.videoLoaded)return;this.setVideoTitle();this.setVideoLength();this.setMilestones();this._sendHit("player loaded",true);this._asData.videoLoaded=true},_vidPlay:function(e){var _asVid=e===undefined?this:e.target;if(_asVid._asData.adPlaying){_asVid._asData.adPlaying=false;return}if(e===undefined)if(this._asData&&this._asData.eventType==="player loaded")this._asData.adPlaying=true;if((_asVid._asData.eventType===
  10.    "autoplay"||_asVid._asData.eventType==="click to play")&&_asVid._asData.currentTime===0)return;if(_asVid.milestoneCheck)window.clearInterval(_asVid.milestoneCheck);_asVid.milestoneCheck=window.setInterval(_asVid._vidMilestones,1E3,e);if(_asVid._asData.justFinished){_asVid._asData.justFinished=false;if(!_asVid._asData.isPlaylist)return}var _asPlay=_asVid._asData.firstPlay?["initial click to play","autoplay"]:"resume";if(_asPlay.length===2){_asVid._asData.firstPlay=false;_asVid._asData.autoplay?_asVid._sendHit(_asPlay[1],
  11.    true):_asVid._sendHit(_asPlay[0],false)}else if(!_asVid._asData.justSeeked)_asVid._sendHit(_asPlay,false);if(Math.abs(_asVid._asData.currentTime-_asVid._asData.previousTime)>5&&!_asVid._asData.justFinished)_asVid._sendHit("seek",false);_asVid._asData.justSeeked=false},_vidPause:function(e){var _asVid=e.target;window.clearInterval(_asVid.milestoneCheck);if(Math.abs(_asVid._asData.currentTime-_asVid._asData.previousTime)>5)_asVid._vidSeek(e);else if(_asVid._getCurrentTime()!==_asVid._asData.videoLength)_asVid._sendHit("pause",
  12.     false)},_vidSeek:function(e){var _asVid=e.target;if(_asVid._asData.adPlaying||_asVid._asData.justFinished||_asVid._asData.currentTime===0&&(_asVid._asData.milestones.length===0||_asVid._asData.milestones.length===3)){_asVid._asData.justSeeked=true;return}if(!_asVid._asData.justSeeked&&!_asVid._asData.firstPlay&&!_asVid._asData.justFinished)_asVid._sendHit("seek",false);_asVid._asData.justSeeked=true},_vidComplete:function(e){var _asVid=e.target;if(_asVid._asData.milestones.length>0)for(var i=0;i<
  13.    _asVid._asData.milestones.length;i++){var _asMilestone=_asVid._asData.milestones[i];var _asCurrentPercent=_asMilestone/_asVid._asData.videoLength*100;if(!Number.isNaN(_asCurrentPercent)&&_asCurrentPercent%25===0&&_asCurrentPercent>0&&_asCurrentPercent<=100){_asVid._asData.hitValue=_asCurrentPercent;_asVid._sendHit(decodeURIComponent(encodeURIComponent("milestone "+_asCurrentPercent+"%")),true)}}if(_asVid._asData.justFinished)return;_asVid._asData.adPlaying=false;window.clearInterval(_asVid.milestoneCheck);
  14.     _asVid._asData.hitValue=100;_asVid._sendHit("complete",true);_asVid._asData.firstPlay=true;_asVid._asData.justFinished=true;_asVid._asData.milestones=[25,50,75];if(_asVid._asData.isPlaylist){if(!_asVid._asData.finalPlaylistVid){var _asVidIndex=_asVid.getPlaylistIndex();var _asPlaylistLength=_asVid.getPlaylist().length;if(_asVidIndex+1===_asPlaylistLength)_asVid._asData.finalPlaylistVid=true;_asVid._asData.autoplay=true;_asVid._asData.videoLoaded=false;_asVid._vidLoaded()}}else{_asVid._asData.autoplay=
  15.     false;_asVid.setVideoLength();_asVid.setMilestones()}},_vidMilestones:function(e){if(e===undefined)return;var _asVid=e.target;var _asCurrentTime=_asVid._getCurrentTime();_asVid._asData.previousTime=_asCurrentTime;if(_asCurrentTime<_asVid._asData.milestones[0])return;if(_asVid._asData.milestones.length===0)window.clearInterval(_asVid.milestoneCheck);var _asMilestone;do{_asMilestone=_asVid._asData.milestones.splice(0,1)[0];if(_asVid._asData.firstPlay){_asVid._sendHit("autoplay",true);_asVid._asData.firstPlay=
  16.    false}}while(_asMilestone>_asCurrentTime);var _asCurrentPercent=_asMilestone/_asVid._asData.videoLength*100;if(!Number.isNaN(_asCurrentPercent)&&_asCurrentPercent%25===0&&_asCurrentPercent>0&&_asCurrentPercent<=100){_asVid._asData.hitValue=_asCurrentPercent;_asVid._sendHit(decodeURIComponent(encodeURIComponent("milestone "+_asCurrentPercent+"%")),true)}},_vidMuted:function(){if(this.isMuted()){if(!this._asData.justMuted)this._asData.justMuted=true}else if(this._asData.justMuted)this._asData.justMuted=
  17.     false;else return;this._asData.currentTime=this._getCurrentTime();if(this._asData.currentTime===undefined||this._asData.currentTime<1)return;else if(this._asData.justMuted)this._sendHit("mute",false);else this._sendHit("unmute",false)},_vidFullScreen:function(){if(document.fullscreenElement||document.mozFullScreen||document.webkitIsFullScreen||document.msFullscreenElement||document.fullScreen)this._sendHit("full screen",false)},_sendHit:function(ev,ni){this._asData.previousEventType=this._asData.eventType;
  18.    this._asData.eventType=ev;this._asData.hitValue=ev.match(/milestone|complete/i)?this._asData.hitValue:null;this._asData.nonInteraction=ni;_asSendHit(this._asData)},setListeners:function(){this.addEventListener("onStateChange",function(e){if(e.data===-1){if(e.target._asData.eventType==="player loaded"){e.target._vidPlay(e);return}if(e.target._asData.videoTitle!==e.target.getVideoTitle()){e.target._asData.milestones=[25,50,75];e.target._asData.autoplay=false;e.target._asData.firstPlay=true;e.target._asData.videoLoaded=
  19.    false;e.target._asData.adPlaying=false;e.target._vidLoaded();return}}if(e.data===5)return;e.target._asData.currentTime=e.target._getCurrentTime();var _asEventArray=[e.target._vidComplete,e.target._vidPlay,e.target._vidPause,e.target._vidSeek];var _asEventAction=_asEventArray[e.data]||undefined;if(_asEventAction)_asEventAction(e);e.target._asData.previousTime=e.target._asData.currentTime},true);this.addEventListener("onVolumeChange",function(e){var _asVid=e.target;_asVid._vidMuted()},true)}};function _asProcessIframeToVideo(iframe){if(iframe.src.indexOf("enablejsapi=")===
  20.    -1)iframe.src+=(~iframe.src.indexOf("?")?"&":"?")+"enablejsapi=1";var YT=window.YT;var _asVideo=new YT.Player(iframe,{events:{"onReady":_asOnPlayerReady,"onError":_asOnError}});_asVideo._asData={eventType:undefined,previousEventType:undefined,currentTime:undefined,previousTime:undefined,videoTitle:"not set",videoPlayer:"YT",videoSrc:iframe.src,milestones:[25,50,75],firstPlay:true,autoplay:iframe.src.indexOf("autoplay=1")!==-1,justSeeked:false,justFinished:false,justMuted:false,nonInteraction:null,
  21.    hitValue:0,isPlaylist:false,nextVidClick:false,adPlaying:false,videoLoaded:false};_asExtendFunctions(_asVideo,_asVideoObjectAdditions);_asVideo.setListeners();_asVideos.push(_asVideo)}function _asOnPlayerReady(e){e.target._asData.isPlaylist=!(e.target.getPlaylistIndex()<0);e.target._vidLoaded()}function _asOnError(e){console.log("~ YT Player Error: "+e._asData)}function _asSetFullScreenListeners(){document.addEventListener("webkitfullscreenchange",function(e){_asCheckFullScreen()},false);document.addEventListener("mozfullscreenchange",
  22.    function(e){_asCheckFullScreen()},false);document.addEventListener("fullscreenchange",function(e){_asCheckFullScreen()},false)}function _asCheckFullScreen(){if(document.fullscreenElement){var _asFullscreenVid=_asVideos.filter(function(vid){return vid._asData.videoSrc===document.fullscreenElement.src});_asFullscreenVid[0]._vidFullScreen()}}function _asInfiniteScrollGetVids(){function getdocheight(){var b=document.body,e=document.documentElement;return document.height||Math.max(Math.max(b.scrollHeight,
  23.    e.scrollHeight),Math.max(b.offsetHeight,e.offsetHeight),Math.max(b.clientHeight,e.clientHeight))}var _asMaxHeight=getdocheight();window.setInterval(function(){var _asBufferHeight=100;var _asCurrentHeight=Math.max(_asMaxHeight,getdocheight());if(_asCurrentHeight>_asMaxHeight+_asBufferHeight){_asMaxHeight=_asCurrentHeight;_asGetYouTubeIframes();for(var i=0;i<_asYtIframes.length;i++)if(_asYtIframes[i].src.indexOf("enablejsapi=")===-1)_asProcessIframeToVideo(_asYtIframes[i])}},3E3)}var _asVideos=[];var _asApiInterval;
  24.    var _asApiCurrentTime=0;var _asYtIframes;var _asCheckForYTPlayers=setInterval(function(){_asGetYouTubeIframes();if(_asYtIframes.length>0){clearInterval(_asCheckForYTPlayers);_asEmbedYouTubeAPI()}},500);setTimeout(function(){clearInterval(_asCheckForYTPlayers)},1E4);setTimeout(function(){_asInfiniteScrollGetVids()},5E3)}})();
  25. </script>
RAW Paste Data