bitwarden-estensione-browser/src/scripts/analytics.js

79 lines
2.8 KiB
JavaScript
Raw Normal View History

(function () {
2017-09-09 21:41:49 +02:00
var bgPage = chrome.extension.getBackgroundPage();
if (!bgPage) {
return;
}
2017-09-09 21:41:49 +02:00
var gaTrackingId = bgPage.bg_utilsService.analyticsId();
var gaFunc = null;
var isFirefox = bgPage.bg_utilsService.isFirefox();
2017-09-09 21:41:49 +02:00
window.GoogleAnalyticsObject = 'ga';
window[window.GoogleAnalyticsObject] = function (action, param1, param2, param3, param4) {
if (!gaFunc) {
return;
}
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;
}
2017-09-09 21:41:49 +02:00
gaFunc(action, param1, param2, param3, param4);
});
};
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) : '');
}
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
}
2017-10-17 15:25:22 +02:00
bgPage.bg_appIdService.getAnonymousAppId().then(function (gaAnonAppId) {
2017-09-09 21:41:49 +02:00
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');
}
});
})();