mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-01-26 15:38:14 -05:00
fix(analytics): only try to init Posthog when on cloud (#11843)
## Changes 🏗️ This prevents Posthog from being initialised locally, where we should not be collecting analytics during local development. ## Checklist 📋 ### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] Run locally and test the above
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
"use client";
|
||||
|
||||
import { useSupabase } from "@/lib/supabase/hooks/useSupabase";
|
||||
import { environment } from "@/services/environment";
|
||||
import { PostHogProvider as PHProvider } from "@posthog/react";
|
||||
import { usePathname, useSearchParams } from "next/navigation";
|
||||
import posthog from "posthog-js";
|
||||
import { ReactNode, useEffect, useRef } from "react";
|
||||
|
||||
export function PostHogProvider({ children }: { children: ReactNode }) {
|
||||
const isPostHogEnabled = environment.isPostHogEnabled();
|
||||
|
||||
useEffect(() => {
|
||||
if (process.env.NEXT_PUBLIC_POSTHOG_KEY) {
|
||||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
||||
@@ -19,15 +22,18 @@ export function PostHogProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
if (!isPostHogEnabled) return <>{children}</>;
|
||||
|
||||
return <PHProvider client={posthog}>{children}</PHProvider>;
|
||||
}
|
||||
|
||||
export function PostHogUserTracker() {
|
||||
const { user, isUserLoading } = useSupabase();
|
||||
const previousUserIdRef = useRef<string | null>(null);
|
||||
const isPostHogEnabled = environment.isPostHogEnabled();
|
||||
|
||||
useEffect(() => {
|
||||
if (isUserLoading) return;
|
||||
if (isUserLoading || !isPostHogEnabled) return;
|
||||
|
||||
if (user) {
|
||||
if (previousUserIdRef.current !== user.id) {
|
||||
@@ -41,7 +47,7 @@ export function PostHogUserTracker() {
|
||||
posthog.reset();
|
||||
previousUserIdRef.current = null;
|
||||
}
|
||||
}, [user, isUserLoading]);
|
||||
}, [user, isUserLoading, isPostHogEnabled]);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -49,16 +55,17 @@ export function PostHogUserTracker() {
|
||||
export function PostHogPageViewTracker() {
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const isPostHogEnabled = environment.isPostHogEnabled();
|
||||
|
||||
useEffect(() => {
|
||||
if (pathname) {
|
||||
if (pathname && isPostHogEnabled) {
|
||||
let url = window.origin + pathname;
|
||||
if (searchParams && searchParams.toString()) {
|
||||
url = url + `?${searchParams.toString()}`;
|
||||
}
|
||||
posthog.capture("$pageview", { $current_url: url });
|
||||
}
|
||||
}, [pathname, searchParams]);
|
||||
}, [pathname, searchParams, isPostHogEnabled]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,13 @@ function getPreviewStealingDev() {
|
||||
return branch;
|
||||
}
|
||||
|
||||
function getPostHogCredentials() {
|
||||
return {
|
||||
key: process.env.NEXT_PUBLIC_POSTHOG_KEY,
|
||||
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
||||
};
|
||||
}
|
||||
|
||||
function isProductionBuild() {
|
||||
return process.env.NODE_ENV === "production";
|
||||
}
|
||||
@@ -116,6 +123,13 @@ function areFeatureFlagsEnabled() {
|
||||
return process.env.NEXT_PUBLIC_LAUNCHDARKLY_ENABLED === "enabled";
|
||||
}
|
||||
|
||||
function isPostHogEnabled() {
|
||||
const inCloud = isCloud();
|
||||
const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
|
||||
const host = process.env.NEXT_PUBLIC_POSTHOG_HOST;
|
||||
return inCloud && key && host;
|
||||
}
|
||||
|
||||
export const environment = {
|
||||
// Generic
|
||||
getEnvironmentStr,
|
||||
@@ -128,6 +142,7 @@ export const environment = {
|
||||
getSupabaseUrl,
|
||||
getSupabaseAnonKey,
|
||||
getPreviewStealingDev,
|
||||
getPostHogCredentials,
|
||||
// Assertions
|
||||
isServerSide,
|
||||
isClientSide,
|
||||
@@ -138,5 +153,6 @@ export const environment = {
|
||||
isCloud,
|
||||
isLocal,
|
||||
isVercelPreview,
|
||||
isPostHogEnabled,
|
||||
areFeatureFlagsEnabled,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user