From caf45ad484e5f3043bb623af19b89c42bebd0212 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 26 Jan 2018 10:48:32 -0500 Subject: [PATCH] extract analytics class to jslib --- src/background/commands.background.ts | 3 +- src/background/contextMenus.background.ts | 3 +- src/background/main.background.ts | 5 +- src/background/runtime.background.ts | 3 +- src/browser/browserApi.ts | 4 + src/popup/app/app.js | 12 ++- src/scripts/analytics.ts | 99 -------------------- src/services/browserPlatformUtils.service.ts | 4 + 8 files changed, 26 insertions(+), 107 deletions(-) delete mode 100644 src/scripts/analytics.ts diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 4236dee87c..3542f931da 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -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, diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 0587fba5c3..2da29845ce 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -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, diff --git a/src/background/main.background.ts b/src/background/main.background.ts index b7b5310c32..a6fb9f0439 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -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(); diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index 627d2fbf5d..a43ea8aca5 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -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'; diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 5e28c736e1..9badd89ed7 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -249,4 +249,8 @@ export class BrowserApi { safariTab: tab, }; } + + static gaFilter() { + return BrowserApi.isSafariApi && safari.application.activeBrowserWindow.activeTab.private; + } } diff --git a/src/popup/app/app.js b/src/popup/app/app.js index 5c1e0191be..ab4d9c5ec6 100644 --- a/src/popup/app/app.js +++ b/src/popup/app/app.js @@ -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'; diff --git a/src/scripts/analytics.ts b/src/scripts/analytics.ts deleted file mode 100644 index df0f153bb7..0000000000 --- a/src/scripts/analytics.ts +++ /dev/null @@ -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('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); - } -} diff --git a/src/services/browserPlatformUtils.service.ts b/src/services/browserPlatformUtils.service.ts index 51de73792b..3f392515e9 100644 --- a/src/services/browserPlatformUtils.service.ts +++ b/src/services/browserPlatformUtils.service.ts @@ -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';