diff --git a/agenta-web/src/hooks/usePostHogAg.ts b/agenta-web/src/hooks/usePostHogAg.ts index b70bc9ba14..b19f2d9455 100644 --- a/agenta-web/src/hooks/usePostHogAg.ts +++ b/agenta-web/src/hooks/usePostHogAg.ts @@ -1,14 +1,14 @@ -import {useProfileData} from "@/contexts/profile.context" -import {isDemo} from "@/lib/helpers/utils" -import {usePostHog} from "posthog-js/react" import {useLayoutEffect} from "react" +import {isDemo, generateOrRetrieveDistinctId} from "@/lib/helpers/utils" +import {usePostHog} from "posthog-js/react" +import {useProfileData} from "@/contexts/profile.context" export const usePostHogAg = () => { const trackingEnabled = process.env.NEXT_PUBLIC_TELEMETRY_TRACKING_ENABLED === "true" const {user} = useProfileData() const posthog = usePostHog() - const _id: string | null = isDemo() ? user?.email : null! + const _id: string = isDemo() ? user?.email : generateOrRetrieveDistinctId() const capture: typeof posthog.capture = (...args) => { if (trackingEnabled && user?.id) { diff --git a/agenta-web/src/lib/helpers/utils.ts b/agenta-web/src/lib/helpers/utils.ts index 7b02123a95..69739131ae 100644 --- a/agenta-web/src/lib/helpers/utils.ts +++ b/agenta-web/src/lib/helpers/utils.ts @@ -1,3 +1,4 @@ +import {v4 as uuidv4} from "uuid" import dynamic from "next/dynamic" import {EvaluationType} from "../enums" import {GenericObject} from "../Types" @@ -313,3 +314,12 @@ export const shortPoll = async ( await delay(delayMs) } } + +export const generateOrRetrieveDistinctId = (): string => { + let distinctId = localStorage.getItem("posthog_distinct_id") + if (!distinctId) { + distinctId = uuidv4() + localStorage.setItem("posthog_distinct_id", distinctId) + } + return distinctId +} \ No newline at end of file