试图在初始视频播放上实现Google跟踪。
API给出6个状态
-1 (unstarted)
0 (ended)
1 (playing)
2 (paused)
3 (buffering)
5 (video cued).
但是我正在使用的状态(1:播放)在每次视频点击上触发。我只想跟踪最初的播放事件。
这就是我到目前为止的。
asyncYouTubeApiLoad();
getVideoTrackingValues();
function getVideoTrackingValues() {
$player = $('#player'),
$yt_trackValue = $player.data('tracking-value');
}
executeYouTubeReady();
function executeYouTubeReady() {
// Wait for API
setTimeout(function() {
onYouTubeIframeAPIReady();
}, 200);
}
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
// On Play
if (event.data = 1) {
fireVideoEventTracking('video_play');
}
// On End
else if (event.data = 0) {
fireVideoEventTracking('video_complete');
}
}
function fireVideoEventTracking(eventType) {
_gaq.push(['_trackEvent', 'video', eventType, $yt_trackValue]);
}
最终将布尔值添加到现有方法中并对其进行检查。
var initialLoad = false;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady() {
initialLoad = true;
}
function onPlayerStateChange(event) {
console.log(event.data);
// On Play
if (event.data == 1 && initialLoad) {
fireVideoEventTracking('video_play');
initialLoad = false;
} else if (event.data == 0) {
fireVideoEventTracking('video_complete');
}
}