-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
263 lines (216 loc) · 6.94 KB
/
index.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
'use strict';
var warning = require('warning');
var urljoin = require('url-join');
// api shim. used for serverside rendering and misconfigured tracker instances
var apiShim = {
_isShim: true,
track: function () {},
push: function (args) {},
setUserId: function (userId) {},
trackError: function (e) {},
connectToHistory: function (history, modifier) { return history; },
disconnectFromHistory: function () {}
};
var previousPath = null;
var unlistenFromHistory = null;
var PiwikTracker = function(opts) {
var getEnvironment = function () {
return process && process.env && process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'development';
};
var getBaseUrl = function () {
var u = ''
var url = opts.url;
if (url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1) {
u = url + '/';
}
else {
u = (('https:' == document.location.protocol) ? 'https://' + url + '/' : 'http://' + url + '/');
}
return u;
}
var piwikIsAlreadyInitialized = function () {
if (typeof window._paq === 'undefined' || typeof window._paq.push !== 'function') {
return false;
}
var hasSiteId = false;
var hasTrackerUrl = false;
if (window._paq.length === undefined) {
// piwik seems to have replaced the array by the TrackerProxy
// @see https://github.com/piwik/piwik/blob/3.x-dev/js/piwik.js#L7050
// @see https://github.com/piwik/piwik/blob/3.x-dev/js/piwik.js#L7115
return true;
}
for (var j = 0, l = window._paq.length; j < l; j++) {
if (~window._paq[j].indexOf('setSiteId')) {
hasSiteId = true
}
if (~window._paq[j].indexOf('setTrackerUrl')) {
hasTrackerUrl = true
}
if (hasTrackerUrl && hasSiteId) {
return true
}
}
return false;
};
opts = opts || {};
opts.trackErrors = ((opts.trackErrors !== undefined) ? opts.trackErrors : false);
opts.trackErrorHandler = ((opts.trackErrorHandler !== undefined) ? opts.trackErrorHandler : trackError);
opts.enableLinkTracking = ((opts.enableLinkTracking !== undefined) ? opts.enableLinkTracking : true);
opts.updateDocumentTitle = ((opts.updateDocumentTitle !== undefined) ? opts.updateDocumentTitle : true);
opts.ignoreInitialVisit = ((opts.ignoreInitialVisit !== undefined) ? opts.ignoreInitialVisit : false);
opts.injectScript = ((opts.injectScript !== undefined) ? opts.injectScript : true);
opts.clientTrackerName = ((opts.clientTrackerName !== undefined) ? opts.clientTrackerName : 'piwik.js');
opts.serverTrackerName = ((opts.serverTrackerName !== undefined) ? opts.serverTrackerName : 'piwik.php');
var alreadyInitialized = piwikIsAlreadyInitialized();
window._paq = window['_paq'] || [];
var piwikWithoutUrlOrSiteId = (!opts.url || !opts.siteId) && !alreadyInitialized;
var piwikWithoutInjectScript = !opts.injectScript && !alreadyInitialized;
if (piwikWithoutUrlOrSiteId || piwikWithoutInjectScript) {
// Only return warning if this is not in the test environment as it can break the Tests/CI.
if (getEnvironment() !== 'test') {
warning(null, 'PiwikTracker cannot be initialized! You haven\'t passed a url and siteId to it.');
}
return apiShim;
}
/**
* Adds a page view for the given location
*/
var track = function track (loc) {
var currentPath;
if (loc.path) {
currentPath = loc.path;
} else if (loc.basename) {
currentPath = urljoin(loc.basename, loc.pathname, loc.search);
} else {
currentPath = urljoin(loc.pathname, loc.search);
}
if (previousPath === currentPath) {
return;
}
if (opts.updateDocumentTitle) {
push(['setDocumentTitle', document.title]);
}
push(['setCustomUrl', currentPath]);
push(['trackPageView']);
previousPath = currentPath;
};
/*
* Pushes the specified args to the piwik tracker.
* You can use this method as the low-level api to call methods from the piwik API or call custom functions
*
* @see https://developer.piwik.org/guides/tracking-javascript-guide
*/
var push = function push (args) {
window._paq.push(args);
};
/**
* Sets a user ID to the piwik tracker.
* This method can be used after PiwikReactRouter is instantiated, for example after a user has logged in
*
* @see https://developer.piwik.org/guides/tracking-javascript-guide#user-id
*/
var setUserId = function setUserId (userId) {
window._paq.push(['setUserId', userId])
};
/**
* Tracks occurring javascript errors as a `JavaScript Error` piwik event.
*
* @see http://davidwalsh.name/track-errors-google-analytics
*/
function trackError (e, eventName) {
eventName = eventName || 'JavaScript Error';
push([
'trackEvent',
eventName,
e.message,
e.filename + ': ' + e.lineno
]);
};
/**
* Connects to the given history
*/
var connectToHistory = function (history, modifier) {
modifier = (typeof modifier === 'function') ? modifier : function (location) { return location; };
var applyModifierAndTrackLocation = function (modifier, location) {
var modifiedLocation = modifier(location);
if (modifiedLocation !== undefined) {
track(modifiedLocation);
}
else if (getEnvironment() === 'development') {
warning(null, 'The modifier given to .connectToHistory did not return any object. Please make sure to return the modified location object in your modifier.');
}
}
unlistenFromHistory = history.listen(function (location) {
applyModifierAndTrackLocation(modifier, location);
});
if (!opts.ignoreInitialVisit && history.location) {
applyModifierAndTrackLocation(modifier, history.location);
}
return history;
};
/**
* Disconnects from a previous connected history
*/
var disconnectFromHistory = function () {
if (unlistenFromHistory) {
unlistenFromHistory();
return true;
}
return false;
};
if (opts.trackErrors) {
if (window.addEventListener) {
window.addEventListener('error', opts.trackErrorHandler, false);
}
else if (window.attachEvent) {
window.attachEvent('onerror', opts.trackErrorHandler);
}
else {
window.onerror = opts.trackErrorHandler;
}
}
// piwik initializer
(function() {
var alreadyInitialized = piwikIsAlreadyInitialized();
if (!alreadyInitialized) {
var u = getBaseUrl();
push(['setSiteId', opts.siteId]);
push(['setTrackerUrl', u + opts.serverTrackerName]);
}
if (opts.userId) {
setUserId(opts.userId);
}
if (opts.enableLinkTracking) {
push(['enableLinkTracking']);
}
if (opts.injectScript && !alreadyInitialized) {
var d=document;
var g=d.createElement('script');
var s=d.getElementsByTagName('script')[0];
g.type='text/javascript';
g.defer=true;
g.async=true;
g.src=u+opts.clientTrackerName;
s.parentNode.insertBefore(g, s);
}
})();
// return api
return {
_isShim: false,
track: track,
push: push,
setUserId: setUserId,
trackError: opts.trackErrorHandler,
connectToHistory: connectToHistory,
disconnectFromHistory: disconnectFromHistory
};
};
if (typeof window === 'undefined') {
module.exports = function() {
return apiShim;
};
}
else {
module.exports = PiwikTracker;
}