/
angulartics-ga-cordova-google-analytics-plugin.js
95 lines (82 loc) · 2.55 KB
/
angulartics-ga-cordova-google-analytics-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @license Angulartics
* (c) 2013 Luis Farzati http://luisfarzati.github.io/angulartics
* License: MIT
*/
(function(angular) {
'use strict';
/**
* @ngdoc overview
* @name angulartics.google.analytics
* Enables analytics support for Google Analytics (http://google.com/analytics)
* for Cordova with google-analytics-plugin (https://github.com/danwilson/google-analytics-plugin)
*/
angular.module('angulartics.google.analytics.cordova', ['angulartics'])
.provider('googleAnalyticsCordova', function () {
var GoogleAnalyticsCordova = [
'$q', '$log', 'ready', 'debug', 'trackingId', 'period',
function ($q, $log, ready, debug, trackingId, period) {
var deferred = $q.defer();
var deviceReady = false;
window.addEventListener('deviceReady', function () {
deviceReady = true;
deferred.resolve();
});
setTimeout(function () {
if (!deviceReady) {
deferred.resolve();
}
}, 3000);
function success() {
if (debug) {
$log.info(arguments);
}
}
function failure(err) {
if (debug) {
$log.error(err);
}
}
this.init = function () {
return deferred.promise.then(function () {
var gaAnalytics = window.ga || window.analytics;
if (typeof gaAnalytics != 'undefined') {
ready(gaAnalytics, success, failure);
gaAnalytics.startTrackerWithId(trackingId);
} else if (debug) {
$log.error('Google Analytics Plugin for Cordova is not available');
}
});
};
}];
return {
$get: ['$injector', function ($injector) {
return $injector.instantiate(GoogleAnalyticsCordova, {
ready: this._ready || angular.noop,
debug: this.debug,
trackingId: this.trackingId,
period: this.period
});
}],
ready: function (fn) {
this._ready = fn;
}
};
})
.config(['$analyticsProvider', 'googleAnalyticsCordovaProvider', function ($analyticsProvider, googleAnalyticsCordovaProvider) {
googleAnalyticsCordovaProvider.ready(function (analytics, success, failure) {
$analyticsProvider.registerPageTrack(function (path) {
analytics.trackView(path);
});
$analyticsProvider.registerEventTrack(function (action, properties) {
analytics.trackEvent(properties.category, action, properties.label, properties.value);
});
$analyticsProvider.registerSetUsername(function (userId) {
analytics.setUserId(userId);
});
});
}])
.run(['googleAnalyticsCordova', function (googleAnalyticsCordova) {
googleAnalyticsCordova.init();
}]);
})(angular);