From 9c73d2523f704fd42447d400764e9c8a033e87cb Mon Sep 17 00:00:00 2001 From: jliddev Date: Thu, 22 Oct 2020 15:02:05 -0500 Subject: [PATCH] Use firebase analytics --- wowup-electron/package.json | 1 + wowup-electron/src/app/app.component.ts | 8 +- .../addon-update-button.component.ts | 26 ++-- .../user-action-tracker.directive.ts | 1 - .../get-addons/get-addons.component.html | 6 +- .../src/app/services/addons/addon.service.ts | 24 ++-- .../services/analytics/analytics.service.ts | 118 +++++++++++------- .../src/environments/environment.dev.ts | 1 + .../src/environments/environment.prod.ts | 10 ++ .../src/environments/environment.ts | 10 ++ .../src/environments/environment.web.ts | 1 + 11 files changed, 136 insertions(+), 70 deletions(-) diff --git a/wowup-electron/package.json b/wowup-electron/package.json index 58a07648..c5fd3848 100644 --- a/wowup-electron/package.json +++ b/wowup-electron/package.json @@ -118,6 +118,7 @@ "electron-log": "4.2.4", "electron-store": "6.0.1", "electron-updater": "4.3.5", + "firebase": "7.24.0", "fs-extra": "9.0.1", "globrex": "0.1.2", "lodash": "4.17.20", diff --git a/wowup-electron/src/app/app.component.ts b/wowup-electron/src/app/app.component.ts index 7e79036d..657f049d 100644 --- a/wowup-electron/src/app/app.component.ts +++ b/wowup-electron/src/app/app.component.ts @@ -52,10 +52,10 @@ export class AppComponent implements AfterViewInit { } this.onAutoUpdateInterval(); - // this._autoUpdateInterval = window.setInterval( - // this.onAutoUpdateInterval, - // AUTO_UPDATE_PERIOD_MS - // ); + this._autoUpdateInterval = window.setInterval( + this.onAutoUpdateInterval, + AUTO_UPDATE_PERIOD_MS + ); } openDialog(): void { diff --git a/wowup-electron/src/app/components/addon-update-button/addon-update-button.component.ts b/wowup-electron/src/app/components/addon-update-button/addon-update-button.component.ts index 19a824b5..17707399 100644 --- a/wowup-electron/src/app/components/addon-update-button/addon-update-button.component.ts +++ b/wowup-electron/src/app/components/addon-update-button/addon-update-button.component.ts @@ -1,9 +1,11 @@ import { Component, Input, OnDestroy, OnInit } from "@angular/core"; import { TranslateService } from "@ngx-translate/core"; import { AddonViewModel } from "app/business-objects/my-addon-list-item"; +import { WowClientType } from "app/models/warcraft/wow-client-type"; import { AddonInstallState } from "app/models/wowup/addon-install-state"; import { AddonService } from "app/services/addons/addon.service"; -import { filter } from "rxjs/operators"; +import { AnalyticsService } from "app/services/analytics/analytics.service"; +import { getEnumName } from "app/utils/enum.utils"; @Component({ selector: "app-addon-update-button", @@ -15,14 +17,18 @@ export class AddonUpdateButtonComponent implements OnInit, OnDestroy { constructor( private _addonService: AddonService, + private _analyticsService: AnalyticsService, private _translateService: TranslateService ) {} - ngOnInit(): void { - - } - - ngOnDestroy(): void { + ngOnInit(): void {} + + ngOnDestroy(): void {} + + public get actionLabel() { + return `${getEnumName(WowClientType, this.listItem?.addon?.clientType)}|${ + this.listItem?.addon.providerName + }|${this.listItem?.addon.externalId}|${this.listItem?.addon.name}`; } public get installProgress() { @@ -39,7 +45,7 @@ export class AddonUpdateButtonComponent implements OnInit, OnDestroy { public get isButtonDisabled() { return ( this.listItem?.isUpToDate || - this.listItem?.installState < AddonInstallState.Unknown + this.listItem?.installState < AddonInstallState.Unknown ); } @@ -52,6 +58,12 @@ export class AddonUpdateButtonComponent implements OnInit, OnDestroy { } public onInstallUpdateClick() { + this._analyticsService.trackUserAction( + "addons", + "update_addon", + this.actionLabel + ); + this._addonService.installAddon( this.listItem.addon.id, this.onInstallUpdate diff --git a/wowup-electron/src/app/directives/user-action-tracker.directive.ts b/wowup-electron/src/app/directives/user-action-tracker.directive.ts index 4100daaf..c1d13c33 100644 --- a/wowup-electron/src/app/directives/user-action-tracker.directive.ts +++ b/wowup-electron/src/app/directives/user-action-tracker.directive.ts @@ -12,7 +12,6 @@ export class UserActionTrackerDirective { @Input() label: string; @HostListener('click', ['$event']) onClick($event) { - this._analyticsService.trackUserAction(this.category, this.action, this.label); } constructor(private _analyticsService: AnalyticsService) { } diff --git a/wowup-electron/src/app/pages/get-addons/get-addons.component.html b/wowup-electron/src/app/pages/get-addons/get-addons.component.html index ed06b92d..0655491b 100644 --- a/wowup-electron/src/app/pages/get-addons/get-addons.component.html +++ b/wowup-electron/src/app/pages/get-addons/get-addons.component.html @@ -64,8 +64,10 @@ {{'PAGES.GET_ADDONS.TABLE.RELEASED_AT_COLUMN_HEADER' | translate}} - - {{element | getAddonListItemFileProp:'releaseDate':defaultAddonChannel | date:'short'}} + +
+ {{element | getAddonListItemFileProp:'releaseDate':defaultAddonChannel | date:'short'}} +
diff --git a/wowup-electron/src/app/services/addons/addon.service.ts b/wowup-electron/src/app/services/addons/addon.service.ts index bf5b9c1d..92b709c9 100644 --- a/wowup-electron/src/app/services/addons/addon.service.ts +++ b/wowup-electron/src/app/services/addons/addon.service.ts @@ -24,6 +24,7 @@ import { TocService } from "../toc/toc.service"; import { AddonUpdateEvent } from "app/models/wowup/addon-update-event"; import { AddonProviderFactory } from "./addon.provider.factory"; import { AnalyticsService } from "../analytics/analytics.service"; +import { getEnumName } from "app/utils/enum.utils"; interface InstallQueueItem { addonId: string; @@ -84,8 +85,8 @@ export class AddonService { var searchResults = await Promise.all(searchTasks); await this._analyticsService.trackUserAction( - "Addons", - "Search", + "addons", + "search", `${clientType}|${query}` ); @@ -268,10 +269,13 @@ export class AddonService { this._addonStorage.set(addon.id, addon); - await this._analyticsService.trackUserAction( - "Addons", - "InstallById", - `${addon.clientType}|${addon.name}` + const actionLabel = `${getEnumName(WowClientType, addon.clientType)}|${ + addon.providerName + }|${addon.externalId}|${addon.name}`; + this._analyticsService.trackUserAction( + "addons", + "install_by_id", + actionLabel ); queueItem.completion.resolve(); @@ -557,10 +561,8 @@ export class AddonService { if ( !result || !latestFile || - ( - latestFile.version === addon.latestVersion && - latestFile.releaseDate === addon.releasedAt - ) + (latestFile.version === addon.latestVersion && + latestFile.releaseDate === addon.releasedAt) ) { continue; } @@ -755,7 +757,7 @@ export class AddonService { autoUpdateEnabled: this._wowUpService.getDefaultAutoUpdate(clientType), releasedAt: latestFile.releaseDate, summary: searchResult.summary, - screenshotUrls: searchResult.screenshotUrls + screenshotUrls: searchResult.screenshotUrls, }; } } diff --git a/wowup-electron/src/app/services/analytics/analytics.service.ts b/wowup-electron/src/app/services/analytics/analytics.service.ts index 29747f23..d27086e5 100644 --- a/wowup-electron/src/app/services/analytics/analytics.service.ts +++ b/wowup-electron/src/app/services/analytics/analytics.service.ts @@ -1,10 +1,12 @@ -import { ErrorHandler, Injectable } from "@angular/core"; +import { Injectable } from "@angular/core"; import { v4 as uuidv4 } from "uuid"; import { PreferenceStorageService } from "../storage/preference-storage.service"; import { AppConfig } from "environments/environment"; import { HttpClient, HttpParams } from "@angular/common/http"; import { ElectronService } from "../electron/electron.service"; import { BehaviorSubject } from "rxjs"; +import * as firebase from "firebase/app"; +import "firebase/analytics"; @Injectable({ providedIn: "root", @@ -16,6 +18,9 @@ export class AnalyticsService { private readonly _appVersion: string; private readonly _telemetryEnabledSrc = new BehaviorSubject(false); + private _firebaseApp?: firebase.app.App; + private _firebaseAnalytics?: firebase.analytics.Analytics; + public readonly telemetryPromptUsedKey = "telemetry_prompt_sent"; public readonly telemetryEnabledKey = "telemetry_enabled"; public readonly telemetryEnabled$ = this._telemetryEnabledSrc.asObservable(); @@ -24,6 +29,22 @@ export class AnalyticsService { return this._installId; } + private startFirebase() { + if (this._firebaseApp) { + return; + } + + console.log("Firebase setup"); + + this._firebaseApp = firebase.initializeApp(AppConfig.firebaseConfig); + this._firebaseAnalytics = firebase.analytics(this._firebaseApp); + this._firebaseAnalytics.setUserId(this.installId); + } + + private setFirebaseAnalyticsEnabled(enabled: boolean) { + this._firebaseAnalytics?.setAnalyticsCollectionEnabled(enabled); + } + public get shouldPromptTelemetry() { return ( this._preferenceStorageService.get(this.telemetryEnabledKey) === undefined @@ -34,7 +55,14 @@ export class AnalyticsService { const preference = this._preferenceStorageService.findByKey( this.telemetryEnabledKey ); - return preference === true.toString(); + const isEnabled = preference === true.toString(); + + this.setFirebaseAnalyticsEnabled(isEnabled); + if (isEnabled) { + this.startFirebase(); + } + + return isEnabled; } public set telemetryEnabled(value: boolean) { @@ -44,70 +72,70 @@ export class AnalyticsService { constructor( private _electronService: ElectronService, - private _httpClient: HttpClient, private _preferenceStorageService: PreferenceStorageService ) { - this._appVersion = _electronService.remote.app.getVersion(); + this._appVersion = this._electronService.remote.app.getVersion(); this._installId = this.loadInstallId(); this._telemetryEnabledSrc.next(this.telemetryEnabled); - console.log("installId", this._installId); } - public async trackStartup() { - await this.track((params) => { - params.set("t", "pageview"); - params.set("dp", "app/startup"); - }); + public trackStartup() { + this._firebaseAnalytics.logEvent("app_startup"); } - public async trackUserAction( + public trackUserAction( category: string, action: string, label: string = null ) { - await this.track((params) => { - params.set("t", "event"); - params.set("ec", category); - params.set("ea", action); - params.set("el", label); + this.trackEvent(category, { + [action]: label, }); } - private async track(action: (params: HttpParams) => void = undefined) { + private trackEvent(eventName: string, params: { [key: string]: any }) { if (!this.telemetryEnabled) { return; } - var url = `${this.analyticsUrl}/collect`; - - try { - let params = new URLSearchParams(); - params.set("v", "1"); - params.set("tid", AppConfig.googleAnalyticsId); - params.set("cid", this._installId); - params.set("ua", window.navigator.userAgent); - params.set("an", "WowUp Client"); - params.set("av", this._appVersion); - - action?.call(this, params); - - const fullUrl = `${url}?${params}`; - - const response = await this._httpClient - .post( - fullUrl, - {}, - { - responseType: "text", - } - ) - .toPromise(); - } catch (e) { - // eat - console.error(e); - } + this._firebaseAnalytics.logEvent(eventName, params); } + // private async track(action: (params: HttpParams) => void = undefined) { + // if (!this.telemetryEnabled) { + // return; + // } + + // var url = `${this.analyticsUrl}/collect`; + + // try { + // let params = new URLSearchParams(); + // params.set("v", "1"); + // params.set("tid", AppConfig.googleAnalyticsId); + // params.set("cid", this._installId); + // params.set("ua", window.navigator.userAgent); + // params.set("an", "WowUp Client"); + // params.set("av", this._appVersion); + + // action?.call(this, params); + + // const fullUrl = `${url}?${params}`; + + // const response = await this._httpClient + // .post( + // fullUrl, + // {}, + // { + // responseType: "text", + // } + // ) + // .toPromise(); + // } catch (e) { + // // eat + // console.error(e); + // } + // } + private loadInstallId() { let installId = this._preferenceStorageService.findByKey( this.installIdPreferenceKey diff --git a/wowup-electron/src/environments/environment.dev.ts b/wowup-electron/src/environments/environment.dev.ts index b0d3deaa..2c891dee 100644 --- a/wowup-electron/src/environments/environment.dev.ts +++ b/wowup-electron/src/environments/environment.dev.ts @@ -10,4 +10,5 @@ export const AppConfig = { wowUpHubUrl: "https://hub.dev.wowup.io", rollbarAccessKey: "d01c11314a064572b11acee18d880650", googleAnalyticsId: "UA-92563227-4", + }; diff --git a/wowup-electron/src/environments/environment.prod.ts b/wowup-electron/src/environments/environment.prod.ts index cd8a50b7..99a21680 100644 --- a/wowup-electron/src/environments/environment.prod.ts +++ b/wowup-electron/src/environments/environment.prod.ts @@ -5,4 +5,14 @@ export const AppConfig = { wowUpHubUrl: "https://hub.wowup.io", rollbarAccessKey: "d01c11314a064572b11acee18d880650", googleAnalyticsId: "UA-92563227-4", + firebaseConfig: { + apiKey: "AIzaSyBvZ1_ldinsxeh-iRI53REm8j1CPcQuIBw", + authDomain: "wowup-893c6.firebaseapp.com", + databaseURL: "https://wowup-893c6.firebaseio.com", + projectId: "wowup-893c6", + storageBucket: "wowup-893c6.appspot.com", + messagingSenderId: "914166112595", + appId: "1:914166112595:web:cdb02d231e5f9802b541a6", + measurementId: "G-V646RWJ5NK", + }, }; diff --git a/wowup-electron/src/environments/environment.ts b/wowup-electron/src/environments/environment.ts index 6e4ad5a7..1c91a6a2 100644 --- a/wowup-electron/src/environments/environment.ts +++ b/wowup-electron/src/environments/environment.ts @@ -5,4 +5,14 @@ export const AppConfig = { wowUpHubUrl: "https://hub.dev.wowup.io", rollbarAccessKey: "d01c11314a064572b11acee18d880650", googleAnalyticsId: "UA-92563227-4", + firebaseConfig: { + apiKey: "AIzaSyBvZ1_ldinsxeh-iRI53REm8j1CPcQuIBw", + authDomain: "wowup-893c6.firebaseapp.com", + databaseURL: "https://wowup-893c6.firebaseio.com", + projectId: "wowup-893c6", + storageBucket: "wowup-893c6.appspot.com", + messagingSenderId: "914166112595", + appId: "1:914166112595:web:cdb02d231e5f9802b541a6", + measurementId: "G-V646RWJ5NK", + }, }; diff --git a/wowup-electron/src/environments/environment.web.ts b/wowup-electron/src/environments/environment.web.ts index b0d3deaa..2c891dee 100644 --- a/wowup-electron/src/environments/environment.web.ts +++ b/wowup-electron/src/environments/environment.web.ts @@ -10,4 +10,5 @@ export const AppConfig = { wowUpHubUrl: "https://hub.dev.wowup.io", rollbarAccessKey: "d01c11314a064572b11acee18d880650", googleAnalyticsId: "UA-92563227-4", + };