Skip to content

Commit

Permalink
improve for null returns
Browse files Browse the repository at this point in the history
  • Loading branch information
ardaerzin committed Dec 9, 2024
1 parent 0f874b5 commit 9bbb90c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const ParametersView: React.FC<Props> = ({
onStateChange(false)
res(true)
})
posthog.capture("variant_saved", {variant_id: variant.variantId})
posthog?.capture?.("variant_saved", {variant_id: variant.variantId})
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const PublishVariantModal: React.FC<Props> = ({
closeModal()
await loadEnvironments()
message.success(`Published ${variant.variantName} to ${envName}`)
posthog.capture("app_deployed", {app_id: appId, environment: envName})
posthog?.capture?.("app_deployed", {app_id: appId, environment: envName})
})
}

Expand Down
2 changes: 1 addition & 1 deletion agenta-web/src/components/pages/app-management/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const AppManagement: React.FC = () => {
setFetchingTemplate(false)
if (status === "success") {
mutate()
posthog.capture("app_deployment", {
posthog?.capture?.("app_deployment", {
properties: {
app_id: appId,
environment: "UI",
Expand Down
2 changes: 1 addition & 1 deletion agenta-web/src/contexts/profile.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ProfileContextProvider: React.FC<PropsWithChildren> = ({children}) => {
setLoading(true)
fetchProfile()
.then((profile) => {
posthog.identify()
posthog?.identify?.()
setUser(profile.data, onSuccess)
})
.catch((error) => {
Expand Down
16 changes: 13 additions & 3 deletions agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {useAtom} from "jotai"
import {posthogAtom} from "../store/atoms"
import {type PostHog} from "posthog-js"

export const usePostHogAg = () => {
interface ExtendedPostHog extends PostHog {
identify: PostHog["identify"]
capture: PostHog["capture"]
}

export const usePostHogAg = (): ExtendedPostHog | null => {
const trackingEnabled = process.env.NEXT_PUBLIC_TELEMETRY_TRACKING_ENABLED === "true"
const {user} = useProfileData()
const [posthog] = useAtom(posthogAtom)
Expand All @@ -19,7 +24,6 @@ export const usePostHogAg = () => {
}
const identify: PostHog["identify"] = (id, ...args) => {
if (trackingEnabled && user?.id) {
console.log("POSTHOG: identify")
posthog?.identify?.(_id !== undefined ? _id : id, ...args)
}
}
Expand All @@ -37,5 +41,11 @@ export const usePostHogAg = () => {
if (posthog.get_distinct_id() !== _id) identify()
}, [posthog, _id])

return {...posthog, identify, capture}
return posthog
? ({
...posthog,
identify,
capture,
} as ExtendedPostHog)
: null
}

0 comments on commit 9bbb90c

Please sign in to comment.