forked from danwilson/google-analytics-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyticsService.js
112 lines (107 loc) · 3.21 KB
/
AnalyticsService.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
define(function(require) {
var Disposable = require('lavaca/util/Disposable');
var Config = require('lavaca/util/Config');
var Device = require('lavaca/env/Device');
var ga = require('google-analytics');
var AnalyticsService = Disposable.extend(function AnalyticsService() {
Disposable.call(this);
this.appId = Config.get('google_analytics_id');
this.webId = Config.get('google_analytics_web_id');
this.isWeb = this.webId.length;
if (!Device.isCordova() && this.isWeb) {
ga('create', this.webId);
}
document.addEventListener('deviceready', this.init.bind(this), false);
}, {
ready: false,
queue: [],
isWeb: false,
//isApp: false, //TODO need to research when Device.isCordova() is valid in Lavaca load flow
init: function() {
if (Device.isCordova() && analytics) {
this.ready = true;
analytics.startTrackerWithId(this.appId);
this.processQueue();
this.isWeb = false;
}
},
trackView: function(screen) {
if (Device.isCordova()) {
if (this.ready) {
analytics.trackView(screen);
} else {
this.queue.push({
action: 'trackView',
params: [screen]
});
}
} else if (this.isWeb) {
ga('send', 'pageview', {
'title': screen
});
}
},
setUserId: function() {
throw 'setUserId is not implemented for Lavaca';
},
debugMode: function() {
throw 'debugMode is not implemented for Lavaca';
},
trackEvent: function(category, action, label, value) {
action = action || '';
label = label || '';
value = value || 0;
if (Device.isCordova()) {
if (this.ready) {
analytics.trackEvent(category, action, label, value);
} else {
this.queue.push({
action: 'trackEvent',
params: [category, action, label, value]
});
}
} else if (this.isWeb) {
ga('send', {
'hitType': 'event',
'eventCategory': category,
'eventAction': action,
'eventLabel': label,
'eventValue': value
});
}
},
trackTiming: function(category, intervalInMilliseconds, name, label) {
action = action || '';
label = label || '';
value = value || 0;
if (Device.isCordova()) {
if (this.ready) {
analytics.trackTiming(category, intervalInMilliseconds, name, label);
} else {
this.queue.push({
action: 'trackTiming',
params: [category, intervalInMilliseconds, name, label]
});
}
} else if (this.isWeb) {
ga('send', {
'hitType': 'timing',
'timingCategory': category,
'timingValue': intervalInMilliseconds,
'timingVar': name,
'timingLabel': label
});
}
},
processQueue: function() {
if (this.queue) {
var emptyFunction = function() {};
for (var i = 0; i < this.queue.length; ++i) {
cordova.exec(emptyFunction, emptyFunction,
'UniversalAnalytics', this.queue[i].action, this.queue[i].params);
}
}
}
});
return new AnalyticsService();
});