Javascript Google Analytics设置哈希更改页面视图

Javascript Google Analytics设置哈希更改页面视图,javascript,jquery,google-analytics,jquery-isotope,pageviews,Javascript,Jquery,Google Analytics,Jquery Isotope,Pageviews,我正试图得到一个主页,其中有一堆的内容下 在Google Analytics中将每个哈希更改显示为页面视图。最初,我打算把它作为事件来做,但实际上应该是页面浏览量 因此,我设置了修改后的GA: (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

我正试图得到一个主页,其中有一堆的内容下

在Google Analytics中将每个哈希更改显示为页面视图。最初,我打算把它作为事件来做,但实际上应该是页面浏览量

因此,我设置了修改后的GA:

(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', {'allowAnchor': true});
ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash});
在Google Analytics中,如果有人转到特定的URL,我现在会看到哈希标签-例如: 如果他们重新加载页面,我会看到GA中的散列,但如果他们点击同位素“导航”链接访问它,则不会看到。如果他们点击,我只看到“/”

在同位素燃烧中,我所拥有的似乎不起作用:

//Sets up filtering on click of Isotope navigational elements 
    $('#isotopeFilters a, .subnav a, #isotopeContainer .isotopeNav a, .page-template-page-home-php #logo').click(function(){
        var selector = $(this).attr('data-filter');
        var prettyselector = selector.substr(1);
        ga('send', 'pageview', location.pathname+location.search+location.hash);

        location.hash = prettyselector;

        $('#isotopeFilters a, .subnav a').removeClass('active');
        $('a[class="' + prettyselector + '"]').addClass('active');

        $container.isotope({ 
            filter: selector,
            itemSelector: '.item',
            masonry: {
                columnWidth: 270
            },
            animationOptions: {
            duration: 750,
            easing: 'linear',
            queue: false,
        }
      });
      return false;
    });
我认为单击函数中的这一行可以做到:

ga('send', 'pageview', location.pathname+location.search+location.hash);
我的语法是否不正确或遗漏了什么

//Fires Isotope functionality when hash/URL changes
    $(window).hashchange( function(){
        if(location.hash!=''){
            var hashfilter = '.' + location.hash.substr(1);
        }else{
            var hashfilter = '.home';
        }

        $container.isotope({
            filter: hashfilter,
            itemSelector: '.item',
            masonry: {
                columnWidth: 270
            },
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false,
           }
        });
        isotopeSubNav();
    });

    if(location.hash!=''){
        var hashfilter = '.' + location.hash.substr(1);
        ga('send', 'pageview', location.pathname+location.search+location.hash);
        $(hashfilter).addClass('active');
    }

这使用的是相同的语法,因此我假设如果我修复了一个语法,那么将语法复制到hashchange函数也将获得该记录。

要更改发送到GA的页面路径,只需对代码稍作修改:

ga('send', 'pageview', {'page': location.pathname+location.search+location.hash});

更多信息可以在这里找到:

发送
页面
在发送
页面视图
中,谷歌不建议呼叫:

从技术上讲,pageview hits的send命令接受 可选页面字段作为第三个参数,传递页面字段 在跟踪单页应用程序时,不建议使用这种方式。这 是因为通过send命令传递的字段未在 它们仅适用于当前命中。不更新跟踪器 如果应用程序发送任何非页面浏览点击,将导致问题 (例如,事件或社交互动),因为这些点击将被关联 使用跟踪器创建时的任何页面值

使用:


下面是一个完整的代码示例,用于跟踪google Analytics中的哈希页面浏览量和更改:

(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', 'https://www.google-analytics.com/analytics.js', 'ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('set', 'page', location.pathname+location.search+location.hash);
    ga('send', 'pageview');
    window.addEventListener("hashchange", function (event) {
        ga('set', 'page', location.pathname + location.search + location.hash);
        ga('send', 'pageview');
    })

当前Google Analytics配置加载Google Tag Manager脚本,并使用gtag函数,而不是ga,因此对我来说,以前的解决方案会抛出错误,因为“ga未定义”。我所做的是对初始Google Analytics配置脚本进行修改,包括:

<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR-GA-ID"></script>
<script>
     window.dataLayer = window.dataLayer || [];
     function gtag() {
         dataLayer.push(arguments);
     }
     gtag('js', new Date());
     gtag('config', 'YOUR-GA-ID', {
         'page_path': location.pathname + location.hash
     });
 </script>
你可以看看


和<

感谢这项工作,但是考虑更新代码,因为它缺少了关闭卷曲括号<代码> }/COD>,这会导致语法错误。它应该是这样的:
ga('send','pageview',{'page':location.pathname+location.search+location.hash})谢谢,修复了错误!我在gtag中使用了你的代码。在实时GA中,我看到带有哈希的url,但在Behavior->Site Content中,我只看到一个url:“/”没有哈希。如何修复它以在行为->网站内容中查看带有哈希的页面?我在行为->网站内容下看到hastag URL。。。我没有这个问题
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR-GA-ID"></script>
<script>
     window.dataLayer = window.dataLayer || [];
     function gtag() {
         dataLayer.push(arguments);
     }
     gtag('js', new Date());
     gtag('config', 'YOUR-GA-ID', {
         'page_path': location.pathname + location.hash
     });
 </script>
window.addEventListener("hashchange", function (event) {
       gtag('event', 'pageview', {
         'page_path': location.pathname+location.search+location.hash
       });
});