From 8cb990d63b69a0d2e7280235ca117d58fbe182ff Mon Sep 17 00:00:00 2001 From: domchan <31119455+domechn@users.noreply.github.com> Date: Sun, 6 Aug 2023 03:26:35 +0800 Subject: [PATCH] improve data tracker (#114) --- src/components/data-management/index.tsx | 3 +++ src/components/refresh-data/index.tsx | 4 ++-- src/main.tsx | 15 +++------------ src/utils/app.ts | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/components/data-management/index.tsx b/src/components/data-management/index.tsx index c963281..06e1cc3 100644 --- a/src/components/data-management/index.tsx +++ b/src/components/data-management/index.tsx @@ -21,6 +21,7 @@ import { } from "../../middlelayers/cloudsync"; import { LoadingContext } from "../../App"; import { timestampToDate } from "../../utils/date"; +import { trackEventWithClientID } from '../../utils/app' const App = ({ onDataImported, @@ -139,6 +140,8 @@ const App = ({ } try { await signIn(email, verificationCode); + + await trackEventWithClientID("sign_in") } finally { if (signInRef.current) { signInRef.current!.disabled = false; diff --git a/src/components/refresh-data/index.tsx b/src/components/refresh-data/index.tsx index fb141e7..747adb1 100644 --- a/src/components/refresh-data/index.tsx +++ b/src/components/refresh-data/index.tsx @@ -5,7 +5,7 @@ import { refreshAllData } from "../../middlelayers/charts"; import { toast } from "react-hot-toast"; import { LoadingContext } from "../../App"; import { updateAllCurrencyRates } from "../../middlelayers/currency"; -import { trackEvent } from '@aptabase/tauri' +import { trackEventWithClientID } from '../../utils/app' const retries = 3; const retryInterval = 3000; // 3s @@ -51,7 +51,7 @@ const App = ({ }) .finally(() => { setLoading(false); - trackEvent("data_refreshed") + trackEventWithClientID("data_refreshed") if (refreshError) { toast.error(refreshError.message || (refreshError as any)); } else { diff --git a/src/main.tsx b/src/main.tsx index 4381324..dc42668 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,8 +2,7 @@ import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; import "./style.css"; -import { getClientID, getVersion } from "./utils/app"; -import { trackEvent } from "@aptabase/tauri"; +import { trackEventWithClientID } from "./utils/app"; ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( @@ -27,13 +26,5 @@ function disableContextmenu() { disableContextmenu(); -// ga4 -(async () => { - try { - const cid = await getClientID(); - trackEvent("app_started", { clientId: cid || "unknown" }); - } catch (e) { - trackEvent("app_started"); - throw e; - } -})(); +// track event +(async () => trackEventWithClientID("app_started"))(); diff --git a/src/utils/app.ts b/src/utils/app.ts index 83e702c..fcab9f8 100644 --- a/src/utils/app.ts +++ b/src/utils/app.ts @@ -1,5 +1,6 @@ import * as api from '@tauri-apps/api' import { getClientIDConfiguration } from '../middlelayers/configuration' +import { trackEvent } from '@aptabase/tauri' export async function getVersion() { return api.app.getVersion() @@ -8,3 +9,16 @@ export async function getVersion() { export async function getClientID() { return getClientIDConfiguration() } + + +export async function trackEventWithClientID(event: string, props?: { [k: string]: string | number }) { + const cid = await getClientID() + try { + await trackEvent(event, { + clientID: cid || "unknown", + ...(props ?? {}) + }) + } catch (e) { + console.error("track event failed", e) + } +} \ No newline at end of file