Easily track how long users are staying on your website
View the Project on GitHub mattmcneeney/angularjs-time-tracker
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 will be logged in Google Analytics as follows:
Time Tracker
Seconds elapsed
<number of seconds spent on page>
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');
}
};
}]);