diff --git a/apps/remix/app/entry.client.tsx b/apps/remix/app/entry.client.tsx index c32cdc4e1..6c2c0fc65 100644 --- a/apps/remix/app/entry.client.tsx +++ b/apps/remix/app/entry.client.tsx @@ -3,7 +3,6 @@ import { StrictMode, startTransition, useEffect } from 'react'; import { i18n } from '@lingui/core'; import { detect, fromHtmlTag } from '@lingui/detect-locale'; import { I18nProvider } from '@lingui/react'; -import posthog from 'posthog-js'; import { hydrateRoot } from 'react-dom/client'; import { HydratedRouter } from 'react-router/dom'; @@ -17,9 +16,11 @@ function PosthogInit() { useEffect(() => { if (postHogConfig) { - posthog.init(postHogConfig.key, { - api_host: postHogConfig.host, - capture_exceptions: true, + void import('posthog-js').then(({ default: posthog }) => { + posthog.init(postHogConfig.key, { + api_host: postHogConfig.host, + capture_exceptions: true, + }); }); } }, []); diff --git a/packages/lib/client-only/hooks/use-analytics.ts b/packages/lib/client-only/hooks/use-analytics.ts index 92a92c40c..ce302f7df 100644 --- a/packages/lib/client-only/hooks/use-analytics.ts +++ b/packages/lib/client-only/hooks/use-analytics.ts @@ -1,7 +1,15 @@ -import { posthog } from 'posthog-js'; - import { extractPostHogConfig } from '@documenso/lib/constants/feature-flags'; +let posthogPromise: Promise | null = null; + +const getPosthog = async () => { + if (!posthogPromise) { + posthogPromise = import('posthog-js'); + } + + return posthogPromise; +}; + export function useAnalytics() { // const featureFlags = useFeatureFlags(); const isPostHogEnabled = extractPostHogConfig(); @@ -17,7 +25,9 @@ export function useAnalytics() { return; } - posthog.capture(event, properties); + void getPosthog().then(({ default: posthog }) => { + posthog.capture(event, properties); + }); }; /** @@ -31,7 +41,9 @@ export function useAnalytics() { return; } - posthog.captureException(error, properties); + void getPosthog().then(({ default: posthog }) => { + posthog.captureException(error, properties); + }); }; /**