google analytics page views are being incremented incorrectly for each page change (angularjs)

女生的网名这么多〃 提交于 2019-12-12 03:14:37

问题


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.

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?


回答1:


$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.

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



来源:https://stackoverflow.com/questions/34538042/google-analytics-page-views-are-being-incremented-incorrectly-for-each-page-chan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!