46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
import { usePostHog } from "posthog-js/react";
|
|
|
|
/**
|
|
* Composant pour identifier l'utilisateur dans PostHog
|
|
* À placer dans le layout après l'authentification
|
|
*/
|
|
export default function PostHogIdentifier() {
|
|
const posthog = usePostHog();
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
try {
|
|
const res = await fetch("/api/me", { credentials: "include", cache: "no-store" });
|
|
if (!res.ok) {
|
|
// Si pas authentifié, on réinitialise l'identité
|
|
posthog?.reset();
|
|
return;
|
|
}
|
|
|
|
const me = await res.json();
|
|
|
|
// L'API /api/me retourne { user: { id, email, ... }, active_org_id, ... }
|
|
const userId = me?.user?.id || me?.user_id;
|
|
|
|
if (userId) {
|
|
// Identifier l'utilisateur avec son ID unique
|
|
posthog?.identify(userId, {
|
|
email: me.user?.email || me.email,
|
|
first_name: me.user?.first_name || me.first_name,
|
|
display_name: me.user?.display_name || me.display_name,
|
|
company_name: me.active_org_name,
|
|
company_id: me.active_org_id,
|
|
is_staff: me.is_staff || false,
|
|
});
|
|
}
|
|
} catch (e) {
|
|
console.error('PostHog: Erreur lors de l\'identification:', e);
|
|
}
|
|
})();
|
|
}, [posthog]);
|
|
|
|
return null;
|
|
}
|