diff options
| author | Saksham Kushwaha <[email protected]> | 2025-10-28 04:40:15 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-10-27 16:10:15 -0700 |
| commit | cccb35160de6c803def1cfbb5447d272a3c50c37 (patch) | |
| tree | 79358543fe626b818fc19065bec00d990edf0041 | |
| parent | Fix/invalid dom property (#526) (diff) | |
| download | supermemory-cccb35160de6c803def1cfbb5447d272a3c50c37.tar.xz supermemory-cccb35160de6c803def1cfbb5447d272a3c50c37.zip | |
feat: optional posthog intialization (#525)
| -rw-r--r-- | apps/web/lib/analytics.ts | 49 | ||||
| -rw-r--r-- | packages/lib/error-tracking.tsx | 3 | ||||
| -rw-r--r-- | packages/lib/posthog.tsx | 13 |
3 files changed, 40 insertions, 25 deletions
diff --git a/apps/web/lib/analytics.ts b/apps/web/lib/analytics.ts index 428ac872..f73c03b5 100644 --- a/apps/web/lib/analytics.ts +++ b/apps/web/lib/analytics.ts @@ -1,7 +1,14 @@ import posthog from "posthog-js" +// Helper function to safely capture events +const safeCapture = (eventName: string, properties?: Record<string, any>) => { + if (posthog.__loaded) { + posthog.capture(eventName, properties) + } +} + export const analytics = { - userSignedOut: () => posthog.capture("user_signed_out"), + userSignedOut: () => safeCapture("user_signed_out"), memoryAdded: (props: { type: "note" | "link" | "file" @@ -9,35 +16,35 @@ export const analytics = { content_length?: number file_size?: number file_type?: string - }) => posthog.capture("memory_added", props), + }) => safeCapture("memory_added", props), - memoryDetailOpened: () => posthog.capture("memory_detail_opened"), + memoryDetailOpened: () => safeCapture("memory_detail_opened"), - projectCreated: () => posthog.capture("project_created"), + projectCreated: () => safeCapture("project_created"), - newChatStarted: () => posthog.capture("new_chat_started"), - chatHistoryViewed: () => posthog.capture("chat_history_viewed"), - chatDeleted: () => posthog.capture("chat_deleted"), + newChatStarted: () => safeCapture("new_chat_started"), + chatHistoryViewed: () => safeCapture("chat_history_viewed"), + chatDeleted: () => safeCapture("chat_deleted"), viewModeChanged: (mode: "graph" | "list") => - posthog.capture("view_mode_changed", { mode }), + safeCapture("view_mode_changed", { mode }), - documentCardClicked: () => posthog.capture("document_card_clicked"), + documentCardClicked: () => safeCapture("document_card_clicked"), - billingViewed: () => posthog.capture("billing_viewed"), - upgradeInitiated: () => posthog.capture("upgrade_initiated"), - upgradeCompleted: () => posthog.capture("upgrade_completed"), - billingPortalOpened: () => posthog.capture("billing_portal_opened"), + billingViewed: () => safeCapture("billing_viewed"), + upgradeInitiated: () => safeCapture("upgrade_initiated"), + upgradeCompleted: () => safeCapture("upgrade_completed"), + billingPortalOpened: () => safeCapture("billing_portal_opened"), connectionAdded: (provider: string) => - posthog.capture("connection_added", { provider }), - connectionDeleted: () => posthog.capture("connection_deleted"), - connectionAuthStarted: () => posthog.capture("connection_auth_started"), - connectionAuthCompleted: () => posthog.capture("connection_auth_completed"), - connectionAuthFailed: () => posthog.capture("connection_auth_failed"), + safeCapture("connection_added", { provider }), + connectionDeleted: () => safeCapture("connection_deleted"), + connectionAuthStarted: () => safeCapture("connection_auth_started"), + connectionAuthCompleted: () => safeCapture("connection_auth_completed"), + connectionAuthFailed: () => safeCapture("connection_auth_failed"), - mcpViewOpened: () => posthog.capture("mcp_view_opened"), - mcpInstallCmdCopied: () => posthog.capture("mcp_install_cmd_copied"), + mcpViewOpened: () => safeCapture("mcp_view_opened"), + mcpInstallCmdCopied: () => safeCapture("mcp_install_cmd_copied"), - extensionInstallClicked: () => posthog.capture("extension_install_clicked"), + extensionInstallClicked: () => safeCapture("extension_install_clicked"), } diff --git a/packages/lib/error-tracking.tsx b/packages/lib/error-tracking.tsx index bf320271..de8cc89d 100644 --- a/packages/lib/error-tracking.tsx +++ b/packages/lib/error-tracking.tsx @@ -14,6 +14,7 @@ export function useErrorTracking() { error: Error | unknown, context?: Record<string, any>, ) => { + if (!posthog.__loaded) return const errorDetails = { error_message: error instanceof Error ? error.message : String(error), error_stack: error instanceof Error ? error.stack : undefined, @@ -117,6 +118,7 @@ export function useInteractionTracking() { const pathname = usePathname() const trackInteraction = (action: string, details?: Record<string, any>) => { + if (!posthog.__loaded) return posthog.capture("user_interaction", { action, pathname, @@ -131,6 +133,7 @@ export function useInteractionTracking() { success: boolean, details?: Record<string, any>, ) => { + if (!posthog.__loaded) return posthog.capture("form_submission", { form_name: formName, success, diff --git a/packages/lib/posthog.tsx b/packages/lib/posthog.tsx index ac563aae..62a60e09 100644 --- a/packages/lib/posthog.tsx +++ b/packages/lib/posthog.tsx @@ -11,7 +11,7 @@ function PostHogPageTracking() { // Page tracking useEffect(() => { - if (pathname) { + if (pathname && posthog.__loaded) { let url = window.origin + pathname if (searchParams.toString()) { url = `${url}?${searchParams.toString()}` @@ -38,19 +38,24 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) { useEffect(() => { if (typeof window !== "undefined") { - posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY ?? "", { + const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY + if (posthogKey){ + posthog.init(posthogKey, { api_host: process.env.NEXT_PUBLIC_BACKEND_URL + "/orange", ui_host: "https://us.i.posthog.com", person_profiles: "identified_only", capture_pageview: false, capture_pageleave: true, - }) + })} + else{ + console.warn("PostHog API key is not set. PostHog will not be initialized.") + } } }, []) // User identification useEffect(() => { - if (session?.user) { + if (session?.user && posthog.__loaded) { posthog.identify(session.user.id, { email: session.user.email, name: session.user.name, |