From f6219f956cf1976ee2231f6ffd5c61a86d88169d Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 10 Mar 2022 17:36:28 +0530 Subject: [PATCH] incr: capture telemetryobject on unload --- src/initialization.js | 2 ++ src/main.js | 11 +++++++++++ src/telemetry/telemetry.ts | 18 ++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/initialization.js b/src/initialization.js index d8145931..f897c82e 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -96,7 +96,9 @@ export async function connectToLocalDatabase(filePath) { // second init with currency, normal usage await callInitializeMoneyMaker(); + telemetry.start(); + await telemetry.setCount(); return { connectionSuccess: true, reason: '' }; } diff --git a/src/main.js b/src/main.js index 90d5ea8a..447b6b10 100644 --- a/src/main.js +++ b/src/main.js @@ -8,6 +8,7 @@ import config from './config'; import { getErrorHandled, handleError } from './errorHandling'; import { IPC_CHANNELS, IPC_MESSAGES } from './messages'; import router from './router'; +import telemetry from './telemetry/telemetry'; import { outsideClickDirective } from './ui'; import { setLanguageMap, showToast, stringifyCircular } from './utils'; (async () => { @@ -135,4 +136,14 @@ function registerIpcRendererListeners() { error.name = 'Updation Error'; handleError(true, error); }); + + document.addEventListener('visibilitychange', function () { + if (document.visibilityState !== 'hidden') { + return; + } + + const telemetryData = telemetry.stop(); + console.log(telemetryData); + // navigator.sendBeacon('', telemetryData) + }); } diff --git a/src/telemetry/telemetry.ts b/src/telemetry/telemetry.ts index da2ca493..718350e8 100644 --- a/src/telemetry/telemetry.ts +++ b/src/telemetry/telemetry.ts @@ -9,7 +9,7 @@ class TelemetryManager { #telemetryObject: Partial = {}; start() { - this.#telemetryObject.locale ||= getLocale(); + this.#telemetryObject.locale = getLocale(); this.#telemetryObject.deviceId ||= getDeviceId(); this.#telemetryObject.instanceId ||= getInstanceId(); this.#telemetryObject.openTime ||= new Date().valueOf(); @@ -49,18 +49,28 @@ class TelemetryManager { this.#telemetryObject.errors[name] += 1; } - async stop() { + async setCount() { + this.#telemetryObject.counts = this.getCanLog() ? await getCounts() : {}; + } + + stop() { // Will set ids if not set. this.start(); //@ts-ignore this.#telemetryObject.version = frappe.store.appVersion ?? ''; - this.#telemetryObject.counts = this.getCanLog() ? await getCounts() : {}; this.#telemetryObject.closeTime = new Date().valueOf(); + const telemetryObject = this.#telemetryObject; + + this.#started = false; + this.#telemetryObject = {}; + if (config.get(ConfigKeys.Telemetry) === TelemetrySetting.dontLogAnything) { - return; + return ''; } + + return JSON.stringify(telemetryObject); } get telemetryObject(): Readonly> {