From e3e52abedeb5507ca71d663faa7ef050b661cf0a Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 27 Aug 2024 12:50:03 +0800 Subject: [PATCH] feat: add biz user info on sentry tracker Signed-off-by: Innei --- src/renderer/src/atoms/user.ts | 2 +- src/renderer/src/configs.ts | 1 + src/renderer/src/initialize/helper.ts | 14 ++++++++++++++ src/renderer/src/initialize/posthog.ts | 4 +--- src/renderer/src/initialize/sentry.ts | 11 ++++++++--- src/renderer/src/providers/user-provider.tsx | 14 ++++++-------- 6 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/renderer/src/initialize/helper.ts diff --git a/src/renderer/src/atoms/user.ts b/src/renderer/src/atoms/user.ts index 958066d149..a5be16a143 100644 --- a/src/renderer/src/atoms/user.ts +++ b/src/renderer/src/atoms/user.ts @@ -2,7 +2,7 @@ import type { User } from "@auth/core/types" import { createAtomHooks } from "@renderer/lib/jotai" import { atom } from "jotai" -export const [, , useWhoami, useSetWhoami, whoami, setWhoami] = createAtomHooks( +export const [, , useWhoami, , whoami, setWhoami] = createAtomHooks( atom>(null), ) diff --git a/src/renderer/src/configs.ts b/src/renderer/src/configs.ts index 4b45449ff8..c5d4f8b205 100644 --- a/src/renderer/src/configs.ts +++ b/src/renderer/src/configs.ts @@ -35,6 +35,7 @@ export const SentryConfig: BrowserOptions = { if (error instanceof FetchError) { return null } + return event }, } diff --git a/src/renderer/src/initialize/helper.ts b/src/renderer/src/initialize/helper.ts new file mode 100644 index 0000000000..43fdf57cf3 --- /dev/null +++ b/src/renderer/src/initialize/helper.ts @@ -0,0 +1,14 @@ +import type { User } from "@auth/core/types" + +export const setIntegrationIdentify = (user: User) => Promise.all([ + import("@sentry/react").then(({ setTag }) => { + setTag("user_id", user.id) + setTag("user_name", user.name) + }), + import("posthog-js").then(({ default: posthog }) => { + posthog.identify(user.id, { + name: user.name, + handle: user.handle, + }) + }), +]) diff --git a/src/renderer/src/initialize/posthog.ts b/src/renderer/src/initialize/posthog.ts index b8cf673ecb..878695ce9c 100644 --- a/src/renderer/src/initialize/posthog.ts +++ b/src/renderer/src/initialize/posthog.ts @@ -5,7 +5,6 @@ import type { CaptureOptions, Properties } from "posthog-js" declare global { interface Window { posthog?: { - identify: InstanceType["identify"] capture: InstanceType["capture"] reset: InstanceType["reset"] } @@ -20,10 +19,9 @@ export const initPostHog = async () => { person_profiles: "identified_only", }) - const { capture, identify, reset } = posthog + const { capture, reset } = posthog window.posthog = { - identify, reset, capture( event_name: string, diff --git a/src/renderer/src/initialize/sentry.ts b/src/renderer/src/initialize/sentry.ts index 402cd285c6..dda4b2db27 100644 --- a/src/renderer/src/initialize/sentry.ts +++ b/src/renderer/src/initialize/sentry.ts @@ -1,5 +1,6 @@ import { env } from "@env" import { version } from "@pkg" +import { whoami } from "@renderer/atoms/user" import { channel } from "@renderer/constants" import { useEffect } from "react" import { @@ -43,7 +44,11 @@ export const initSentry = async () => { ...SentryConfig, }) - Sentry.setTags({ - appVersion: version, - }) + const user = whoami() + if (user) { + Sentry.setTag("user_id", user.id) + Sentry.setTag("user_name", user.name) + } + + Sentry.setTag("appVersion", version) } diff --git a/src/renderer/src/providers/user-provider.tsx b/src/renderer/src/providers/user-provider.tsx index e9e17a9b7d..cbe2445bd8 100644 --- a/src/renderer/src/providers/user-provider.tsx +++ b/src/renderer/src/providers/user-provider.tsx @@ -1,4 +1,5 @@ -import { useSetWhoami } from "@renderer/atoms/user" +import { setWhoami } from "@renderer/atoms/user" +import { setIntegrationIdentify } from "@renderer/initialize/helper" import { tipcClient } from "@renderer/lib/client" import { useSession } from "@renderer/queries/auth" import { CleanerService } from "@renderer/services/cleaner" @@ -6,21 +7,18 @@ import { useEffect } from "react" export const UserProvider = () => { const { session } = useSession() - const setUser = useSetWhoami() + useEffect(() => { if (!session?.user) return - setUser(session.user) + setWhoami(session.user) - window.posthog?.identify(session.user.id, { - name: session.user.name, - handle: session.user.handle, - }) + setIntegrationIdentify(session.user) tipcClient?.trackerIdentify({ user: session.user, }) CleanerService.cleanRemainingData(session.user.id) - }, [session?.user, setUser]) + }, [session?.user]) return null }