AngularJS Time Tracker

Easily track how long users are staying on your website

View the Project on GitHub mattmcneeney/angularjs-time-tracker

Requirements

Usage

Include the directive in your app, and simply add the time tracker element to the HTML of any page you want to track:

<time-tracker></time-tracker>

Events

Events will be logged in Google Analytics as follows:

Code

angular.module('yourApp', [])
.directive('timeTracker', [ '$timeout', function($timeout) {
   return {
      restrict: 'E',
      link: function(scope, element, attrs, ctrl) {

         // The durations (in seconds) that we want to track
         scope.durations = [ 0, 5, 10, 20, 30, 60, 120, 300 ];

         // Setup the Google Analytics tracking calls
         scope.trackDuration = function(secondsElapsed) {
            ga('send', 'event', 'Time Tracker', 'Track', 'Seconds elapsed', secondsElapsed, { 'nonInteraction': 1 });
         };

         // Setup the tracking calls
         scope.durations.forEach(function(duration) {
            $timeout(function() {
               scope.trackDuration(duration);
            }, (duration * 1000));
         });

         // Log that time tracking is starting
         console.log('Time tracker started');
      }
   };
}]);