fix: updated per @paabloLC request to move Posthog initialization outside of providers.tsx

This commit is contained in:
Amit Sharma
2025-07-30 16:45:30 -07:00
parent c7e4c3d839
commit f7fe55d95a
2 changed files with 26 additions and 12 deletions
+4 -11
View File
@@ -7,8 +7,8 @@ import { ThemeProvider as NextThemesProvider } from "next-themes";
import { ThemeProviderProps } from "next-themes/dist/types";
import posthog from "posthog-js";
import { PostHogProvider as PHProvider } from "posthog-js/react";
import { useEffect } from "react";
import * as React from "react";
import { initializePostHog } from "@/lib/analytics";
export interface ProvidersProps {
children: React.ReactNode;
@@ -18,16 +18,9 @@ export interface ProvidersProps {
export function Providers({ children, themeProps }: ProvidersProps) {
const router = useRouter();
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
autocapture: false,
defaults: "2025-05-24",
capture_exceptions: true,
capture_pageview: false,
capture_pageleave: false,
});
// Initialize PostHog
React.useMemo(() => {
initializePostHog();
}, []);
return (
+22 -1
View File
@@ -1,5 +1,24 @@
import posthog from "posthog-js";
// Initialize PostHog
export const initializePostHog = (): void => {
if (typeof window === "undefined") return; // Don't initialize on server side
try {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
autocapture: false,
defaults: "2025-05-24",
capture_exceptions: true,
capture_pageview: false,
capture_pageleave: false,
});
} catch (error) {
console.error("Failed to initialize PostHog:", error);
}
};
// Type definitions for tracking payloads
export interface UserLoginPayload {
email: string;
@@ -144,7 +163,9 @@ export const setUserProperties = (properties: Record<string, any>): void => {
export const isAnalyticsReady = (): boolean => {
try {
return (
typeof posthog !== "undefined" && posthog._isIdentified !== undefined
typeof window !== "undefined" &&
typeof posthog !== "undefined" &&
posthog.__loaded === true
);
} catch {
return false;