diff --git a/agenta-web/src/components/Playground/Views/ParametersView.tsx b/agenta-web/src/components/Playground/Views/ParametersView.tsx index a9ba316b5f..72da9eda90 100644 --- a/agenta-web/src/components/Playground/Views/ParametersView.tsx +++ b/agenta-web/src/components/Playground/Views/ParametersView.tsx @@ -128,7 +128,7 @@ const ParametersView: React.FC = ({ onStateChange(false) res(true) }) - posthog.capture("variant_saved", {variant_id: variant.variantId}) + posthog?.capture?.("variant_saved", {variant_id: variant.variantId}) }) } diff --git a/agenta-web/src/components/Playground/Views/PublishVariantModal.tsx b/agenta-web/src/components/Playground/Views/PublishVariantModal.tsx index dede33f89c..ab4221e04c 100644 --- a/agenta-web/src/components/Playground/Views/PublishVariantModal.tsx +++ b/agenta-web/src/components/Playground/Views/PublishVariantModal.tsx @@ -58,7 +58,7 @@ const PublishVariantModal: React.FC = ({ 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}) }) } diff --git a/agenta-web/src/components/pages/app-management/index.tsx b/agenta-web/src/components/pages/app-management/index.tsx index 1e557487a2..6a69ef89a7 100644 --- a/agenta-web/src/components/pages/app-management/index.tsx +++ b/agenta-web/src/components/pages/app-management/index.tsx @@ -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", diff --git a/agenta-web/src/contexts/profile.context.tsx b/agenta-web/src/contexts/profile.context.tsx index bcb6fd8bb7..815b616fcf 100644 --- a/agenta-web/src/contexts/profile.context.tsx +++ b/agenta-web/src/contexts/profile.context.tsx @@ -38,7 +38,7 @@ const ProfileContextProvider: React.FC = ({children}) => { setLoading(true) fetchProfile() .then((profile) => { - posthog.identify() + posthog?.identify?.() setUser(profile.data, onSuccess) }) .catch((error) => { diff --git a/agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts b/agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts index d1ea894de8..a7ef761edb 100644 --- a/agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts +++ b/agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts @@ -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) @@ -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) } } @@ -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 }