From de833f846bc8181462ed973fc50634b961ea2078 Mon Sep 17 00:00:00 2001 From: Rohit Dash Date: Tue, 3 Dec 2024 16:13:28 +0000 Subject: [PATCH] feat(analytics): make analytics optional for self-hosted installations --- .env.example | 4 ++++ apps/frontend/src/app/layout.tsx | 9 +++++++-- libraries/helpers/src/utils/use.fire.events.ts | 6 +++--- libraries/react-shared-libraries/src/helpers/posthog.tsx | 9 +++++---- .../src/helpers/variable.context.tsx | 1 + 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 7ca10d13b..94c180571 100644 --- a/.env.example +++ b/.env.example @@ -88,3 +88,7 @@ STRIPE_SIGNING_KEY_CONNECT="" # Developer Settings NX_ADD_PLUGINS=false IS_GENERAL="true" # required for now + +# Analytics Configuration +ENABLE_PLAUSIBLE=false # Set to "true" to enable Plausible analytics +ENABLE_POSTHOG=false # Set to "true" to enable PostHog analytics \ No newline at end of file diff --git a/apps/frontend/src/app/layout.tsx b/apps/frontend/src/app/layout.tsx index c143d959a..9ee82aa2a 100644 --- a/apps/frontend/src/app/layout.tsx +++ b/apps/frontend/src/app/layout.tsx @@ -17,7 +17,10 @@ import UtmSaver from '@gitroom/helpers/utils/utm.saver'; const chakra = Chakra_Petch({ weight: '400', subsets: ['latin'] }); export default async function AppLayout({ children }: { children: ReactNode }) { - const Plausible = !!process.env.STRIPE_PUBLISHABLE_KEY + const enablePlausible = process.env.ENABLE_PLAUSIBLE === 'true'; + const enablePosthog = process.env.ENABLE_POSTHOG === 'true'; + + const Plausible = (!!process.env.STRIPE_PUBLISHABLE_KEY && enablePlausible) ? PlausibleProvider : Fragment; @@ -38,6 +41,7 @@ export default async function AppLayout({ children }: { children: ReactNode }) { backendUrl={process.env.NEXT_PUBLIC_BACKEND_URL!} plontoKey={process.env.NEXT_PUBLIC_POLOTNO!} billingEnabled={!!process.env.STRIPE_PUBLISHABLE_KEY} + analyticsEnabled={enablePlausible || enablePosthog} discordUrl={process.env.NEXT_PUBLIC_DISCORD_SUPPORT!} frontEndUrl={process.env.FRONTEND_URL!} isGeneral={!!process.env.IS_GENERAL} @@ -49,6 +53,7 @@ export default async function AppLayout({ children }: { children: ReactNode }) { {children} @@ -58,4 +63,4 @@ export default async function AppLayout({ children }: { children: ReactNode }) { ); -} +} \ No newline at end of file diff --git a/libraries/helpers/src/utils/use.fire.events.ts b/libraries/helpers/src/utils/use.fire.events.ts index 2f39fd569..77fce88b3 100644 --- a/libraries/helpers/src/utils/use.fire.events.ts +++ b/libraries/helpers/src/utils/use.fire.events.ts @@ -5,14 +5,14 @@ import { useVariables } from '@gitroom/react/helpers/variable.context'; import { useUser } from '@gitroom/frontend/components/layout/user.context'; export const useFireEvents = () => { - const { billingEnabled } = useVariables(); + const { billingEnabled, analyticsEnabled } = useVariables(); const plausible = usePlausible(); const posthog = usePostHog(); const user = useUser(); return useCallback( (name: string, props?: any) => { - if (!billingEnabled) { + if (!billingEnabled || !analyticsEnabled) { return; } @@ -23,6 +23,6 @@ export const useFireEvents = () => { posthog.capture(name, props); plausible(name, { props }); }, - [user] + [user, analyticsEnabled] ); }; diff --git a/libraries/react-shared-libraries/src/helpers/posthog.tsx b/libraries/react-shared-libraries/src/helpers/posthog.tsx index 08e4e9f80..0f79ece88 100644 --- a/libraries/react-shared-libraries/src/helpers/posthog.tsx +++ b/libraries/react-shared-libraries/src/helpers/posthog.tsx @@ -8,9 +8,10 @@ export const PHProvider: FC<{ children: ReactNode; phkey?: string; host?: string; -}> = ({ children, phkey, host }) => { + enabled?: boolean; +}> = ({ children, phkey, host, enabled = false }) => { useEffect(() => { - if (!phkey || !host) { + if (!enabled || !phkey || !host) { return; } @@ -19,9 +20,9 @@ export const PHProvider: FC<{ person_profiles: 'identified_only', capture_pageview: false, // Disable automatic pageview capture, as we capture manually }); - }, []); + }, [enabled]); - if (!phkey || !host) { + if (!enabled || !phkey || !host) { return <>{children}; } return {children}; diff --git a/libraries/react-shared-libraries/src/helpers/variable.context.tsx b/libraries/react-shared-libraries/src/helpers/variable.context.tsx index dbadde01d..b679d0cf0 100644 --- a/libraries/react-shared-libraries/src/helpers/variable.context.tsx +++ b/libraries/react-shared-libraries/src/helpers/variable.context.tsx @@ -11,6 +11,7 @@ interface VariableContextInterface { backendUrl: string; discordUrl: string; uploadDirectory: string; + analyticsEnabled: boolean; } const VariableContext = createContext({ billingEnabled: false,