使用谷歌分析跟踪嵌入的YouTube视频

时间:2012-11-20 10:11:31

标签: google-chrome google-analytics youtube-api

我试图找出检查有多少用户观看嵌入式YouTube视频的最简单方法。以下工作会好吗?

$('.video').click(function(){
  _gaq.push(['_trackPageview', 'Video Views', 'Play', 'The Perfect Belgian Serve']);

    });

.video是iframe代码的一个类。

刚刚找到这个插件:

<div class="ytvid">ylLzyHk54Z0</div>
<script src="//code.jquery.com/jquery-1.7.2.js"></script>
<script src="jquery.ytiframetracker.js"></script>
<script>
  jQuery(function ($) {
    $.ytiframetracker({
      track: function (action, label, value) { // track callback
        console.log('custom tracking', action, label, value);
      }
    });
  });
</script>

https://code.google.com/p/jquery-ytiframetracker/wiki/Examples

有人可以解释一下这些动作,标签和价值吗?

3 个答案:

答案 0 :(得分:2)

您可以使用YouTube Analytics API:http://developers.google.com/youtube/analytics

,而不是依赖Google Analytics和自定义事件/跟踪。

这假定相关视频属于您自己的帐户,因为您无法将YouTube Analytics用于任意帐户中的视频。

答案 1 :(得分:1)

我相信该插件使用了事件跟踪功能,您可以在此处找到大量信息:https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#Anatomy

答案 2 :(得分:0)

只需在html中添加代码,然后用跟踪ID

替换输入您的GA跟踪ID

&#13;
&#13;
<script>
		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
		(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
		m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
		})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
		ga('create', 'Enter your GA Tracking ID', 'auto', {'allowLinker': true});
		ga('require', 'linker');
		ga('linker:autoLink', ['instamojo.com'] );
		ga('send', 'pageview');

	
	
	 var tag = document.createElement('script');
  tag.id = 'iframe-demo';
  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('youTubePlayer', {
		events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
    });
  }
  function onPlayerReady(event) {
	
  }
  function changeBorderColor(playerStatus) {
	   
    var color;
    if (playerStatus == -1) {
      color = "#37474F"; 
    } else if (playerStatus == 0) {
      color = "#FFFF00"; 
    } else if (playerStatus == 1) {
		
      color = "#33691E"; 
	  ga('send', 'event', 'Videos', 'play-videoname', 'videoname');  
	
	  
    } else if (playerStatus == 2) {
	
      color = "#DD2C00"; 
	   ga('send', 'event', 'Videos', 'Pause-videoname', 'videoname');  
	
    } else if (playerStatus == 3) { 
      color = "#AA00FF"; 
    } else if (playerStatus == 5) {
      color = "#FF6DOO"; 
    }
    if (color) {
      document.getElementById('youTubePlayer').style.borderColor = color;
    }
  }
  function onPlayerStateChange(event) {
    changeBorderColor(event.data);
  }
  
	</script>
&#13;
&#13;
&#13;