Skip to content

Commit

Permalink
sprig user id for authenticated users (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Winston-Hsiao authored Sep 20, 2024
1 parent 3ab2565 commit 51457d2
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions frontend/src/components/SprigInitializer.tsx
Original file line number Diff line number Diff line change
@@ -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;
};
Expand Down

0 comments on commit 51457d2

Please sign in to comment.