-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2357 from Agenta-AI/AGE-1414/improve-posthog-impo…
…rts-to-reduce-initial-bundle-size (frontend)[AGE-1418]: Improve posthog initialization to reduce load on bundle size
- Loading branch information
Showing
11 changed files
with
135 additions
and
77 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 was deleted.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
agenta-web/src/lib/helpers/analytics/AgPosthogProvider.tsx
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,55 @@ | ||
import {useCallback, useEffect, useRef, useState} from "react" | ||
import {useRouter} from "next/router" | ||
import {useAtom} from "jotai" | ||
import {posthogAtom, type PostHogConfig} from "./store/atoms" | ||
import {CustomPosthogProviderType} from "./types" | ||
|
||
const CustomPosthogProvider: CustomPosthogProviderType = ({children, config}) => { | ||
const router = useRouter() | ||
const [loadingPosthog, setLoadingPosthog] = useState(false) | ||
const [posthogClient, setPosthogClient] = useAtom(posthogAtom) | ||
|
||
const initPosthog = useCallback(async () => { | ||
if (!!posthogClient) return | ||
if (loadingPosthog) return | ||
|
||
setLoadingPosthog(true) | ||
|
||
try { | ||
const posthog = (await import("posthog-js")).default | ||
|
||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY!, { | ||
api_host: "https://app.posthog.com", | ||
// Enable debug mode in development | ||
loaded: (posthog) => { | ||
setPosthogClient(posthog) | ||
if (process.env.NODE_ENV === "development") posthog.debug() | ||
}, | ||
capture_pageview: false, | ||
...config, | ||
}) | ||
} finally { | ||
setLoadingPosthog(false) | ||
} | ||
}, [loadingPosthog, config, posthogClient, setPosthogClient]) | ||
|
||
useEffect(() => { | ||
initPosthog() | ||
}, [initPosthog]) | ||
|
||
const handleRouteChange = useCallback(() => { | ||
posthogClient?.capture("$pageview", {$current_url: window.location.href}) | ||
}, [posthogClient]) | ||
|
||
useEffect(() => { | ||
router.events.on("routeChangeComplete", handleRouteChange) | ||
|
||
return () => { | ||
router.events.off("routeChangeComplete", handleRouteChange) | ||
} | ||
}, [handleRouteChange, router.events]) | ||
|
||
return <>{children}</> | ||
} | ||
|
||
export default CustomPosthogProvider |
51 changes: 51 additions & 0 deletions
51
agenta-web/src/lib/helpers/analytics/hooks/usePostHogAg.ts
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,51 @@ | ||
import {isDemo, generateOrRetrieveDistinctId} from "@/lib/helpers/utils" | ||
import {useProfileData} from "@/contexts/profile.context" | ||
import {useAtom} from "jotai" | ||
import {posthogAtom} from "../store/atoms" | ||
import {type PostHog} from "posthog-js" | ||
import useIsomorphicLayoutEffect from "@/hooks/useIsomorphicLayoutEffect" | ||
|
||
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) | ||
|
||
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) | ||
} | ||
} | ||
useIsomorphicLayoutEffect(() => { | ||
if (!posthog) return | ||
|
||
if (!trackingEnabled) { | ||
console.log("POSTHOG: opt_out_capturing") | ||
posthog.opt_out_capturing() | ||
} | ||
}, [posthog, trackingEnabled]) | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
if (!posthog) return | ||
if (posthog.get_distinct_id() !== _id) identify() | ||
}, [posthog, _id]) | ||
|
||
return posthog | ||
? ({ | ||
...posthog, | ||
identify, | ||
capture, | ||
} as ExtendedPostHog) | ||
: null | ||
} |
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,5 @@ | ||
import {atom} from "jotai" | ||
import {type PostHog, type PostHogConfig} from "posthog-js" | ||
|
||
export type {PostHogConfig} | ||
export const posthogAtom = atom<PostHog | null>(null) |
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,7 @@ | ||
import {type PostHogConfig} from "./store/atoms" | ||
|
||
export interface CustomPosthogProviderType | ||
extends React.FC<{ | ||
children: React.ReactNode | ||
config: Partial<PostHogConfig> | ||
}> {} |
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