我想将数据发送到Google Analytics(分析),以衡量用户观看多个视频的行为-我想知道他们是否玩过Cideo,观看了25%,50%,75%或100%。以下代码未将数据发送到GA。我不确定我的问题是否属于Google Analytics(分析)或javascript或两者兼而有之。请帮忙。谢谢。
<video id="v1" src="/v1.mp4" width="400px" height="200px" controls />
<video id="v2" src="/v2.mp4" width="400px" height="200px" controls />
<script type="text/javascript">
(function() {
document.addEventListener('DOMContentLoaded', init, false);
var videoId, dur, quarter, stat;
function init() {
for (var video of document.querySelectorAll('video')) {
video.addEventListener('play', videoPlay, false);
video.addEventListener('ended', videoEnd, false);
video.addEventListener('timeupdate', videoTimeUpdate, false);
}
}
function setKeyFrames(duration) {
if (dur) {
return;
}
dur = duration;
quarter = duration / 4;
}
function videoTimeUpdate(e) {
var videoId = e.target;
var curTime = videoId.currentTime;
if (curTime >= quarter && curTime < quarter * 2 && stat !== 1) {
gtag('event', 'Watched Video', {
'event_category': 'Video',
'event_label': 'Watched 25% of ' + videoId.id
});
stat = 1;
} else if (curTime >= quarter * 2 && curTime < quarter * 3 && stat !== 2) {
gtag('event', 'Watched Video', {
'event_category': 'Video',
'event_label': 'Watched 50% of ' + videoId.id
});
stat = 2;
} else if (curTime >= quarter * 3 && curTime < dur && stat !== 3) {
gtag('event', 'Watched Video', {
'event_category': 'Video',
'event_label': 'Watched 75% of ' + videoId.id
});
stat = 3;
}
}
function videoPlay(e) {
var videoId = e.target;
gtag('event', 'Watched Video', {
'event_category': 'Video',
'event_label': 'Video Played Is ' + videoId.id
})
setKeyFrames(this.duration);
}
function videoEnd(e) {
var videoId = e.target;
stat = 4;
gtag('event', 'Watched Video', {
'event_category': 'Video',
'event_label': 'Watched ALL of ' + videoId.id
});
}
})();
</script>