谷歌分析页面访问量正在增加不正确的每一页变化(angularjs) [英] google analytics page views are being incremented incorrectly for each page change (angularjs)

查看:144
本文介绍了谷歌分析页面访问量正在增加不正确的每一页变化(angularjs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌分析为我的网站跟踪页面访问量(使用AngularJS建)。出于某种原因虽然,页面访问量被错误地递增。例如,当我主页上,我切换到有关网页,关于网页获得1来看,这是正确的,但是当我切换到另一个页面,该页面获得2次时,它应该只得到1。当我切换到另一个页面,该页面获得,依此类推直到我重新加载网页3次。重新加载该网站将重置递增回1,它会重新开始计数,这意味着有似乎是递增与每个状态更改计数。

I'm using Google analytics for my website (built using AngularJS) to track page views. For some reason though, the page views are incremented incorrectly. For example, when I'm on home page and I switch to the about page, about page gets 1 view, which is correct, but when I switch to another page, that page gets 2 views when it should just get 1. When I switch to another page, that page gets 3 views and so on until I reload the website. Reloading the website will reset the incrementation back to 1, and it'll start counting up again, which means there appears to be a count that is incremented with each state change.

我在所有控制器为每个页面此code:

I have this code in all the controllers for each page:

  $rootScope.$on('$stateChangeSuccess', function(event) {
     if (!$window.ga)
        return;
     $window.ga('send', 'pageview', { page: $location.path() });
  });

什么是跟踪误差的原因以及如何解决?

What is the cause of the tracking error and how can I resolve it?

推荐答案

$ stateChangeSuccess是一个全球性的事件,这意味着如果您在每个控制器上code,每一次新的控制器被实例化,你是创建活动的新侦听器。当状态发生变化时,每次您注册监听器被炒鱿鱼,​​因此正在发生的呼叫数量的增加。

$stateChangeSuccess is a global event, which means that if you include the above code in every controller, every time a new controller is instantiated, you are creating a new listener for the event. When a state change occurs, every listener you have registered gets fired, thus the increasing number of calls that are happening.

您实际上只需做一次,很可能在你的主模块的run()方法,而不是在所有的控制器。

You actually only need to do this once, probably in your main module run() method, rather than in all of your controllers.

这篇关于谷歌分析页面访问量正在增加不正确的每一页变化(angularjs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆