-
Notifications
You must be signed in to change notification settings - Fork 0
/
angulartics.js
266 lines (240 loc) · 10 KB
/
angulartics.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @license Angulartics v0.16.3
* (c) 2013 Luis Farzati http://luisfarzati.github.io/angulartics
* License: MIT
*/
(function(angular, analytics) {
'use strict';
var angulartics = window.angulartics || (window.angulartics = {});
angulartics.waitForVendorCount = 0;
angulartics.waitForVendorApi = function (objectName, delay, containsField, registerFn, onTimeout) {
if (!onTimeout) { angulartics.waitForVendorCount++; }
if (!registerFn) { registerFn = containsField; containsField = undefined; }
if (!Object.prototype.hasOwnProperty.call(window, objectName) || (containsField !== undefined && window[objectName][containsField] === undefined)) {
setTimeout(function () { angulartics.waitForVendorApi(objectName, delay, containsField, registerFn, true); }, delay);
}
else {
angulartics.waitForVendorCount--;
registerFn(window[objectName]);
}
};
/**
* @ngdoc overview
* @name angulartics
*/
angular.module('angulartics', [])
.provider('$analytics', function () {
var settings = {
pageTracking: {
autoTrackFirstPage: true,
autoTrackVirtualPages: true,
trackRelativePath: false,
autoBasePath: false,
basePath: '',
customBasePath: ''
},
eventTracking: {},
bufferFlushDelay: 1000 // Support only one configuration for buffer flush delay to simplify buffering
};
// List of known handlers that plugins can register themselves for
var knownHandlers = [
'pageTrack',
'eventTrack',
'setUsername',
'setUserProperties',
'setUserPropertiesOnce',
'setSuperProperties',
'setSuperPropertiesOnce'
];
// Cache and handler properties will match values in 'knownHandlers' as the buffering functons are installed.
var cache = {};
var handlers = {};
// General buffering handler
var bufferedHandler = function(handlerName){
return function(){
if(angulartics.waitForVendorCount){
if(!cache[handlerName]){ cache[handlerName] = []; }
cache[handlerName].push(arguments);
}
};
};
// As handlers are installed by plugins, they get pushed into a list and invoked in order.
var updateHandlers = function(handlerName, fn){
if(!handlers[handlerName]){
handlers[handlerName] = [];
}
handlers[handlerName].push(fn);
return function(){
var handlerArgs = arguments;
angular.forEach(handlers[handlerName], function(handler){
handler.apply(this, handlerArgs);
}, this);
};
};
// The api (returned by this provider) gets populated with handlers below.
var api = {
settings: settings
};
// Will run setTimeout if delay is > 0
// Runs immediately if no delay to make sure cache/buffer is flushed before anything else.
// Plugins should take care to register handlers by order of precedence.
var onTimeout = function(fn, delay){
if(delay){
setTimeout(fn, delay);
} else {
fn();
}
};
var provider = {
$get: function() { return api; },
api: api,
settings: settings,
virtualPageviews: function (value) { this.settings.pageTracking.autoTrackVirtualPages = value; },
firstPageview: function (value) { this.settings.pageTracking.autoTrackFirstPage = value; },
withBase: function (value) { this.settings.pageTracking.basePath = (value) ? angular.element('base').attr('href').slice(0, -1) : ''; },
withAutoBase: function (value) { this.settings.pageTracking.autoBasePath = value; },
customBasePath: function (value) { this.settings.pageTracking.customBasePath = value; },
};
// General function to register plugin handlers. Flushes buffers immediately upon registration according to the specified delay.
var register = function(handlerName, fn){
api[handlerName] = updateHandlers(handlerName, fn);
var handlerSettings = settings[handlerName];
var handlerDelay = (handlerSettings) ? handlerSettings.bufferFlushDelay : null;
var delay = (handlerDelay !== null) ? handlerDelay : settings.bufferFlushDelay;
angular.forEach(cache[handlerName], function (args, index) {
onTimeout(function () { fn.apply(this, args); }, index * delay);
});
};
var capitalize = function (input) {
return input.replace(/^./, function (match) {
return match.toUpperCase();
});
};
// Adds to the provider a 'register#{handlerName}' function that manages multiple plugins and buffer flushing.
var installHandlerRegisterFunction = function(handlerName){
var registerName = 'register'+capitalize(handlerName);
provider[registerName] = function(fn){
register(handlerName, fn);
};
api[handlerName] = updateHandlers(handlerName, bufferedHandler(handlerName));
};
// Set up register functions for each known handler
angular.forEach(knownHandlers, installHandlerRegisterFunction);
return provider;
})
.run(['$rootScope', '$location', '$window', '$analytics', '$injector', function ($rootScope, $location, $window, $analytics, $injector) {
if ($analytics.settings.pageTracking.autoTrackFirstPage) {
/* Only track the 'first page' if there are no routes or states on the page */
var noRoutesOrStates = true;
if ($injector.has('$route')) {
var $route = $injector.get('$route');
for (var route in $route.routes) {
noRoutesOrStates = false;
break;
}
} else if ($injector.has('$state')) {
var $state = $injector.get('$state');
for (var state in $state.get()) {
noRoutesOrStates = false;
break;
}
} else {
noRoutesOrStates = false;
}
if (noRoutesOrStates) {
if ($analytics.settings.pageTracking.autoBasePath) {
$analytics.settings.pageTracking.basePath = $window.location.pathname;
}
if ($analytics.settings.trackRelativePath) {
var url = $analytics.settings.pageTracking.basePath + $location.url();
$analytics.pageTrack(url);
} else {
$analytics.pageTrack($location.absUrl());
}
}
}
if ($analytics.settings.pageTracking.autoTrackVirtualPages) {
if ($analytics.settings.pageTracking.customBasePath){
$analytics.settings.pageTracking.basePath = $analytics.settings.pageTracking.customBasePath;
}
if ($analytics.settings.pageTracking.autoBasePath) {
/* Add the full route to the base. */
$analytics.settings.pageTracking.basePath = $window.location.pathname;
}
if ($injector.has('$route')) {
$rootScope.$on('$routeChangeSuccess', function (event, current) {
if (current && (current.$$route||current).redirectTo) return;
var url = $analytics.settings.pageTracking.basePath + $location.url();
$analytics.pageTrack(url);
});
}
if ($injector.has('$state')) {
$rootScope.$on('$stateChangeSuccess', function (event, current) {
var url = $analytics.settings.pageTracking.basePath + $location.url();
$analytics.pageTrack(url);
});
}
}
}])
.directive('analyticsOn', ['$analytics', '$timeout', function ($analytics, $timeout) {
function isCommand(element) {
return ['a:','button:','button:button','button:submit','input:button','input:submit'].indexOf(
element.tagName.toLowerCase()+':'+(element.type||'')) >= 0;
}
function inferEventType(element) {
if (isCommand(element)) return 'click';
return 'click';
}
function inferEventName(element) {
if (isCommand(element)) return element.innerText || element.value;
return element.id || element.name || element.tagName;
}
function isProperty(name) {
return name.substr(0, 9) === 'analytics' && ['On', 'Event', 'If', 'Properties', 'EventType'].indexOf(name.substr(9)) === -1;
}
function propertyName(name) {
var s = name.slice(9); // slice off the 'analytics' prefix
if (typeof s !== 'undefined' && s!==null && s.length > 0) {
return s.substring(0, 1).toLowerCase() + s.substring(1);
}
else {
return s;
}
}
return {
restrict: 'A',
scope: true,
link: function ($scope, $element, $attrs) {
var eventType = $attrs.analyticsOn || inferEventType($element[0]);
$scope.$analytics = {};
angular.forEach($attrs.$attr, function(attr, name) {
if (isProperty(name)) {
$scope.$analytics[propertyName(name)] = $attrs[name];
$attrs.$observe(name, function(value){
$scope.$analytics[propertyName(name)] = value;
});
}
});
angular.element($element[0]).bind(eventType, function ($event) {
var eventName = $attrs.analyticsEvent || inferEventName($element[0]);
var properties = {};
$scope.$analytics.eventType = $event.type;
if($attrs.analyticsIf){
if(! $scope.$eval($attrs.analyticsIf)){
return; // Cancel this event if we don't pass the analytics-if condition
}
}
// Allow components to pass through an expression that gets merged on to the event properties
// eg. analytics-properites='myComponentScope.someConfigExpression.$analyticsProperties'
if($attrs.analyticsProperties){
angular.extend($scope.$analytics, $scope.$eval($attrs.analyticsProperties));
}
$analytics.eventTrack(eventName, $scope.$analytics);
});
}
};
}]);
})(angular);
if(module && module.exports){
module.exports = 'angulartics';
}