-
Notifications
You must be signed in to change notification settings - Fork 51
/
angulartics-ga.js
executable file
·96 lines (83 loc) · 3.34 KB
/
angulartics-ga.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
/**
* @license Angulartics v0.17.2
* (c) 2013 Luis Farzati http://luisfarzati.github.io/angulartics
* Universal Analytics update contributed by http://github.com/willmcclellan
* License: MIT
*/
(function(angular) {
'use strict';
/**
* @ngdoc overview
* @name angulartics.google.analytics
* Enables analytics support for Google Analytics (http://google.com/analytics)
*/
angular.module('angulartics.google.analytics', ['angulartics'])
.config(['$analyticsProvider', function ($analyticsProvider) {
// GA already supports buffered invocations so we don't need
// to wrap these inside angulartics.waitForVendorApi
$analyticsProvider.settings.trackRelativePath = true;
// Set the default settings for this module
$analyticsProvider.settings.ga = {
// array of additional account names (only works for analyticsjs)
additionalAccountNames: undefined
};
$analyticsProvider.registerPageTrack(function (path) {
if (window._gaq) _gaq.push(['_trackPageview', path]);
if (window.ga) {
ga('send', 'pageview', path);
angular.forEach($analyticsProvider.settings.ga.additionalAccountNames, function (accountName){
ga(accountName +'.send', 'pageview', path);
});
}
});
/**
* Track Event in GA
* @name eventTrack
*
* @param {string} action Required 'action' (string) associated with the event
* @param {object} properties Comprised of the mandatory field 'category' (string) and optional fields 'label' (string), 'value' (integer) and 'noninteraction' (boolean)
*
* @link https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking
*
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/events
*/
$analyticsProvider.registerEventTrack(function (action, properties) {
// do nothing if there is no category (it's required by GA)
if (!properties || !properties.category) {
return;
}
// GA requires that eventValue be an integer, see:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#eventValue
// https://github.com/luisfarzati/angulartics/issues/81
if (properties.value) {
var parsed = parseInt(properties.value, 10);
properties.value = isNaN(parsed) ? 0 : parsed;
}
if (window.ga) {
var eventOptions = {
eventCategory: properties.category || null,
eventAction: action || null,
eventLabel: properties.label || null,
eventValue: properties.value || null,
nonInteraction: properties.noninteraction || null
};
// add custom dimensions and metrics
for(var idx = 1; idx<=20;idx++) {
if (properties['dimension' +idx.toString()]) {
eventOptions['dimension' +idx.toString()] = properties['dimension' +idx.toString()];
}
if (properties['metric' +idx.toString()]) {
eventOptions['metric' +idx.toString()] = properties['metric' +idx.toString()];
}
}
ga('send', 'event', eventOptions);
angular.forEach($analyticsProvider.settings.ga.additionalAccountNames, function (accountName){
ga(accountName +'.send', 'event', eventOptions);
});
}
else if (window._gaq) {
_gaq.push(['_trackEvent', properties.category, action, properties.label, properties.value, properties.noninteraction]);
}
});
}]);
})(angular);