Google Analytics 中的视频跟踪
Posted
技术标签:
【中文标题】Google Analytics 中的视频跟踪【英文标题】:video tracking in Google Analytics 【发布时间】:2015-03-17 14:54:42 【问题描述】:我写了这个脚本: 我正在跟踪播放、暂停和结束的视频事件。 我正在尝试将这些事件链接到分析。(使用 youtube api 和事件跟踪)。 警报正在出现,但在 Analytics 中没有记录事件。 所以请大家帮忙。
enter code here
【问题讨论】:
您使用的是 Universal Analytics 还是经典的 Google Analytics?您的事件语法表明您使用的是后者,但只是想先检查一下这不是问题。 【参考方案1】:可能为时已晚,而且我可能没有足够的时间来解释这一点,但是一旦您设置了事件跟踪事件,请尝试这样做;
//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();
【讨论】:
以上是关于Google Analytics 中的视频跟踪的主要内容,如果未能解决你的问题,请参考以下文章
使用 Google Tag Manager 和 Universal Analytics 的 onclick 事件
2.3.6Google Analytics高级应用——事件跟踪
javascript 使用事件跟踪和jQuery跟踪Google Analytics中的传出链接
javascript 跟踪Google Analytics中的下载和出站链接