mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-26 17:08:33 +01:00
refactor and cleanup analytics class
This commit is contained in:
parent
34e2cc2e39
commit
ee036db2dd
@ -1,8 +1,6 @@
|
|||||||
import MainBackground from './background/main.background';
|
import MainBackground from './background/main.background';
|
||||||
|
|
||||||
const bitwardenIsBackground = (window as any).bitwardenIsBackground = true;
|
|
||||||
const bitwardenMain = (window as any).bitwardenMain = new MainBackground();
|
const bitwardenMain = (window as any).bitwardenMain = new MainBackground();
|
||||||
|
|
||||||
bitwardenMain.bootstrap().then(() => {
|
bitwardenMain.bootstrap().then(() => {
|
||||||
// Finished bootstrapping
|
// Finished bootstrapping
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { BrowserApi } from '../browser/browserApi';
|
import { BrowserApi } from '../browser/browserApi';
|
||||||
|
|
||||||
|
import Analytics from '../scripts/analytics';
|
||||||
import MainBackground from './main.background';
|
import MainBackground from './main.background';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -16,7 +17,7 @@ export default class CommandsBackground {
|
|||||||
private isVivaldi: boolean;
|
private isVivaldi: boolean;
|
||||||
|
|
||||||
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
|
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
|
||||||
private platformUtilsService: PlatformUtilsService) {
|
private platformUtilsService: PlatformUtilsService, private analytics: Analytics) {
|
||||||
this.isSafari = this.platformUtilsService.isSafari();
|
this.isSafari = this.platformUtilsService.isSafari();
|
||||||
this.isEdge = this.platformUtilsService.isEdge();
|
this.isEdge = this.platformUtilsService.isEdge();
|
||||||
this.isVivaldi = this.platformUtilsService.isVivaldi();
|
this.isVivaldi = this.platformUtilsService.isVivaldi();
|
||||||
@ -68,7 +69,7 @@ export default class CommandsBackground {
|
|||||||
UtilsService.copyToClipboard(password);
|
UtilsService.copyToClipboard(password);
|
||||||
this.passwordGenerationService.addHistory(password);
|
this.passwordGenerationService.addHistory(password);
|
||||||
|
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Generated Password From Command',
|
eventAction: 'Generated Password From Command',
|
||||||
});
|
});
|
||||||
@ -85,7 +86,7 @@ export default class CommandsBackground {
|
|||||||
|
|
||||||
this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
|
this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
|
||||||
|
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Autofilled From Command',
|
eventAction: 'Autofilled From Command',
|
||||||
});
|
});
|
||||||
@ -98,7 +99,7 @@ export default class CommandsBackground {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.main.openPopup();
|
this.main.openPopup();
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Opened Popup From Command',
|
eventAction: 'Opened Popup From Command',
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { BrowserApi } from '../browser/browserApi';
|
import { BrowserApi } from '../browser/browserApi';
|
||||||
|
|
||||||
|
import Analytics from '../scripts/analytics';
|
||||||
import MainBackground from './main.background';
|
import MainBackground from './main.background';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -13,7 +14,7 @@ export default class ContextMenusBackground {
|
|||||||
private contextMenus: any;
|
private contextMenus: any;
|
||||||
|
|
||||||
constructor(private main: MainBackground, private cipherService: CipherService,
|
constructor(private main: MainBackground, private cipherService: CipherService,
|
||||||
private passwordGenerationService: PasswordGenerationService) {
|
private passwordGenerationService: PasswordGenerationService, private analytics: Analytics) {
|
||||||
this.contextMenus = chrome.contextMenus;
|
this.contextMenus = chrome.contextMenus;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ export default class ContextMenusBackground {
|
|||||||
UtilsService.copyToClipboard(password);
|
UtilsService.copyToClipboard(password);
|
||||||
this.passwordGenerationService.addHistory(password);
|
this.passwordGenerationService.addHistory(password);
|
||||||
|
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Generated Password From Context Menu',
|
eventAction: 'Generated Password From Context Menu',
|
||||||
});
|
});
|
||||||
@ -61,19 +62,19 @@ export default class ContextMenusBackground {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (info.parentMenuItemId === 'autofill') {
|
if (info.parentMenuItemId === 'autofill') {
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Autofilled From Context Menu',
|
eventAction: 'Autofilled From Context Menu',
|
||||||
});
|
});
|
||||||
await this.startAutofillPage(cipher);
|
await this.startAutofillPage(cipher);
|
||||||
} else if (info.parentMenuItemId === 'copy-username') {
|
} else if (info.parentMenuItemId === 'copy-username') {
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Copied Username From Context Menu',
|
eventAction: 'Copied Username From Context Menu',
|
||||||
});
|
});
|
||||||
UtilsService.copyToClipboard(cipher.login.username);
|
UtilsService.copyToClipboard(cipher.login.username);
|
||||||
} else if (info.parentMenuItemId === 'copy-password') {
|
} else if (info.parentMenuItemId === 'copy-password') {
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Copied Password From Context Menu',
|
eventAction: 'Copied Password From Context Menu',
|
||||||
});
|
});
|
||||||
|
@ -85,6 +85,7 @@ export default class MainBackground {
|
|||||||
totpService: TotpServiceAbstraction;
|
totpService: TotpServiceAbstraction;
|
||||||
autofillService: AutofillServiceAbstraction;
|
autofillService: AutofillServiceAbstraction;
|
||||||
containerService: ContainerService;
|
containerService: ContainerService;
|
||||||
|
analytics: Analytics;
|
||||||
|
|
||||||
onUpdatedRan: boolean;
|
onUpdatedRan: boolean;
|
||||||
onReplacedRan: boolean;
|
onReplacedRan: boolean;
|
||||||
@ -139,6 +140,7 @@ export default class MainBackground {
|
|||||||
this.autofillService = new AutofillService(this.cipherService, this.tokenService,
|
this.autofillService = new AutofillService(this.cipherService, this.tokenService,
|
||||||
this.totpService, this.utilsService, this.platformUtilsService);
|
this.totpService, this.utilsService, this.platformUtilsService);
|
||||||
this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService);
|
this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService);
|
||||||
|
this.analytics = new Analytics(window, this.platformUtilsService, this.storageService, this.appIdService);
|
||||||
|
|
||||||
// Other fields
|
// Other fields
|
||||||
this.isSafari = this.platformUtilsService.isSafari();
|
this.isSafari = this.platformUtilsService.isSafari();
|
||||||
@ -147,14 +149,14 @@ export default class MainBackground {
|
|||||||
|
|
||||||
// Background
|
// Background
|
||||||
this.runtimeBackground = new RuntimeBackground(this, this.autofillService, this.cipherService,
|
this.runtimeBackground = new RuntimeBackground(this, this.autofillService, this.cipherService,
|
||||||
this.platformUtilsService, this.storageService, this.i18nService);
|
this.platformUtilsService, this.storageService, this.i18nService, this.analytics);
|
||||||
this.tabsBackground = new TabsBackground(this, this.platformUtilsService);
|
this.tabsBackground = new TabsBackground(this, this.platformUtilsService);
|
||||||
this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService,
|
this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService,
|
||||||
this.platformUtilsService);
|
this.platformUtilsService, this.analytics);
|
||||||
|
|
||||||
if (!this.isSafari) {
|
if (!this.isSafari) {
|
||||||
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
|
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
|
||||||
this.passwordGenerationService);
|
this.passwordGenerationService, this.analytics);
|
||||||
this.idleBackground = new IdleBackground(this, this.lockService, this.storageService);
|
this.idleBackground = new IdleBackground(this, this.lockService, this.storageService);
|
||||||
this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService);
|
this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService);
|
||||||
this.windowsBackground = new WindowsBackground(this);
|
this.windowsBackground = new WindowsBackground(this);
|
||||||
@ -162,7 +164,7 @@ export default class MainBackground {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async bootstrap() {
|
async bootstrap() {
|
||||||
await new Analytics(window).init();
|
this.analytics.ga('send', 'pageview', '/background.html');
|
||||||
this.containerService.attachToWindow(window);
|
this.containerService.attachToWindow(window);
|
||||||
|
|
||||||
await this.runtimeBackground.init();
|
await this.runtimeBackground.init();
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
|
|
||||||
import { BrowserApi } from '../browser/browserApi';
|
import { BrowserApi } from '../browser/browserApi';
|
||||||
|
|
||||||
|
import Analytics from '../scripts/analytics';
|
||||||
import MainBackground from './main.background';
|
import MainBackground from './main.background';
|
||||||
|
|
||||||
import { AutofillService } from '../services/abstractions/autofill.service';
|
import { AutofillService } from '../services/abstractions/autofill.service';
|
||||||
@ -24,7 +25,7 @@ export default class RuntimeBackground {
|
|||||||
|
|
||||||
constructor(private main: MainBackground, private autofillService: AutofillService,
|
constructor(private main: MainBackground, private autofillService: AutofillService,
|
||||||
private cipherService: CipherService, private platformUtilsService: PlatformUtilsService,
|
private cipherService: CipherService, private platformUtilsService: PlatformUtilsService,
|
||||||
private storageService: StorageService, private i18nService: any) {
|
private storageService: StorageService, private i18nService: any, private analytics: Analytics) {
|
||||||
this.isSafari = this.platformUtilsService.isSafari();
|
this.isSafari = this.platformUtilsService.isSafari();
|
||||||
this.runtime = this.isSafari ? safari.application : chrome.runtime;
|
this.runtime = this.isSafari ? safari.application : chrome.runtime;
|
||||||
|
|
||||||
@ -190,7 +191,7 @@ export default class RuntimeBackground {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await this.cipherService.saveWithServer(cipher);
|
await this.cipherService.saveWithServer(cipher);
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'Added Login from Notification Bar',
|
eventAction: 'Added Login from Notification Bar',
|
||||||
});
|
});
|
||||||
@ -284,7 +285,7 @@ export default class RuntimeBackground {
|
|||||||
await this.reseedStorage();
|
await this.reseedStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
(window as any).ga('send', {
|
this.analytics.ga('send', {
|
||||||
hitType: 'event',
|
hitType: 'event',
|
||||||
eventAction: 'onInstalled ' + this.onInstalledReason,
|
eventAction: 'onInstalled ' + this.onInstalledReason,
|
||||||
});
|
});
|
||||||
|
@ -20,7 +20,7 @@ require('../less/libs.less');
|
|||||||
require('../less/popup.less');
|
require('../less/popup.less');
|
||||||
|
|
||||||
import Analytics from '../../scripts/analytics';
|
import Analytics from '../../scripts/analytics';
|
||||||
new Analytics(window).init(); // await?
|
new Analytics(window);
|
||||||
|
|
||||||
import DirectivesModule from './directives/directives.module';
|
import DirectivesModule from './directives/directives.module';
|
||||||
import ComponentsModule from './components/components.module';
|
import ComponentsModule from './components/components.module';
|
||||||
|
@ -7,19 +7,13 @@ import { StorageService } from 'jslib/abstractions/storage.service';
|
|||||||
const gaObj = 'ga';
|
const gaObj = 'ga';
|
||||||
|
|
||||||
export default class Analytics {
|
export default class Analytics {
|
||||||
private inited: boolean = false;
|
|
||||||
private platformUtilsService: PlatformUtilsService;
|
|
||||||
private storageService: StorageService;
|
|
||||||
private appIdService: AppIdService;
|
|
||||||
private gaTrackingId: string = null;
|
private gaTrackingId: string = null;
|
||||||
private isFirefox = false;
|
private isFirefox = false;
|
||||||
private isSafari = false;
|
private isSafari = false;
|
||||||
private gaFunc: Function = null;
|
|
||||||
private win: any;
|
|
||||||
private isBackground: boolean = false;
|
|
||||||
private appVersion: string = BrowserApi.getApplicationVersion();
|
private appVersion: string = BrowserApi.getApplicationVersion();
|
||||||
|
|
||||||
constructor(win: Window) {
|
constructor(win: Window, private platformUtilsService?: PlatformUtilsService,
|
||||||
|
private storageService?: StorageService, private appIdService?: AppIdService) {
|
||||||
const bgPage = BrowserApi.getBackgroundPage();
|
const bgPage = BrowserApi.getBackgroundPage();
|
||||||
if (!bgPage) {
|
if (!bgPage) {
|
||||||
return;
|
return;
|
||||||
@ -30,74 +24,58 @@ export default class Analytics {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService;
|
if (platformUtilsService == null) {
|
||||||
this.storageService = bgMain.storageService as StorageService;
|
this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService;
|
||||||
this.appIdService = bgMain.appIdService as AppIdService;
|
}
|
||||||
|
if (storageService == null) {
|
||||||
|
this.storageService = bgMain.storageService as StorageService;
|
||||||
|
}
|
||||||
|
if (appIdService == null) {
|
||||||
|
this.appIdService = bgMain.appIdService as AppIdService;
|
||||||
|
}
|
||||||
|
|
||||||
this.win = win;
|
|
||||||
this.isFirefox = this.platformUtilsService.isFirefox();
|
this.isFirefox = this.platformUtilsService.isFirefox();
|
||||||
this.isSafari = this.platformUtilsService.isSafari();
|
this.isSafari = this.platformUtilsService.isSafari();
|
||||||
this.gaTrackingId = this.platformUtilsService.analyticsId();
|
this.gaTrackingId = this.platformUtilsService.analyticsId();
|
||||||
this.isBackground = (typeof this.win.bitwardenIsBackground !== 'undefined');
|
|
||||||
|
(win as any).GoogleAnalyticsObject = gaObj;
|
||||||
|
(win as any)[gaObj] = async (action: string, param1: any, param2?: any) => {
|
||||||
|
await this.ga(action, param1, param2);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async ga(action: string, param1: any, param2?: any) {
|
||||||
if (this.inited) {
|
if (this.isSafari && safari.application.activeBrowserWindow.activeTab.private) {
|
||||||
throw new Error('Analytics already initialized.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.platformUtilsService || !this.storageService || !this.appIdService) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.inited = true;
|
const disabled = await this.storageService.get<boolean>('disableGa');
|
||||||
|
// Default for Firefox is disabled.
|
||||||
|
if ((this.isFirefox && disabled == null) || disabled != null && disabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.win.GoogleAnalyticsObject = gaObj;
|
if (action !== 'send' || !param1) {
|
||||||
this.win[gaObj] = async (action: any, param1: any, param2: any, param3: any, param4: any) => {
|
return;
|
||||||
if (!this.gaFunc) {
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isSafari && safari.application.activeBrowserWindow.activeTab.private) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const disabled = await this.storageService.get<boolean>('disableGa');
|
|
||||||
// Default for Firefox is disabled.
|
|
||||||
if ((this.isFirefox && disabled == null) || disabled != null && disabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.gaFunc(action, param1, param2, param3, param4);
|
|
||||||
};
|
|
||||||
|
|
||||||
const gaAnonAppId = await this.appIdService.getAnonymousAppId();
|
const gaAnonAppId = await this.appIdService.getAnonymousAppId();
|
||||||
this.gaFunc = (action: string, param1: any, param2: any, param3: any, param: any) => {
|
const version = encodeURIComponent(this.appVersion);
|
||||||
if (action !== 'send' || !param1) {
|
let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const version = encodeURIComponent(this.appVersion);
|
if (param1 === 'pageview' && param2) {
|
||||||
let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
|
message += this.gaTrackPageView(param2);
|
||||||
|
} else if (typeof param1 === 'object' && param1.hitType === 'pageview') {
|
||||||
if (param1 === 'pageview' && param2) {
|
message += this.gaTrackPageView(param1.page);
|
||||||
message += this.gaTrackPageView(param2);
|
} else if (param1 === 'event' && param2) {
|
||||||
} else if (typeof param1 === 'object' && param1.hitType === 'pageview') {
|
message += this.gaTrackEvent(param2);
|
||||||
message += this.gaTrackPageView(param1.page);
|
} else if (typeof param1 === 'object' && param1.hitType === 'event') {
|
||||||
} else if (param1 === 'event' && param2) {
|
message += this.gaTrackEvent(param1);
|
||||||
message += this.gaTrackEvent(param2);
|
|
||||||
} else if (typeof param1 === 'object' && param1.hitType === 'event') {
|
|
||||||
message += this.gaTrackEvent(param1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const request = new XMLHttpRequest();
|
|
||||||
request.open('POST', 'https://www.google-analytics.com/collect', true);
|
|
||||||
request.send(message);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.isBackground) {
|
|
||||||
this.win[gaObj]('send', 'pageview', '/background.html');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const request = new XMLHttpRequest();
|
||||||
|
request.open('POST', 'https://www.google-analytics.com/collect', true);
|
||||||
|
request.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private gaTrackEvent(options: any) {
|
private gaTrackEvent(options: any) {
|
||||||
|
Loading…
Reference in New Issue
Block a user