我正在尝试获得一个主页,该主页在 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});
在 Google Analytics 中,如果有人转到特定 URL,我现在会看到哈希标签 — 例如:http://www.example.com/#pet-health 如果他们重新加载页面,我会在 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});
可在此处找到更多信息:https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced?hl=en#fieldObject
关于javascript - Google Analytics 在哈希变化时设置页面 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32378607/