Google 异步跟踪器会记录每个“页面视图”的新访问

发布于 2024-11-17 05:31:41 字数 2230 浏览 2 评论 0原文

在单页网络应用程序上,我实现了谷歌异步跟踪器。我创建了一个类,以便能够进行一些简单的调用,以便能够在整个站点上跟踪用户:

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'}

这一切都可以进入谷歌,但在分析中,它会将页面视图跟踪为唯一的页面访问,并且不会跟踪用户的访问。所以我的分析有点无用,因为看起来我的访问量比实际多得多,而且跳出率也很荒谬。

我是否缺少一些可以解决此问题并使其像正常分析安装一样跟踪页面视图的功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸印温柔 2024-11-24 05:31:41

Frankie 在我的评论中说得对,我相信问题在于 require.js 创建了一个新的作用域并在那里破坏了一些东西。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文