refactor and cleanup analytics class

This commit is contained in:
Kyle Spearrin 2018-01-19 16:19:24 -05:00
parent 34e2cc2e39
commit ee036db2dd
7 changed files with 62 additions and 81 deletions

View File

@ -1,8 +1,6 @@
import MainBackground from './background/main.background';
const bitwardenIsBackground = (window as any).bitwardenIsBackground = true;
const bitwardenMain = (window as any).bitwardenMain = new MainBackground();
bitwardenMain.bootstrap().then(() => {
// Finished bootstrapping
});

View File

@ -1,5 +1,6 @@
import { BrowserApi } from '../browser/browserApi';
import Analytics from '../scripts/analytics';
import MainBackground from './main.background';
import {
@ -16,7 +17,7 @@ export default class CommandsBackground {
private isVivaldi: boolean;
constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService,
private platformUtilsService: PlatformUtilsService) {
private platformUtilsService: PlatformUtilsService, private analytics: Analytics) {
this.isSafari = this.platformUtilsService.isSafari();
this.isEdge = this.platformUtilsService.isEdge();
this.isVivaldi = this.platformUtilsService.isVivaldi();
@ -68,7 +69,7 @@ export default class CommandsBackground {
UtilsService.copyToClipboard(password);
this.passwordGenerationService.addHistory(password);
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Generated Password From Command',
});
@ -85,7 +86,7 @@ export default class CommandsBackground {
this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd');
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Autofilled From Command',
});
@ -98,7 +99,7 @@ export default class CommandsBackground {
}
this.main.openPopup();
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Opened Popup From Command',
});

View File

@ -1,5 +1,6 @@
import { BrowserApi } from '../browser/browserApi';
import Analytics from '../scripts/analytics';
import MainBackground from './main.background';
import {
@ -13,7 +14,7 @@ export default class ContextMenusBackground {
private contextMenus: any;
constructor(private main: MainBackground, private cipherService: CipherService,
private passwordGenerationService: PasswordGenerationService) {
private passwordGenerationService: PasswordGenerationService, private analytics: Analytics) {
this.contextMenus = chrome.contextMenus;
}
@ -38,7 +39,7 @@ export default class ContextMenusBackground {
UtilsService.copyToClipboard(password);
this.passwordGenerationService.addHistory(password);
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Generated Password From Context Menu',
});
@ -61,19 +62,19 @@ export default class ContextMenusBackground {
}
if (info.parentMenuItemId === 'autofill') {
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Autofilled From Context Menu',
});
await this.startAutofillPage(cipher);
} else if (info.parentMenuItemId === 'copy-username') {
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Copied Username From Context Menu',
});
UtilsService.copyToClipboard(cipher.login.username);
} else if (info.parentMenuItemId === 'copy-password') {
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Copied Password From Context Menu',
});

View File

@ -85,6 +85,7 @@ export default class MainBackground {
totpService: TotpServiceAbstraction;
autofillService: AutofillServiceAbstraction;
containerService: ContainerService;
analytics: Analytics;
onUpdatedRan: boolean;
onReplacedRan: boolean;
@ -139,6 +140,7 @@ export default class MainBackground {
this.autofillService = new AutofillService(this.cipherService, this.tokenService,
this.totpService, this.utilsService, this.platformUtilsService);
this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService);
this.analytics = new Analytics(window, this.platformUtilsService, this.storageService, this.appIdService);
// Other fields
this.isSafari = this.platformUtilsService.isSafari();
@ -147,14 +149,14 @@ export default class MainBackground {
// Background
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.commandsBackground = new CommandsBackground(this, this.passwordGenerationService,
this.platformUtilsService);
this.platformUtilsService, this.analytics);
if (!this.isSafari) {
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
this.passwordGenerationService);
this.passwordGenerationService, this.analytics);
this.idleBackground = new IdleBackground(this, this.lockService, this.storageService);
this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService);
this.windowsBackground = new WindowsBackground(this);
@ -162,7 +164,7 @@ export default class MainBackground {
}
async bootstrap() {
await new Analytics(window).init();
this.analytics.ga('send', 'pageview', '/background.html');
this.containerService.attachToWindow(window);
await this.runtimeBackground.init();

View File

@ -11,6 +11,7 @@ import {
import { BrowserApi } from '../browser/browserApi';
import Analytics from '../scripts/analytics';
import MainBackground from './main.background';
import { AutofillService } from '../services/abstractions/autofill.service';
@ -24,7 +25,7 @@ export default class RuntimeBackground {
constructor(private main: MainBackground, private autofillService: AutofillService,
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.runtime = this.isSafari ? safari.application : chrome.runtime;
@ -190,7 +191,7 @@ export default class RuntimeBackground {
});
await this.cipherService.saveWithServer(cipher);
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'Added Login from Notification Bar',
});
@ -284,7 +285,7 @@ export default class RuntimeBackground {
await this.reseedStorage();
}
(window as any).ga('send', {
this.analytics.ga('send', {
hitType: 'event',
eventAction: 'onInstalled ' + this.onInstalledReason,
});

View File

@ -20,7 +20,7 @@ require('../less/libs.less');
require('../less/popup.less');
import Analytics from '../../scripts/analytics';
new Analytics(window).init(); // await?
new Analytics(window);
import DirectivesModule from './directives/directives.module';
import ComponentsModule from './components/components.module';

View File

@ -7,19 +7,13 @@ import { StorageService } from 'jslib/abstractions/storage.service';
const gaObj = 'ga';
export default class Analytics {
private inited: boolean = false;
private platformUtilsService: PlatformUtilsService;
private storageService: StorageService;
private appIdService: AppIdService;
private gaTrackingId: string = null;
private isFirefox = false;
private isSafari = false;
private gaFunc: Function = null;
private win: any;
private isBackground: boolean = false;
private appVersion: string = BrowserApi.getApplicationVersion();
constructor(win: Window) {
constructor(win: Window, private platformUtilsService?: PlatformUtilsService,
private storageService?: StorageService, private appIdService?: AppIdService) {
const bgPage = BrowserApi.getBackgroundPage();
if (!bgPage) {
return;
@ -30,34 +24,27 @@ export default class Analytics {
return;
}
if (platformUtilsService == null) {
this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService;
}
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.isSafari = this.platformUtilsService.isSafari();
this.gaTrackingId = this.platformUtilsService.analyticsId();
this.isBackground = (typeof this.win.bitwardenIsBackground !== 'undefined');
}
async init() {
if (this.inited) {
throw new Error('Analytics already initialized.');
}
if (!this.platformUtilsService || !this.storageService || !this.appIdService) {
return;
}
this.inited = true;
this.win.GoogleAnalyticsObject = gaObj;
this.win[gaObj] = async (action: any, param1: any, param2: any, param3: any, param4: any) => {
if (!this.gaFunc) {
return;
(win as any).GoogleAnalyticsObject = gaObj;
(win as any)[gaObj] = async (action: string, param1: any, param2?: any) => {
await this.ga(action, param1, param2);
};
}
async ga(action: string, param1: any, param2?: any) {
if (this.isSafari && safari.application.activeBrowserWindow.activeTab.private) {
return;
}
@ -68,15 +55,11 @@ export default class Analytics {
return;
}
this.gaFunc(action, param1, param2, param3, param4);
};
const gaAnonAppId = await this.appIdService.getAnonymousAppId();
this.gaFunc = (action: string, param1: any, param2: any, param3: any, param: any) => {
if (action !== 'send' || !param1) {
return;
}
const gaAnonAppId = await this.appIdService.getAnonymousAppId();
const version = encodeURIComponent(this.appVersion);
let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
@ -93,11 +76,6 @@ export default class Analytics {
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');
}
}
private gaTrackEvent(options: any) {