开发者

Google async tracker logs new visit every "page view"

On a single page webapp, I've implemented the google async tracker. I created a class to be able to make some simple calls to be able to track users across the site:

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);
    }
}

When the app is loaded, I create an instance of the above like so:

var that = this;
require(['js/plugins/GoogleAnalytics'],function(ga){ that.ga = GoogleAnalytics; });

it uses require.js to load in the above script, and assign this.ga to it.

Then, when trying to track a page view, I use this:

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 just contains an array of events. an event looks something like this for a page view: {'eventType':'page','currentURL':'http://www.testurl.com/#!/testing/test/test'}

This all gets to google ok, but in analytics, it tracks a page view as a unique page visit, and doesn't track across the users' visit. So my analytics are somewhat useless, as it looks like I have WAY more visits than I really do, and a ridiculous bounce rate.

Is there something I'm missing that would fix this and make it track page views like a normal analytics install?


Frankie got this one right in my comments, I believe the issue was with require.js making a new scope and something breaking there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜