Skip to content

Commit e849dce

Browse files
committed
fix: posthog tracking
1 parent 95ce79f commit e849dce

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/client/app/onboarding/page.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ const OnboardingPage = () => {
357357
result.message || "Failed to save onboarding data"
358358
)
359359
}
360+
// Identify the user in PostHog as soon as we have their name
361+
posthog?.identify(
362+
(await (await fetch("/api/user/profile")).json()).sub, // Fetch user ID from session
363+
{ name: mainOnboardingData["user-name"] }
364+
)
365+
posthog?.capture("user_signed_up", {
366+
signup_method: "auth0", // or derive from user profile if available
367+
referral_source: "direct" // Placeholder, can be populated from URL params
368+
})
360369
posthog?.capture("onboarding_completed")
361370
router.push("/chat?show_demo=true")
362371
} catch (error) {

src/client/components/LayoutWrapper.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useGlobalShortcuts } from "@hooks/useGlobalShortcuts"
1919
import { cn } from "@utils/cn"
2020
import toast from "react-hot-toast"
2121
import { useUser } from "@auth0/nextjs-auth0"
22+
import { usePostHog } from "posthog-js/react"
2223

2324
// ... (keep the rest of your imports and context creation)
2425
export const PlanContext = createContext({
@@ -56,6 +57,7 @@ export default function LayoutWrapper({ children }) {
5657
const pathname = usePathname()
5758
const router = useRouter()
5859
const searchParams = useSearchParams() // Hook to read URL query parameters
60+
const posthog = usePostHog()
5961

6062
const { user, error: authError, isLoading: isAuthLoading } = useUser()
6163

@@ -64,10 +66,25 @@ export default function LayoutWrapper({ children }) {
6466

6567
const showNav = !["/", "/onboarding"].includes(pathname)
6668

69+
useEffect(() => {
70+
if (user && posthog) {
71+
posthog.identify(user.sub, {
72+
name: user.name,
73+
email: user.email
74+
})
75+
}
76+
}, [user, posthog])
77+
6778
useEffect(() => {
6879
const paymentStatus = searchParams.get("payment_status")
6980
const needsRefresh = searchParams.get("refresh_session")
7081

82+
if (paymentStatus === "success" && posthog) {
83+
posthog.capture("plan_upgraded", {
84+
plan_name: "pro"
85+
// MRR and billing_cycle are not available on the client
86+
})
87+
}
7188
// Check for either trigger
7289
if (paymentStatus === "success" || needsRefresh === "true") {
7390
// CRITICAL FIX: Clean the URL synchronously *before* doing anything else.

src/client/components/Sidebar.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { cn } from "@utils/cn"
3333
import { usePlan } from "@hooks/usePlan"
3434
import { motion, AnimatePresence } from "framer-motion"
35+
import { usePostHog } from "posthog-js/react"
3536
import useClickOutside from "@hooks/useClickOutside"
3637

3738
const proPlanFeatures = [
@@ -238,6 +239,7 @@ const UserProfileSection = ({ isCollapsed, user }) => {
238239
const [isUserMenuOpen, setUserMenuOpen] = useState(false)
239240
const userMenuRef = useRef(null)
240241
const { isPro, plan } = usePlan()
242+
const posthog = usePostHog()
241243
useClickOutside(userMenuRef, () => setUserMenuOpen(false))
242244

243245
const planName = isPro ? "Pro" : "Basic"
@@ -321,6 +323,9 @@ const UserProfileSection = ({ isCollapsed, user }) => {
321323
</a>
322324
<a
323325
href="/auth/logout"
326+
onClick={() => {
327+
posthog?.reset()
328+
}}
324329
className="w-full flex items-center gap-2 text-left px-3 py-2 text-sm rounded-md text-red-400 hover:bg-red-500/20 hover:text-red-300 transition-colors"
325330
>
326331
<IconLogout size={16} />

0 commit comments

Comments
 (0)