extract analytics class to jslib

This commit is contained in:
Kyle Spearrin 2018-01-26 10:48:32 -05:00
parent f2f34c8e70
commit caf45ad484
8 changed files with 26 additions and 107 deletions

View File

@ -1,8 +1,9 @@
import { BrowserApi } from '../browser/browserApi';
import Analytics from '../scripts/analytics';
import MainBackground from './main.background';
import { Analytics } from 'jslib/misc';
import {
PasswordGenerationService,
PlatformUtilsService,

View File

@ -1,8 +1,9 @@
import { BrowserApi } from '../browser/browserApi';
import Analytics from '../scripts/analytics';
import MainBackground from './main.background';
import { Analytics } from 'jslib/misc';
import {
CipherService,
PasswordGenerationService,

View File

@ -41,7 +41,7 @@ import {
UtilsService as UtilsServiceAbstraction,
} from 'jslib/abstractions';
import Analytics from '../scripts/analytics';
import { Analytics } from 'jslib/misc';
import { BrowserApi } from '../browser/browserApi';
@ -140,7 +140,8 @@ 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);
this.analytics = new Analytics(window, () => BrowserApi.gaFilter(), this.platformUtilsService,
this.storageService, this.appIdService);
// Other fields
this.isSafari = this.platformUtilsService.isSafari();

View File

@ -6,6 +6,8 @@ import { LoginView } from 'jslib/models/view/loginView';
import { ConstantsService } from 'jslib/services/constants.service';
import { UtilsService } from 'jslib/services/utils.service';
import { Analytics } from 'jslib/misc';
import {
CipherService,
PlatformUtilsService,
@ -14,7 +16,6 @@ import {
import { BrowserApi } from '../browser/browserApi';
import Analytics from '../scripts/analytics';
import MainBackground from './main.background';
import { AutofillService } from '../services/abstractions/autofill.service';

View File

@ -249,4 +249,8 @@ export class BrowserApi {
safariTab: tab,
};
}
static gaFilter() {
return BrowserApi.isSafariApi && safari.application.activeBrowserWindow.activeTab.private;
}
}

View File

@ -18,9 +18,6 @@ require('../../scripts/duo.js');
require('../less/libs.less');
require('../less/popup.less');
import Analytics from '../../scripts/analytics';
new Analytics(window);
import DirectivesModule from './directives/directives.module';
import ComponentsModule from './components/components.module';
import ToolsModule from './tools/tools.module';
@ -35,6 +32,15 @@ window.BrowserApi = BrowserApi;
import { U2f } from '../../scripts/u2f';
window.U2f = U2f;
import { Analytics } from '../../../node_modules/@bitwarden/jslib/src/misc/analytics';
new Analytics(window, () => BrowserApi.gaFilter(), null, null, null, () => {
const bgPage = BrowserApi.getBackgroundPage();
if (!bgPage || !bgPage.bitwardenMain) {
throw 'Cannot resolve background page main.';
}
return bgPage.bitwardenMain;
});
// Model imports
import { Attachment } from '../../../node_modules/@bitwarden/jslib/src/models/domain/attachment';
import { Card } from '../../../node_modules/@bitwarden/jslib/src/models/domain/card';

View File

@ -1,99 +0,0 @@
import { BrowserApi } from '../browser/browserApi';
import { AppIdService } from 'jslib/abstractions/appId.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { StorageService } from 'jslib/abstractions/storage.service';
const gaObj = 'ga';
export default class Analytics {
private gaTrackingId: string = null;
private isFirefox = false;
private isSafari = false;
private appVersion: string = BrowserApi.getApplicationVersion();
constructor(win: Window, private platformUtilsService?: PlatformUtilsService,
private storageService?: StorageService, private appIdService?: AppIdService) {
const bgPage = BrowserApi.getBackgroundPage();
if (!bgPage) {
return;
}
const bgMain = bgPage.bitwardenMain;
if (platformUtilsService == null && bgMain) {
this.platformUtilsService = bgMain.platformUtilsService as PlatformUtilsService;
}
if (storageService == null && bgMain) {
this.storageService = bgMain.storageService as StorageService;
}
if (appIdService == null && bgMain) {
this.appIdService = bgMain.appIdService as AppIdService;
}
this.isFirefox = this.platformUtilsService.isFirefox();
this.isSafari = this.platformUtilsService.isSafari();
this.gaTrackingId = this.platformUtilsService.analyticsId();
(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;
}
const disabled = await this.storageService.get<boolean>('disableGa');
// Default for Firefox is disabled.
if ((this.isFirefox && disabled == null) || disabled != null && disabled) {
return;
}
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;
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) {
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) : '') +
(options.page ? '&dp=' + this.cleanPagePath(options.page) : '');
}
private gaTrackPageView(pagePath: string) {
return '&t=pageview&dp=' + this.cleanPagePath(pagePath);
}
private cleanPagePath(pagePath: string) {
const paramIndex = pagePath.indexOf('?');
if (paramIndex > -1) {
pagePath = pagePath.substring(0, paramIndex);
}
if (pagePath.indexOf('!/') === 0) {
pagePath = pagePath.substring(1);
}
return encodeURIComponent(pagePath);
}
}

View File

@ -152,6 +152,10 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
// TODO
}
getApplicationVersion(): string {
return BrowserApi.getApplicationVersion();
}
private sidebarViewName(): string {
if ((window as any).chrome.sidebarAction && this.isFirefox()) {
return 'sidebar';