Skip to content

Commit

Permalink
enhance(frontend): allow posthog provider accept config options
Browse files Browse the repository at this point in the history
  • Loading branch information
ardaerzin committed Dec 10, 2024
1 parent a81cdff commit bfaf283
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
9 changes: 5 additions & 4 deletions agenta-web/src/lib/helpers/analytics/AgPosthogProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {useCallback, useEffect, useRef} from "react"
import {useRouter} from "next/router"
import {useAtom} from "jotai"
import {posthogAtom} from "./store/atoms"
import {posthogAtom, type PostHogConfig} from "./store/atoms"
import {CustomPosthogProviderType} from "./types"

const CustomPosthogProvider = ({children}: {children: React.ReactNode}) => {
const CustomPosthogProvider: CustomPosthogProviderType = ({children, config}) => {
const router = useRouter()
const loadingPosthog = useRef(false)
const [posthogClient, setPosthogClient] = useAtom(posthogAtom)
Expand All @@ -24,9 +25,9 @@ const CustomPosthogProvider = ({children}: {children: React.ReactNode}) => {
if (process.env.NODE_ENV === "development") posthog.debug()
},
capture_pageview: false,
persistence: "localStorage+cookie",
...config,
})
}, [posthogClient, setPosthogClient])
}, [config, posthogClient, setPosthogClient])

useEffect(() => {
initPosthog()
Expand Down
3 changes: 2 additions & 1 deletion agenta-web/src/lib/helpers/analytics/store/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {atom} from "jotai"
import {type PostHog} from "posthog-js"
import {type PostHog, type PostHogConfig} from "posthog-js"

export type {PostHogConfig}
export const posthogAtom = atom<PostHog | null>(null)
7 changes: 7 additions & 0 deletions agenta-web/src/lib/helpers/analytics/types.d.ts
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>
}> {}
6 changes: 5 additions & 1 deletion agenta-web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default function App({Component, pageProps}: AppProps) {
<link rel="shortcut icon" href="/assets/favicon.ico" />
</Head>
<main className={`${inter.variable} font-sans`}>
<CustomPosthogProvider>
<CustomPosthogProvider
config={{
persistence: "localStorage+cookie",
}}
>
<ThemeContextProvider>
<ProfileContextProvider>
<ProjectContextProvider>
Expand Down

0 comments on commit bfaf283

Please sign in to comment.