Tracking Google Analytics Page Views with AngularJS

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

    Just a quick addition. If you're using the new analytics.js, then:

    var track = function() {     
     ga('send', 'pageview', {'page': $location.path()});                
    };
    

    Additionally one tip is that google analytics will not fire on localhost. So if you are testing on localhost, use the following instead of the default create (full documentation)

    ga('create', 'UA-XXXX-Y', {'cookieDomain': 'none'});
    
    0 讨论(0)
  • 2020-11-27 09:44

    In your index.html, copy and paste the ga snippet but remove the line ga('send', 'pageview');

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
    <script>
      (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-XXXXXXXX-X');
    </script>
    

    I like to give it it's own factory file my-google-analytics.js with self injection:

    angular.module('myApp')
      .factory('myGoogleAnalytics', [
        '$rootScope', '$window', '$location', 
        function ($rootScope, $window, $location) {
    
          var myGoogleAnalytics = {};
    
          /**
           * Set the page to the current location path
           * and then send a pageview to log path change.
           */
          myGoogleAnalytics.sendPageview = function() {
            if ($window.ga) {
              $window.ga('set', 'page', $location.path());
              $window.ga('send', 'pageview');
            }
          }
    
          // subscribe to events
          $rootScope.$on('$viewContentLoaded', myGoogleAnalytics.sendPageview);
    
          return myGoogleAnalytics;
        }
      ])
      .run([
        'myGoogleAnalytics', 
        function(myGoogleAnalytics) {
            // inject self
        }
      ]);
    
    0 讨论(0)
  • 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:

    <a href="#" ga="[['set', 'metric1', 10], ['send', 'event', 'player', 'play', video.id]]"></a>
    
    0 讨论(0)
提交回复
热议问题