AngularJS with Google Analytics

。_饼干妹妹 提交于 2019-12-11 11:38:26

问题


I want to add Google Analytics code to my AngularJS app.

I am using Ruby On Rails as a backend framework. The AngularJS application.html.erb file of Rails loads only on the first request.

So, I am putting google analytics code inside viewcontentLoad event.

 $rootScope.$on('$viewContentLoaded', function () {

        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-XXXX-Y', 'auto');
//        ga('send', 'pageview');
        ga('send', 'pageview', {'page': $location.path()});
});

I don't know if this is a valid way of inserting Google Analytics code or not.


回答1:


This answer on Tracking Google Analytics Page Views with Angular.js may be helpful.

One of the comments suggests that this is a good way to do it:

$rootScope.$on('$routeChangeSuccess', ...)


来源:https://stackoverflow.com/questions/20614891/angularjs-with-google-analytics

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