単一ページのウェブアプリに、グーグル非同期トラッカーを実装しました。サイト全体でユーザーを追跡できるように、いくつかの簡単な呼び出しを行えるようにクラスを作成しました。
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'}
これはすべてグーグルOKになりますが、分析では、ページビューを一意のページ訪問として追跡し、ユーザーの訪問全体を追跡しません。ですから、私の分析は、実際よりもはるかに多くの訪問があり、ばかげたバウンス率があるように見えるので、やや役に立たないです。
これを修正し、通常の分析インストールのようにページビューを追跡するために欠けているものはありますか?