我写了这个脚本:我正在跟踪视频事件的播放,暂停和结束。我正在尝试将这些事件链接到分析。(使用 YouTube API 和事件跟踪)。警报出现,但 Analytics(分析)中未记录任何事件。所以请帮忙。
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播放器; function onYouTubeIframeAPIReady() { 玩家 = 新 YT。玩家("玩家", { 高度: '390', 宽度: '640', videoId: 'Ivso-k5ECII', 事件: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } var pauseFlag = false; 函数 onPlayerReady(event) { 什么都不做,无需跟踪 } 函数 onPlayerStateChange(event) { 跟踪用户点击播放的时间 if (event.data == YT.PlayerState.PLAYING) { 警报("你好!我是一个警觉的玩!! _gaq.push(['_trackEvent', '视频', '播放', '测试视频']); 暂停标志 = 真; } }
可能为时已晚,我可能没有足够的时间来解释这一点,但是一旦您设置了事件跟踪事件,请尝试此操作;
//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();
}
}