我有当前的代码来跟踪页面上滚动的百分比。
ga('create', 'UA-68653010-1', 'auto');
window.onbeforeunload = function(){
var viewportHeight = $(this).height();
var progress = $(this).scrollTop() / ($(document).height() - viewportHeight);
ga('send', 'event', 'scroll', Math.round(progress*100));
};
但是每当事件应该发送命中时,它都会返回与预期不同的结果,其中参数之一是:
exd:ReferenceError: ga is not defined
最佳答案
以正确的顺序包含您的 JS 文件(片段)。
首先包含此脚本:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)
[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-68653010-1', 'auto') ;
</script>
...
然后包含您的主 JS 文件,其中包含 onbeforeunload
处理程序:
window.onbeforeunload = function(){
var viewportHeight = $(this).height();
var progress = $(this).scrollTop() / ($(document).height() - viewportHeight);
ga('send', 'event', 'scroll', Math.round(progress*100));
};
关于javascript - 谷歌分析标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34268971/