-
Notifications
You must be signed in to change notification settings - Fork 5
/
GoogleAnalytics.js
192 lines (165 loc) · 5.21 KB
/
GoogleAnalytics.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*!
* Ext.ux.GoogleAnalytics
* http://github.com/ahj/Ext.ux.Analytics
*
* Copyright 2014 Alun Huw Jones
* Released under the MIT license
* Check MIT-LICENSE.txt
*/
/*
* @class Ext.ux.GoogleAnalytics
* @extend Ext.app.Controller
*
* Enables Google Analytics integration for Ext JS 4 MVC architecture.
*
* Ext.application({
* name: 'MyApp',
* ...
* paths: {
* 'Ext.ux': 'app/ux'
* },
* GoogleAnalytics: {
* trackingCode: 'your tracking code'
* }
* });
*
* @docauthor Alun Huw Jones
*/
Ext.define('Ext.ux.GoogleAnalytics', {
singleton: true,
alternateClassName: 'Ext.GoogleAnalytics',
mixins: {
observable: 'Ext.util.Observable'
},
requires: [
'Ext.app.Application'
],
// @private
constructor: function() {
var me = this;
me.ready = false;
me.configured = false;
me.GoogleAnalytics = {};
me.mixins.observable.constructor.call(me);
},
/**
* Processes the config for the given app.
* @private
*/
init: function(app) {
var me = this;
if (!app || !app.GoogleAnalytics) {
return;
}
me.processConfig(app);
if (me.ready || !me.configured) {
return;
}
me.ready = true;
me.addEvents(
/**
* @event ga_track_event
* Fires when an event is tracked
* @param {String} category
* @param {String} action
*/
'ga_track_event'
);
Ext.onReady(function() {
// Ensure that the globally-scoped queue variable is defined
var _q = _gaq || [],
url = (document.location.protocol === 'https:')
? https://ssl.google-analytics.com/ga.js
: http://www.google-analytics.com/ga.js;
_q.push(['_setAccount', me.trackingCode]);
_q.push(['trackPageview']);
me._gas(url);
});
},
/**
* Validate configuration.
* @private
*/
processConfig: function(app) {
var me = this,
config = app.GoogleAnalytics,
events = config.events || [];
if (!config.trackingCode) {
Ext.log.error('ga: tracking code is missing from config');
return false;
}
me.trackingCode = config.trackingCode;
me.configured = true;
},
/**
* Method used to load in to the current page the Google Analytics
* JavaScript code needed to support reading the _gmq queue and
* pushing items found there to the Google Analytics servers
* using Ajax calls.
*
* @param {String} url The url of a JavaScript file to be loaded
*/
_gas: function(url) {
setTimeout(function() {
var d = document,
f = d.getElementsByTagName('script')[0],
s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = url;
f.parentNode.insertBefore(s, f);
}, 1);
},
/**
* Issues a track event to the Google Analytics server.
*
* @param {String} category The name you supply for the group of objects you want to track.
* @param {String} action A string that is uniquely paired with each category and commonly used
* to define the type of user interaction for the web object.
* @param {String} label An optional string to provide additional dimensions to the event data.
* @param {Number} value An integer that you can use to provide additional dimensions to the event data.
* @param {Boolean) noninteraction A boolean that whe set to true, indicates that the event hit
* will not be used in bounce-rate calculation.
* @return {Boolean} true if the function was successful otherwise false
*/
trackEvent: function(category, action, label, value, noninteraction) {
var me = this,
args = ['_trackEvent'];
if (!category || !Ext.isString(category)) {
Ext.log.error('trackEvent: category arg not defined or not a string');
return false;
}
args.push(category);
if (!action || !Ext.isString(action)) {
Ext.log.error('trackEvent: action arg not defined or not a string');
return false;
}
args.push(action);
if (label) {
args.push(label);
}
if (value) {
args.push(value);
}
if (noninteraction) {
args.push(noninteraction);
}
_gaq.push(args);
me.fireEvent('ga_track_event', category, action);
return true;
}
},
function() {
/*
* Patch Ext.Application to auto-initialize Google Analytics tracker
*/
Ext.override(Ext.app.Application, {
enableGoogleAnalytics: true,
onBeforeLaunch: function() {
this.callOverridden();
if (this.enableGoogleAnalytics) {
Ext.ux.GoogleAnalytics.init(this);
}
}
});
});