历史状态推送和谷歌分析

History state pushing and Google Analytics

本文关键字:谷歌 历史状态      更新时间:2023-09-26

我在通过history.state/pushing跟踪我的网站时遇到问题。我有这个谷歌分析代码:

<script type="text/javascript">
    (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-XXXXXXXX-X', 'auto');
    ga('send', 'pageview');
</script>

现在我有事件侦听器来检查何时history.pushState

<script>
(function(history){
    var pushState = history.pushState;
    history.pushState = function(state) {
        if (typeof history.onpushstate == "function") {
            history.onpushstate({state: state});
        }
        // ... whatever else you want to do
        // maybe call onhashchange e.handler
        return pushState.apply(history, arguments);
    }
})(window.history);
function __inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}
window.onpopstate = history.onpushstate = function(e) {
    setTimeout(function() {
        var path = document.location.pathname;
        ga('set', path);
        ga('send', 'pageview');
    }, 650);
}
</script>

跟踪良好,它会将适当的URL发送到应有的位置,但是Chrome扩展程序标签助手(Google)报告了我:

同一网络媒体资源 ID 会被跟踪两次。

我的第二个 Google Analytics(分析)跟踪代码实例会显示在跟踪代码助手的列表中。

我的实现和/或方法有什么问题?


编辑:

我有四个网址:

/home
/home/personal-info
/home/employment-info
/home/privacy-settings
/home/documents

而且我不想/home跟踪。

标签助理录制文件会报告一条警告,指出如果某个网页在短时间内发送了多次网页浏览匹配,则该网页会被跟踪两次。

(标签助手

录音目前不考虑网页浏览的参数,这在标签助手中可能应该改变)

在这种情况下,您在网页初始化中有一个

网页浏览命中,在事件处理程序中有一个。

您没有正确设置路径。应该ga('set', 'page', path)或者你可以做ga('send', 'pageview', path)