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'; 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
}); });

View File

@ -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',
}); });

View File

@ -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',
}); });

View File

@ -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();

View File

@ -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,
}); });

View File

@ -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';

View File

@ -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,34 +24,27 @@ export default class Analytics {
return; return;
} }
if (platformUtilsService == null) {
this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService; this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService;
}
if (storageService == null) {
this.storageService = bgMain.storageService as StorageService; this.storageService = bgMain.storageService as StorageService;
}
if (appIdService == null) {
this.appIdService = bgMain.appIdService as AppIdService; 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) => {
async init() { await this.ga(action, param1, param2);
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;
} }
async ga(action: string, param1: any, param2?: any) {
if (this.isSafari && safari.application.activeBrowserWindow.activeTab.private) { if (this.isSafari && safari.application.activeBrowserWindow.activeTab.private) {
return; return;
} }
@ -68,15 +55,11 @@ export default class Analytics {
return; 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) { if (action !== 'send' || !param1) {
return; return;
} }
const gaAnonAppId = await this.appIdService.getAnonymousAppId();
const version = encodeURIComponent(this.appVersion); const version = encodeURIComponent(this.appVersion);
let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version; let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
@ -93,11 +76,6 @@ export default class Analytics {
const request = new XMLHttpRequest(); const request = new XMLHttpRequest();
request.open('POST', 'https://www.google-analytics.com/collect', true); request.open('POST', 'https://www.google-analytics.com/collect', true);
request.send(message); request.send(message);
};
if (this.isBackground) {
this.win[gaObj]('send', 'pageview', '/background.html');
}
} }
private gaTrackEvent(options: any) { private gaTrackEvent(options: any) {