Google Analytics在Hash Change上设置PageView

Kee*_*fer 5 javascript jquery google-analytics pageviews jquery-isotope

我正在尝试在Isotope下获得一堆包含大量内容的主页

将每个哈希更改显示为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});
Run Code Online (Sandbox Code Playgroud)

在Google Analytics中,如果有人访问特定的网址,我会立即看到哈希标记 - 例如:http://www.example.com/#pet-health 如果他们重新加载页面,我会在GA中看到哈希,但是如果他们点击Isotope"nav"链接进入它.如果他们点击,我只是看到"/"

在同位素射击中,我所拥有的似乎没有起作用:

//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;
    });
Run Code Online (Sandbox Code Playgroud)

我认为click函数中的这一行可以解决这个问题:

ga('send', 'pageview', location.pathname+location.search+location.hash);
Run Code Online (Sandbox Code Playgroud)

我的语法不正确或遗漏了什么?

//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');
    }
Run Code Online (Sandbox Code Playgroud)

这是使用相同的语法,所以我假设如果我修复一个,将语法复制到hashchange函数也将获得该记录.

nyu*_*uen 10

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

ga('send', 'pageview', {'page': location.pathname+location.search+location.hash});
Run Code Online (Sandbox Code Playgroud)

可以在此处找到更多信息:https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl = en#fieldObject


Dan*_*mak 6

发送page在发送pageview呼叫不建议由谷歌:

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

使用:

ga('set', 'page', location.pathname+location.search+location.hash);
ga('send', 'pageview');
Run Code Online (Sandbox Code Playgroud)

有关跟踪单页应用程序的Google Analytics指南.