Tracking Google Analytics Page Views with AngularJS

前端 未结 21 909
北恋
北恋 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:25

    If someone wants to implement using directives then, identify (or create) a div in the index.html (just under the body tag, or at same DOM level)

    and then add the following code in the directive

    myApp.directive('googleAnalytics', function ( $location, $window ) {
      return {
        scope: true,
        link: function (scope) {
          scope.$on( '$routeChangeSuccess', function () {
            $window._gaq.push(['_trackPageview', $location.path()]);
          });
        }
      };
    });
    

提交回复
热议问题