-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84a9f35
commit 8aa1925
Showing
5 changed files
with
50 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {useProfileData} from "@/contexts/profile.context" | ||
import {isDemo} from "@/lib/helpers/utils" | ||
import {usePostHog} from "posthog-js/react" | ||
import {useLayoutEffect} from "react" | ||
|
||
export const usePostHogAg = () => { | ||
const trackingEnabled = process.env.NEXT_PUBLIC_TELEMETRY_TRACKING_ENABLED === "true" | ||
const {user} = useProfileData() | ||
const posthog = usePostHog() | ||
|
||
const _id = isDemo() ? user?.email : user?.id | ||
|
||
const capture: typeof posthog.capture = (...args) => { | ||
if (trackingEnabled && user?.id) { | ||
posthog.capture(...args) | ||
} | ||
} | ||
|
||
const identify: typeof posthog.identify = (id, ...args) => { | ||
if (trackingEnabled && user?.id) { | ||
posthog.identify(_id || id, ...args) | ||
} | ||
} | ||
|
||
useLayoutEffect(() => { | ||
if (!trackingEnabled) posthog.opt_out_capturing() | ||
}, [trackingEnabled]) | ||
|
||
useLayoutEffect(() => { | ||
if (posthog.get_distinct_id() !== _id) identify() | ||
}, [user?.id]) | ||
|
||
return {...posthog, identify, capture} | ||
} |