我想在不使用 API 脚本格式的情况下使用 Google Analytics 跟踪视频播放,而是跟踪嵌入的<iframe>
. 这个选项有可能吗?嵌入的 YouTube URL 中的enabledjsapi是否可能,以便在点击视频时跟踪状态变化?
目前使用这种格式来跟踪 Google Analytics 中的玩家状态变化。
<div id="player"></div>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'fYmrMt01S1U',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
/// event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data ==YT.PlayerState.PLAYING)
{_gaq.push(['_trackEvent', 'Videos', 'Play',
player.getVideoUrl() ]); }
if (event.data ==YT.PlayerState.PAUSED)
{_gaq.push(['_trackEvent', 'Videos', 'Paused',
player.getVideoUrl() ]); }
if (event.data ==YT.PlayerState.ENDED)
{_gaq.push(['_trackEvent', 'Videos', 'Watch to End',
player.getVideoUrl() ]); } }
// ]]>
</script>