1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

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

@@ -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,74 +24,58 @@ export default class Analytics {
return;
}
this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService;
this.storageService = bgMain.storageService as StorageService;
this.appIdService = bgMain.appIdService as AppIdService;
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');
(win as any).GoogleAnalyticsObject = gaObj;
(win as any)[gaObj] = async (action: string, param1: any, param2?: any) => {
await this.ga(action, param1, param2);
};
}
async init() {
if (this.inited) {
throw new Error('Analytics already initialized.');
}
if (!this.platformUtilsService || !this.storageService || !this.appIdService) {
async ga(action: string, param1: any, param2?: any) {
if (this.isSafari && safari.application.activeBrowserWindow.activeTab.private) {
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;
this.win[gaObj] = async (action: any, param1: any, param2: any, param3: any, param4: any) => {
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);
};
if (action !== 'send' || !param1) {
return;
}
const gaAnonAppId = await this.appIdService.getAnonymousAppId();
this.gaFunc = (action: string, param1: any, param2: any, param3: any, param: any) => {
if (action !== 'send' || !param1) {
return;
}
const version = encodeURIComponent(this.appVersion);
let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
const version = encodeURIComponent(this.appVersion);
let message = 'v=1&tid=' + this.gaTrackingId + '&cid=' + gaAnonAppId + '&cd1=' + version;
if (param1 === 'pageview' && param2) {
message += this.gaTrackPageView(param2);
} else if (typeof param1 === 'object' && param1.hitType === 'pageview') {
message += this.gaTrackPageView(param1.page);
} else if (param1 === 'event' && param2) {
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');
if (param1 === 'pageview' && param2) {
message += this.gaTrackPageView(param2);
} else if (typeof param1 === 'object' && param1.hitType === 'pageview') {
message += this.gaTrackPageView(param1.page);
} else if (param1 === 'event' && param2) {
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);
}
private gaTrackEvent(options: any) {