Skip to content

Commit

Permalink
fix(frontend): returned posthog value
Browse files Browse the repository at this point in the history
  • Loading branch information
ardaerzin committed Dec 19, 2024
1 parent 835bc3e commit c6ad565
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useAtom} from "jotai"
import {posthogAtom} from "../store/atoms"
import {type PostHog} from "posthog-js"
import useIsomorphicLayoutEffect from "@/hooks/useIsomorphicLayoutEffect"
import {useCallback} from "react"

interface ExtendedPostHog extends PostHog {
identify: PostHog["identify"]
Expand All @@ -16,22 +17,27 @@ export const usePostHogAg = (): ExtendedPostHog | null => {
const [posthog] = useAtom(posthogAtom)

const _id: string | undefined = isDemo() ? user?.email : generateOrRetrieveDistinctId()
const capture: PostHog["capture"] = (...args) => {
if (trackingEnabled && user?.id) {
return posthog?.capture?.(...args)
}
return undefined
}
const identify: PostHog["identify"] = (id, ...args) => {
if (trackingEnabled && user?.id) {
posthog?.identify?.(_id !== undefined ? _id : id, ...args)
}
}
const capture: PostHog["capture"] = useCallback(
(...args) => {
if (trackingEnabled && user?.id) {
return posthog?.capture?.(...args)
}
return undefined
},
[posthog, trackingEnabled, user?.id],
)
const identify: PostHog["identify"] = useCallback(
(id, ...args) => {
if (trackingEnabled && user?.id) {
posthog?.identify?.(_id !== undefined ? _id : id, ...args)
}
},
[_id, posthog, trackingEnabled, user?.id],
)
useIsomorphicLayoutEffect(() => {
if (!posthog) return

if (!trackingEnabled) {
console.log("POSTHOG: opt_out_capturing")
posthog.opt_out_capturing()
}
}, [posthog, trackingEnabled])
Expand All @@ -41,11 +47,5 @@ export const usePostHogAg = (): ExtendedPostHog | null => {
if (posthog.get_distinct_id() !== _id) identify()
}, [posthog, _id])

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

0 comments on commit c6ad565

Please sign in to comment.