Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(cloud): remove unnecessary posthog events (#4798)

Marc Klingen8fd93ecff9b8
diff --git a/web/src/features/setup/components/SetupTracingButton.tsx b/web/src/features/setup/components/SetupTracingButton.tsx
index eb3e8f690e..e625098fe1 100644
--- a/web/src/features/setup/components/SetupTracingButton.tsx
+++ b/web/src/features/setup/components/SetupTracingButton.tsx
@@ -6,7 +6,7 @@ import { setupTracingRoute } from "@/src/features/setup/setupRoutes";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { LockIcon } from "lucide-react";
import { useRouter } from "next/router";
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
const SetupTracingButton = () => {
@@ -27,10 +27,13 @@ const SetupTracingButton = () => {
},
);
+ // dedupe result via useRef, otherwise we'll capture the event multiple times on session refresh
+ const capturedEventAlready = useRef<boolean | undefined>(undefined);
const capture = usePostHogClientCapture();
useEffect(() => {
- if (hasAnyTrace !== undefined) {
+ if (hasAnyTrace !== undefined && !capturedEventAlready.current) {
capture("onboarding:tracing_check_active", { active: hasAnyTrace });
+ capturedEventAlready.current = true;
}
}, [hasAnyTrace, capture]);
diff --git a/web/src/pages/_app.tsx b/web/src/pages/_app.tsx
index f7085badb4..03b2822ee0 100644
--- a/web/src/pages/_app.tsx
+++ b/web/src/pages/_app.tsx
@@ -12,7 +12,7 @@ import { QueryParamProvider } from "use-query-params";
import "@/src/styles/globals.css";
import Layout from "@/src/components/layouts/layout";
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import { useRouter } from "next/router";
import posthog from "posthog-js";
@@ -62,6 +62,7 @@ if (
if (process.env.NODE_ENV === "development") posthog.debug();
},
autocapture: false,
+ enable_heatmaps: false,
});
}
@@ -126,8 +127,16 @@ function UserTracking() {
const sessionUser = session.data?.user;
const { organization, project } = useQueryProjectOrOrganization();
+ // dedupe the event via useRef, otherwise we'll capture the event multiple times on session refresh
+ const lastIdentifiedUser = useRef<string | null>(null);
+
useEffect(() => {
- if (sessionUser) {
+ if (
+ session.status === "authenticated" &&
+ sessionUser &&
+ lastIdentifiedUser.current !== JSON.stringify(sessionUser)
+ ) {
+ lastIdentifiedUser.current = JSON.stringify(sessionUser);
// PostHog
if (env.NEXT_PUBLIC_POSTHOG_KEY && env.NEXT_PUBLIC_POSTHOG_HOST)
posthog.identify(sessionUser.id ?? undefined, {
@@ -171,7 +180,8 @@ function UserTracking() {
: "undefined",
},
});
- } else {
+ } else if (session.status === "unauthenticated") {
+ lastIdentifiedUser.current = null;
// PostHog
if (env.NEXT_PUBLIC_POSTHOG_KEY && env.NEXT_PUBLIC_POSTHOG_HOST) {
posthog.reset();
@@ -180,7 +190,7 @@ function UserTracking() {
// Sentry
setUser(null);
}
- }, [sessionUser]);
+ }, [sessionUser, session.status]);
// update crisp segments
const plan = organization?.plan;
@@ -193,7 +203,6 @@ function UserTracking() {
useEffect(() => {
let segments = [];
if (plan && !currentOrgIsDemoOrg) {
- console.log("setting chat segments", plan);
segments.push("plan:" + plan);
}
if (currentOrgIsDemoOrg) {