Tracking Google Analytics Page Views with AngularJS

前端 未结 21 990
北恋
北恋 2020-11-27 09:14

I\'m setting up a new app using AngularJS as the frontend. Everything on the client side is done with HTML5 pushstate and I\'d like to be able to track my page views in Goog

21条回答
  •  盖世英雄少女心
    2020-11-27 09:45

    If you are looking for full control of Google Analytics's new tracking code, you could use my very own Angular-GA.

    It makes ga available through injection, so it's easy to test. It doesn't do any magic, apart from setting the path on every routeChange. You still have to send the pageview like here.

    app.run(function ($rootScope, $location, ga) {
        $rootScope.$on('$routeChangeSuccess', function(){
            ga('send', 'pageview');
        });
    });
    

    Additionaly there is a directive ga which allows to bind multiple analytics functions to events, like this:

    
    

提交回复
热议问题