我写过这个脚本: 我正在跟踪播放,暂停和结束的视频事件。 我正在尝试将这些事件与分析相关联。(使用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 player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'Ivso-k5ECII', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } var pauseFlag = false; function onPlayerReady(event) { // do nothing, no tracking needed } function onPlayerStateChange(event) { // track when user clicks to Play if (event.data == YT.PlayerState.PLAYING) { alert("Hello! I am an alert playing!!"); _gaq.push(['_trackEvent', 'Videos', 'Play', 'Test Video']); pauseFlag = true; } }
答案 0 :(得分:0)
可能为时已晚,我可能没有足够的时间来解释这个问题,但是你设置了事件跟踪事件就试试这个;
//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();
}
}