在单页 webapp 上,我实现了 google 异步跟踪器。我创建了一个类,以便能够进行一些简单的调用以跟踪整个站点的用户:
var GoogleAnalytics = {
config : {
'account':'UA-XXXXXXXX-X'
},
init : function(){
var _gaq = _gaq || [];
this.tracker = _gaq;
_gaq.push(['_setAccount',this.config.account]);
(function() {
var _ga = document.createElement('script'); _ga.type = 'text/javascript'; _ga.async = true;
_ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(_ga, s);
})();
},
doAsyncRequest : function(arr){
if(!this.tracker) this.init();
this.tracker.push(arr);
},
trackPageView : function(url){
var args = ['_trackPageview'];
if(url) args.push(url);
this.doAsyncRequest(args);
},
trackEvent : function(category,action,label,value){
var args = ['_trackEvent',category,action];
if(label) args.push(label);
if(value) args.push(value);
this.doAsyncRequest(args);
},
trackCustom : function(index,name,value,scope){
var args = ['_setCustomVar',index,name,value];
if(scope) args.push(scope);
this.doAsyncRequest(args);
}
}
加载应用程序后,我创建了上面的实例,如下所示:
var that = this;
require(['js/plugins/GoogleAnalytics'],function(ga){ that.ga = GoogleAnalytics; });
它使用 require.js 加载上述脚本,并将 this.ga 分配给它。
然后,当尝试跟踪页面视图时,我使用这个:
var that = this
$.each(payload.events,function(index,event){
if(event.eventType == 'page'){
var pagePath = event.currentURL.replace(/^(https?:\/\/[^\/]+)/i,'');
that.ga.trackPageView(pagePath);
} else {
that.ga.trackEvent(event.eventType,event.elementClass);
}
});
payload.events
只包含一系列事件。对于页面视图,事件看起来像这样:{'eventType':'page','currentURL':'http://www.testurl.com/#!/testing/test/test'}
这一切都可以通过谷歌获得,但在分析中,它会将页面视图跟踪为唯一的页面访问,并且不会跟踪用户的访问。所以我的分析有点没用,因为看起来我的访问量比我真正做的要多得多,而且跳出率也很荒谬。
我是否缺少可以解决此问题并使其像普通分析安装一样跟踪页面浏览量的东西?