У меня есть 11 видео Youtube на странице, которую я хочу отслеживать с помощью Youtube Api и Google Analytics. webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en" rel="nofollow noreferrer">Отладчик Google Analytics и где я ошибся
Я использую New Analytics.js
HTML
<div style="margin-bottom: 24px;">
<iframe id="video01" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/xxx?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video02" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/yyy?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video03" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/zzz?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video04" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/eee?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video05" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/rrr?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video06" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/www?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video07" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/qqq?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video08" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/bbb?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video09" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/lll?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video10" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/ooo?enablejsapi=1" frameborder="0"></iframe>
<iframe id="video11" style="margin-bottom: 16px; margin-right: 16px;" class="etib" width="340" height="220" src="https://www.youtube.com/embed/kkk?enablejsapi=1" frameborder="0"></iframe>
</div>
Java-скрипт
$(function () {
try {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var video01, video02, video03, video04, video05
, video06, video07, video08, video09, video10, video11,
playerInfoList =
[
{ id: 'video01', height: '390', width: '640', videoId: 'ml_BgIm3FzY' }, { id: 'video02', height: '390', width: '640', videoId: 'hV__30BVW8U' },
{ id: 'video03', height: '390', width: '640', videoId: 'A9H93F-8WQk' }, { id: 'video04', height: '390', width: '640', videoId: 'C9N2Chg6E7A' },
{ id: 'video05', height: '390', width: '640', videoId: '-J07hSBRfL8' }, { id: 'video06', height: '390', width: '640', videoId: 'Faor8xaj5RI' },
{ id: 'video07', height: '390', width: '640', videoId: 'R2qR5atJQ6o' }, { id: 'video08', height: '390', width: '640', videoId: 'n-2q3cu5JNA' },
{ id: 'video09', height: '390', width: '640', videoId: 'g963TyA8bgc' }, { id: 'video10', height: '390', width: '640', videoId: '5p4PodfYQEQ' },
{ id: 'video11', height: '390', width: '640', videoId: 'Uba6pRWWr6o' }];
function onYouTubeIframeAPIReady() {
if (typeof playerInfoList === 'undefined')
return;
$.each(playerInfoList, function (index, playerInfo) {
var video = playerInfo.id;
video = createPlayer(playerInfo)
});
function createPlayer(playerInfo) {
return new YT.Player(playerInfo.id, {
height: playerInfo.height,
width: playerInfo.width,
videoId: playerInfo.videoId,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
}
// The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var pauseFlag = false;
function onPlayerStateChange(event) {
// track when user clicks to Play
if (event.data == YT.PlayerState.PLAYING) {
_gaq.push(['_trackEvent', 'Videos', 'Play', 'Test Video']);
pauseFlag = true;
}
// track when user clicks to Pause
if (event.data == YT.PlayerState.PAUSED && pauseFlag) {
_gaq.push(['_trackEvent', 'Videos', 'Pause', 'Test Video']);
pauseFlag = false;
}
// track when video ends
if (event.data == YT.PlayerState.ENDED) {
_gaq.push(['_trackEvent', 'Videos', 'Finished', 'Test Video']);
}
}
}
catch (Exception) { }
});