0
votes

I have written this script : I am tracking the video events from playing,pausing and ending. I am trying to link these event to analytics.(using youtube api and event tracking). The alert is appearing but their is no event recorded in Analytics. So any help please.

enter code here

    
    
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'Ivso-k5ECII',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }
      var pauseFlag = false;
      function onPlayerReady(event) {
     // do nothing, no tracking needed
      }
     function onPlayerStateChange(event) {
     // track when user clicks to Play
     if (event.data == YT.PlayerState.PLAYING) {
        alert("Hello! I am an alert playing!!");
        _gaq.push(['_trackEvent', 'Videos', 'Play', 'Test Video']);
        pauseFlag = true;
       }
     }
      

1
Are you using Universal Analytics or classic Google Analytics? Your event syntax indicates that you are using the latter, but just wanted to check that that wasn't the issue first.nyuen

1 Answers

0
votes

Just may be too late also I may not have enough time to explain this however onces you set up your event tracking event try this;

 //GA SetUp
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); 
  document.write("<script src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'>" + "</sc" + "ript>"); 
var pageTracker = _gat._getTracker("UA-xxxx-x");
pageTracker._initData();
pageTracker._trackPageview();  
// Create the event tracking object
var ytpEventTracker = pageTracker._createEventTracker("YouTube Video Player");
var eventLabel;
var nowPlaying;


        function updateHTML(elmId, value) {
          document.getElementById(elmId).innerHTML = value;
        }

        function setytplayerState(newState) {
          updateHTML("playerstate", newState);
      if (translateYTPState(newState) == 'ended' && parseInt(getCurrentTime()) > 0) { 
            recordEnd(eventLabel,parseInt(getCurrentTime()));
          }
        }

        function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById("myytplayer");
          setInterval(updateytplayerInfo, 250);
          updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          ytpEventTracker._trackEvent("Player Loaded", eventLabel);
    }

        function onytplayerStateChange(newState) {
          setytplayerState(newState);
        }

        function translateYTPState(state) {
          switch (state) {
            case -1: return "unstarted";
            case 0 : return "ended";
            case 1 : return "playing";
            case 2 : return "paused";
            case 3 : return "buffering";
            case 5 : return "video cued";
          }
          return;
        }

        function onPlayerError(errorCode) {
          alert("An error occurred: "+ errorCode);
          ytpEventTracker._trackEvent("Error: " + errorCode, eventLabel);
        }

        function updateytplayerInfo() {
          updateHTML("bytesloaded", getBytesLoaded());
          updateHTML("bytestotal", getBytesTotal());
          updateHTML("videoduration", getDuration());
          updateHTML("videotime", getCurrentTime());
          updateHTML("startbytes", getStartBytes());
          updateHTML("volume", getVolume());
        }

        // functions for the api calls
        function loadNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.loadVideoById(id, startSeconds);
      }
        }

        function cueNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.cueVideoById(id, startSeconds);
          }
        }

    function startNewVideo () {
      // if there is a current video playing, record the end
      var oldTime = parseInt(getCurrentTime());
          var oldEventLabel = eventLabel;

          if (oldTime > 0) { 
        recordEnd(oldEventLabel, oldTime);
      } 

      eventLabel = document.getElementById('loadvideoid').options[document.getElementById('loadvideoid').selectedIndex].text;
      loadNewVideo(document.getElementById('loadvideoid').value, 0);
          ytpEventTracker._trackEvent("Video Started", eventLabel);
    }

        function recordEnd(l,t) {
            ytpEventTracker._trackEvent("Ended", l, parseInt(t));
        }

        function play() {
          if (ytplayer) {
            ytplayer.playVideo();
            ytpEventTracker._trackEvent("Play",eventLabel);
          }
        }

        function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
            ytpEventTracker._trackEvent("Pause",eventLabel);
      }
        }

        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
            ytpEventTracker._trackEvent("Stop",eventLabel);
          }
        }

        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getPlayerState();
          }
        }

        function seekTo(seconds) {
          if (ytplayer) {
            ytplayer.seekTo(seconds, true);
            ytpEventTracker._trackEvent("Seek To: " + seconds, eventLabel); 
          }
        }

        function getBytesLoaded() {
          if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
          }
        }

        function getBytesTotal() {
          if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
          }
        }

        function getCurrentTime() {
          if (ytplayer) {
            return ytplayer.getCurrentTime();
          }
        }

        function getDuration() {
          if (ytplayer) {
            return ytplayer.getDuration();
          }
        }

        function getStartBytes() {
          if (ytplayer) {
            return ytplayer.getVideoStartBytes();
          }
        }

        function mute() {
          if (ytplayer) {
            ytplayer.mute();
            ytpEventTracker._trackEvent("Mute",eventLabel);
          }
        }

        function unMute() {
          if (ytplayer) {
            ytplayer.unMute();
            ytpEventTracker._trackEvent("Unmute",eventLabel);
          }
        }

        function getEmbedCode() {
          alert(ytplayer.getVideoEmbedCode());
      ytpEventTracker._trackEvent("Get Embed Code", eventLabel); 
        }

        function getVideoUrl() {
          alert(ytplayer.getVideoUrl());
          ytpEventTracker._trackEvent("Get Video URL", eventLabel); 
    }

        function setVolume(newVolume) {
          if (ytplayer) {
            ytplayer.setVolume(newVolume);
          }
        }

        function getVolume() {
          if (ytplayer) {
            return ytplayer.getVolume();
          }
        }

        function clearVideo() {
          if (ytplayer) {
            ytplayer.clearVideo();
          }
        }