/
angular-google-analytics.js
151 lines (137 loc) · 5.43 KB
/
angular-google-analytics.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
/* global angular, console */
'use strict';
angular.module('angular-google-analytics', [])
.provider('Analytics', function() {
var created = false,
trackRoutes = true,
accountId,
trackPrefix = ''',
domainName;
this._logs = [];
// config methods
this.setAccount = function(id) {
accountId = id;
return true;
};
this.trackPages = function(doTrack) {
trackRoutes = doTrack;
return true;
};
this.trackPrefix = function(prefix) {
trackPrefix = prefix;
return true;
};
this.setDomainName = function(domain) {
domainName = domain;
return true;
};
// public service
this.$get = ['$document', '$rootScope', '$location', '$window', function($document, $rootScope, $location, $window) {
// private methods
function _createScriptTag() {
// inject the google analytics tag
if (!accountId) return;
$window._gaq = [];
$window._gaq.push(['_setAccount', accountId]);
if (trackRoutes) $window._gaq.push(['_trackPageview']);
if(domainName) $window._gaq.push(['_setDomainName', domainName]);
(function() {
var document = $document[0];
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
created = true;
}
this._log = function() {
// for testing
this._logs.push(arguments);
};
this._trackPage = function(url) {
if (trackRoutes && $window._gaq) {
$window._gaq.push(['_trackPageview', trackPrefix + url]);
this._log('_trackPageview', arguments);
}
};
this._trackEvent = function(category, action, label, value) {
if ($window._gaq) {
$window._gaq.push(['_trackEvent', category, action, label, value]);
this._log('trackEvent', arguments);
}
};
/**
* Add transaction
* https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._addTrans
* @param transactionId
* @param affiliation
* @param total
* @param tax
* @param shipping
* @param city
* @param state
* @param country
* @private
*/
this._addTrans = function (transactionId, affiliation, total, tax, shipping, city, state, country) {
if ($window._gaq) {
$window._gaq.push(['_addTrans', transactionId, affiliation, total, tax, shipping, city, state, country]);
this._log('_addTrans', arguments);
}
};
/**
* Add item to transaction
* https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._addItem
* @param transactionId
* @param sku
* @param name
* @param category
* @param price
* @param quantity
* @private
*/
this._addItem = function (transactionId, sku, name, category, price, quantity) {
if ($window._gaq) {
$window._gaq.push(['_addItem', transactionId, sku, name, category, price, quantity]);
this._log('_addItem', arguments);
}
};
/**
* Track transaction
* https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._trackTrans
* @private
*/
this._trackTrans = function () {
if ($window._gaq) {
$window._gaq.push(['_trackTrans']);
}
this._log('_trackTrans', arguments);
};
// creates the ganalytics tracker
_createScriptTag();
var me = this;
// activates page tracking
if (trackRoutes) $rootScope.$on('$routeChangeSuccess', function() {
me._trackPage($location.path());
});
return {
_logs: me._logs,
trackPage: function(url) {
// add a page event
me._trackPage(url);
},
trackEvent: function(category, action, label, value) {
// add an action event
me._trackEvent(category, action, label, value);
},
addTrans: function (transactionId, affiliation, total, tax, shipping, city, state, country) {
me._addTrans(transactionId, affiliation, total, tax, shipping, city, state, country);
},
addItem: function (transactionId, sku, name, category, price, quantity) {
me._addItem(transactionId, sku, name, category, price, quantity);
},
trackTrans: function () {
me._trackTrans();
}
};
}];
});