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(analytics): make analytics optional for self-hosted installations #465

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions apps/frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}
Expand All @@ -49,6 +53,7 @@ export default async function AppLayout({ children }: { children: ReactNode }) {
<PHProvider
phkey={process.env.NEXT_PUBLIC_POSTHOG_KEY}
host={process.env.NEXT_PUBLIC_POSTHOG_HOST}
enabled={enablePosthog}
>
<UtmSaver />
<LayoutContext>{children}</LayoutContext>
Expand All @@ -58,4 +63,4 @@ export default async function AppLayout({ children }: { children: ReactNode }) {
</body>
</html>
);
}
}
6 changes: 3 additions & 3 deletions libraries/helpers/src/utils/use.fire.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -23,6 +23,6 @@ export const useFireEvents = () => {
posthog.capture(name, props);
plausible(name, { props });
},
[user]
[user, analyticsEnabled]
);
};
9 changes: 5 additions & 4 deletions libraries/react-shared-libraries/src/helpers/posthog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 <PostHogProvider client={posthog}>{children}</PostHogProvider>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface VariableContextInterface {
backendUrl: string;
discordUrl: string;
uploadDirectory: string;
analyticsEnabled: boolean;
}
const VariableContext = createContext({
billingEnabled: false,
Expand Down