Skip to content

Commit

Permalink
improve data tracker (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Aug 5, 2023
1 parent 41d8bb1 commit 8cb990d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/components/data-management/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -139,6 +140,8 @@ const App = ({
}
try {
await signIn(email, verificationCode);

await trackEventWithClientID("sign_in")
} finally {
if (signInRef.current) {
signInRef.current!.disabled = false;
Expand Down
4 changes: 2 additions & 2 deletions src/components/refresh-data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 3 additions & 12 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<React.StrictMode>
Expand All @@ -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"))();
14 changes: 14 additions & 0 deletions src/utils/app.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
}
}

0 comments on commit 8cb990d

Please sign in to comment.