core/apps/webapp/app/hooks/usePostHog.ts
Harshith Mullapudi 4a0a57cb97
Feat: add documents to the kg (#64)
* Feat: add documents to the kg

* Feat: add versioning to documents

* Fix: invalidation of evolved facts

* fix: mcp return

* fix: invalidAt is not displayed in graph popover

* Fix: use document id for the flow

* refactor: consolidate document versioning around sessionId instead of documentId

* fix: add docs link in welcome email

* fix: give more time for larger graphs to settle on

* bump: new version 0.1.20

---------

Co-authored-by: Manoj K <saimanoj58@gmail.com>
2025-09-03 12:39:46 +05:30

54 lines
1.5 KiB
TypeScript

import { useLocation } from "@remix-run/react";
import posthog from "posthog-js";
import { useEffect, useRef } from "react";
import { useOptionalUser, useUserChanged } from "./useUser";
export const usePostHog = (
apiKey?: string,
logging = false,
debug = false,
): void => {
const postHogInitialized = useRef(false);
const location = useLocation();
const user = useOptionalUser();
//start PostHog once
useEffect(() => {
if (apiKey === undefined || apiKey === "") return;
if (postHogInitialized.current === true) return;
if (logging) console.log("Initializing PostHog");
posthog.init(apiKey, {
api_host: "https://us.i.posthog.com",
opt_in_site_apps: true,
debug,
loaded: function (posthog) {
if (logging) console.log("PostHog loaded");
if (user !== undefined) {
if (logging) console.log("Loaded: Identifying user", user);
posthog.identify(user.id, { email: user.email });
}
},
});
postHogInitialized.current = true;
}, [apiKey, logging, user]);
useUserChanged((user) => {
if (postHogInitialized.current === false) return;
if (logging) console.log("User changed");
if (user) {
if (logging) console.log("Identifying user", user);
posthog.identify(user.id, { email: user.email });
} else {
if (logging) console.log("Resetting user");
posthog.reset();
}
});
//page view
useEffect(() => {
if (postHogInitialized.current === false) return;
posthog.capture("$pageview");
}, [location, logging]);
};