Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): Add more checks before initializing posthog #58

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 36 additions & 18 deletions frontend/src/providers/posthog.tsx
Expand Up @@ -3,25 +3,43 @@
import posthog from "posthog-js"
import { PostHogProvider } from "posthog-js/react"

if (typeof window !== "undefined") {
const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY || "" // Ensure that the variable is defined
posthog.init(posthogKey, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_INGEST_HOST,
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
persistence: "memory", // We don't use cookies for analytics!
capture_pageview: false,
// Disable session recording by default
disable_session_recording:
process.env.NEXT_PUBLIC_DISABLE_SESSION_RECORDING === "true",
session_recording: {
// If even session recording is enabled,
// we mask all inputs and text for maximum privacy
maskAllInputs: true,
maskTextSelector: "*",
},
})
export const initPostHog = () => {
// @ts-ignore
console.log("Initializing PostHog 👋")
try {
if (typeof window !== "undefined") {
// @ts-ignore
const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY
if (
process.env.NODE_ENV === "production" &&
process.env.ENABLE_TELEMETRY === "true" &&
posthogKey
) {
posthog.init(posthogKey, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_INGEST_HOST,
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
persistence: "memory", // We don't use cookies for analytics!
capture_pageview: false,
// Disable session recording by default
disable_session_recording:
process.env.NEXT_PUBLIC_DISABLE_SESSION_RECORDING === "true",
session_recording: {
// If even session recording is enabled,
// we mask all inputs and text for maximum privacy
maskAllInputs: true,
maskTextSelector: "*",
},
})
}
}
return posthog
} catch (e) {
console.log("posthog err", e)
}
return undefined
}

export function PHProvider({ children }: { children: React.ReactNode }) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
const ph = initPostHog()
return <PostHogProvider client={ph}>{children}</PostHogProvider>
}