From 51457d285c4ba1ceefb32f329a3c86fc1ff7f235 Mon Sep 17 00:00:00 2001 From: Winston Hsiao <96440583+Winston-Hsiao@users.noreply.github.com> Date: Fri, 20 Sep 2024 00:11:22 -0400 Subject: [PATCH] sprig user id for authenticated users (#414) --- frontend/src/components/SprigInitializer.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/SprigInitializer.tsx b/frontend/src/components/SprigInitializer.tsx index 95d706d5..229966bb 100644 --- a/frontend/src/components/SprigInitializer.tsx +++ b/frontend/src/components/SprigInitializer.tsx @@ -1,22 +1,32 @@ import { useEffect } from "react"; import { useLocation } from "react-router-dom"; +import { useAuthentication } from "@/hooks/useAuth"; + const SprigInitializer = () => { const location = useLocation(); + const { currentUser, isAuthenticated } = useAuthentication(); useEffect(() => { - const checkSprig = () => { + const initializeSprig = () => { if (window.Sprig && typeof window.Sprig === "function") { console.log("Sprig initialized"); + + // Set user ID if authenticated + if (isAuthenticated && currentUser) { + window.Sprig("setUserId", currentUser.id); + } + + // Track page view window.Sprig("track", "page_view", { path: location.pathname }); } else { console.log("Sprig is not ready yet. Retrying..."); - setTimeout(checkSprig, 500); + setTimeout(initializeSprig, 500); } }; - checkSprig(); - }, [location.pathname]); + initializeSprig(); + }, [location.pathname, isAuthenticated, currentUser]); return null; };