-2

有没有更简单的方法可以通过 Google Analytics 跟踪 YouTube 视频流量?在整个网站上部署多个视频似乎是一项乏味的任务。

4

3 回答 3

0

您可以使用以下代码来跟踪视频。

<script>
  // This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  //  This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '289',
      width: '428',
      videoId: 'VIDEO ID',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }
  //  The API will call this function when the video player is ready. Uncomment the below code to start video when ready
  function onPlayerReady(event) {
    // event.target.playVideo();
    }
  function onPlayerStateChange(event) {
    var lastAction="";
      switch (event.data){
        case YT.PlayerState.PLAYING:
        if (lastAction != 'paused'){
            _gaq.push(['_trackEvent','video', 'Playing', getPercentage()]);
        }
        else{
            lastAction = '';
           }
        break;
        case YT.PlayerState.ENDED:
          _gaq.push(['_trackEvent','video', 'Completed',getPercentage()]);
        break;
        case YT.PlayerState.PAUSED:
         if (lastAction != 'paused'){
            _gaq.push(['_trackEvent','video', 'Paused', getPercentage()]);
            lastAction= "paused";
           }
        break;
        }
    }
    function getPercentage()
    {
    var pecentage =((player.getCurrentTime()/player.getDuration())*100).toFixed();
      if(pecentage > 0 && pecentage <= 25){
       return "0-25%";}
      else if(pecentage > 25 && pecentage <= 50){
       return "25-50%";}
      else if(pecentage > 50 && pecentage <= 75){
       return "50-75%";}
      else if(pecentage > 75){
       return "75-100%";}
    }
    function stopVideo() {
     _gaq.push(['_trackEvent','video', 'Stopped', player.getDuration()]);
      player.stopVideo();
    }
</script>
于 2013-10-11T07:29:11.737 回答
0

YouTube 有一个公共分析 API,您可以使用它来检查您的视频的使用情况。

http://developers.google.com/youtube/analytics

于 2013-01-22T13:11:06.903 回答
-1

如果您可以选择在 Youtube 之外托管视频,那绝对是更好的选择。例如,http: //wistia.com/ 在视频前后为您的用户提供了显着改进的分析和更清晰、更可控的体验。

于 2012-06-29T22:26:28.647 回答