2017-09-09 21:41:49 +02:00
|
|
|
|
(function () {
|
|
|
|
|
var bgPage = chrome.extension.getBackgroundPage();
|
|
|
|
|
if (!bgPage) {
|
2016-10-18 04:26:04 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-28 04:44:42 +02:00
|
|
|
|
|
2017-09-09 21:41:49 +02:00
|
|
|
|
var gaTrackingId = bgPage.bg_utilsService.analyticsId();
|
|
|
|
|
var gaFunc = null;
|
|
|
|
|
var isFirefox = bgPage.bg_utilsService.isFirefox();
|
2016-10-19 01:57:15 +02:00
|
|
|
|
|
2017-09-09 21:41:49 +02:00
|
|
|
|
window.GoogleAnalyticsObject = 'ga';
|
|
|
|
|
window[window.GoogleAnalyticsObject] = function (action, param1, param2, param3, param4) {
|
|
|
|
|
if (!gaFunc) {
|
2016-10-18 04:26:04 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-28 04:44:42 +02:00
|
|
|
|
|
2017-09-09 21:41:49 +02:00
|
|
|
|
chrome.storage.local.get('disableGa', function (obj) {
|
|
|
|
|
// Default for Firefox is disabled.
|
|
|
|
|
if ((isFirefox && obj.disableGa === undefined) || obj.disableGa) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-10-18 04:26:04 +02:00
|
|
|
|
|
2017-09-09 21:41:49 +02:00
|
|
|
|
gaFunc(action, param1, param2, param3, param4);
|
|
|
|
|
});
|
2016-10-18 04:26:04 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-09-09 21:41:49 +02:00
|
|
|
|
function gaTrackEvent(options) {
|
|
|
|
|
return '&t=event&ec=' + (options.eventCategory ? encodeURIComponent(options.eventCategory) : 'Event') +
|
|
|
|
|
'&ea=' + encodeURIComponent(options.eventAction) +
|
|
|
|
|
(options.eventLabel ? '&el=' + encodeURIComponent(options.eventLabel) : '') +
|
|
|
|
|
(options.eventValue ? '&ev=' + encodeURIComponent(options.eventValue) : '') +
|
2017-10-05 21:59:45 +02:00
|
|
|
|
(options.page ? '&dp=' + cleanPagePath(options.page) : '');
|
2016-10-18 04:26:04 +02:00
|
|
|
|
}
|
2017-09-09 21:41:49 +02:00
|
|
|
|
|
|
|
|
|
function gaTrackPageView(pagePath) {
|
2017-10-05 21:59:45 +02:00
|
|
|
|
return '&t=pageview&dp=' + cleanPagePath(pagePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanPagePath(pagePath) {
|
|
|
|
|
var paramIndex = pagePath.indexOf('?');
|
|
|
|
|
if (paramIndex > -1) {
|
|
|
|
|
pagePath = pagePath.substring(0, paramIndex);
|
|
|
|
|
}
|
2017-10-06 16:36:45 +02:00
|
|
|
|
return encodeURIComponent(pagePath);
|
2017-09-09 21:41:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bgPage.bg_appIdService.getAnonymousAppId(function (gaAnonAppId) {
|
|
|
|
|
gaFunc = function (action, param1, param2, param3, param4) {
|
|
|
|
|
if (action !== 'send' || !param1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var version = encodeURIComponent(chrome.runtime.getManifest().version);
|
|
|
|
|
var message = 'v=1&tid=' + gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
|
|
|
|
|
|
|
|
|
|
if (param1 === 'pageview' && param2) {
|
|
|
|
|
message += gaTrackPageView(param2);
|
|
|
|
|
}
|
2017-10-05 21:59:45 +02:00
|
|
|
|
else if (typeof param1 === 'object' && param1.hitType === 'pageview') {
|
|
|
|
|
message += gaTrackPageView(param1.page);
|
|
|
|
|
}
|
2017-09-09 21:41:49 +02:00
|
|
|
|
else if (param1 === 'event' && param2) {
|
|
|
|
|
message += gaTrackEvent(param2);
|
|
|
|
|
}
|
|
|
|
|
else if (typeof param1 === 'object' && param1.hitType === 'event') {
|
|
|
|
|
message += gaTrackEvent(param1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
|
request.open('POST', 'https://www.google-analytics.com/collect', true);
|
|
|
|
|
request.send(message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (typeof bg_isBackground !== 'undefined') {
|
|
|
|
|
ga('send', 'pageview', '/background.html');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})();
|