Skip to content

Commit

Permalink
feat: add biz user info on sentry tracker
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 27, 2024
1 parent cbb8649 commit e3e52ab
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/renderer/src/atoms/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Nullable<User>>(null),
)

Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const SentryConfig: BrowserOptions = {
if (error instanceof FetchError) {
return null
}

return event
},
}
14 changes: 14 additions & 0 deletions src/renderer/src/initialize/helper.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}),
])
4 changes: 1 addition & 3 deletions src/renderer/src/initialize/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { CaptureOptions, Properties } from "posthog-js"
declare global {
interface Window {
posthog?: {
identify: InstanceType<typeof import("posthog-js").PostHog>["identify"]
capture: InstanceType<typeof import("posthog-js").PostHog>["capture"]
reset: InstanceType<typeof import("posthog-js").PostHog>["reset"]
}
Expand All @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions src/renderer/src/initialize/sentry.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
14 changes: 6 additions & 8 deletions src/renderer/src/providers/user-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
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"
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
}

0 comments on commit e3e52ab

Please sign in to comment.