Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #777 from hicom150/master
Browse files Browse the repository at this point in the history
Add trackException and trackTiming methods
  • Loading branch information
pbernasconi committed Apr 29, 2015
2 parents dbdfe5b + eefa37a commit e47a98e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/mocks/googleAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ ngCordovaMocks.factory('$cordovaGoogleAnalytics', ['$q', function($q) {
'trackView',
'addCustomDimension',
'trackEvent',
'trackException',
'trackTiming',
'addTransaction',
'addTransactionItem'
];
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/googleAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ angular.module('ngCordova.plugins.googleAnalytics', [])
return d.promise;
},

trackException: function (description, fatal) {
var d = $q.defer();

$window.analytics.trackException(description, fatal, function (response) {
d.resolve(response);
}, function (error) {
d.reject(error);
});

return d.promise;
},

trackTiming: function (category, milliseconds, variable, label) {
var d = $q.defer();

$window.analytics.trackTiming(category, milliseconds, variable, label, function (response) {
d.resolve(response);
}, function (error) {
d.reject(error);
});

return d.promise;
},

addTransaction: function (transactionId, affiliation, revenue, tax, shipping, currencyCode) {
var d = $q.defer();

Expand Down
10 changes: 9 additions & 1 deletion test/mocks/googleAnalytics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ describe('ngCordovaMocks', function() {
testPromises('addCustomDimension');
});

it('should add customer dimensions.', function() {
it('should track event.', function() {
testPromises('trackEvent');
});

it('should track exception.', function() {
testPromises('trackException');
});

it('should track timing.', function() {
testPromises('trackTiming');
});

it('should add transaction.', function() {
testPromises('addTransaction');
});
Expand Down
95 changes: 94 additions & 1 deletion test/plugins/googleAnalytics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('Service: $cordovaGoogleAnalytics', function() {
trackView: angular.noop,
addCustomDimension: angular.noop,
trackEvent: angular.noop,
trackException: angular.noop,
trackTiming: angular.noop,
addTransaction: angular.noop,
addTransactionItem: angular.noop,
};
Expand Down Expand Up @@ -255,7 +257,98 @@ describe('Service: $cordovaGoogleAnalytics', function() {
errorCb('Tracker not started');
});

$cordovaGoogleAnalytics.trackEvent()
$cordovaGoogleAnalytics
.trackEvent()
.then(angular.noop, function (response) {
result = response;
});

$rootScope.$digest();

expect(result).toBe('Tracker not started');
});

it('should call $window\'s analytics.trackException method', function() {

var result;

spyOn($window.analytics, 'trackException')
.andCallFake(function (description, fatal, successCb, errorCb) {
successCb('Track Exception: ' + description);
});

$cordovaGoogleAnalytics
.trackException('Video player exception', false)
.then(function (response) {
result = response;
});

$rootScope.$digest();

expect(result).toBe('Track Exception: Video player exception');
expect($window.analytics.trackException).toHaveBeenCalledWith(
'Video player exception', false,
jasmine.any(Function),
jasmine.any(Function)
);
});

it('should call errorCb when in $window\'s analytics.trackException a error orccurs', function() {

var result;

spyOn($window.analytics, 'trackException')
.andCallFake(function (description, fatal, successCb, errorCb) {
errorCb('Tracker not started');
});

$cordovaGoogleAnalytics
.trackException()
.then(angular.noop, function (response) {
result = response;
});

$rootScope.$digest();

expect(result).toBe('Tracker not started');
});

it('should call $window\'s analytics.trackTiming method', function() {

var result;

spyOn($window.analytics, 'trackTiming')
.andCallFake(function (category, milliseconds, variable, label, successCb, errorCb) {
successCb('Track Timing: ' + category);
});

$cordovaGoogleAnalytics
.trackTiming('Videos', 100, 'Video Load Time', 'Gone With the Wind')
.then(function (response) {
result = response;
});

$rootScope.$digest();

expect(result).toBe('Track Timing: Videos');
expect($window.analytics.trackTiming).toHaveBeenCalledWith(
'Videos', 100, 'Video Load Time', 'Gone With the Wind',
jasmine.any(Function),
jasmine.any(Function)
);
});

it('should call errorCb when in $window\'s analytics.trackTiming a error orccurs', function() {

var result;

spyOn($window.analytics, 'trackTiming')
.andCallFake(function (category, milliseconds, variable, label, successCb, errorCb) {
errorCb('Tracker not started');
});

$cordovaGoogleAnalytics
.trackTiming()
.then(angular.noop, function (response) {
result = response;
});
Expand Down

0 comments on commit e47a98e

Please sign in to comment.