chore: telemetry setup for cloud & selfhosted

This commit is contained in:
LAKHAN BAHETI 2024-03-11 13:26:28 +05:30
parent f90e3172e6
commit 35a9527325

View File

@ -14,31 +14,49 @@ export interface IPosthogWrapper {
currentWorkspaceId: string | undefined; currentWorkspaceId: string | undefined;
posthogAPIKey: string | null; posthogAPIKey: string | null;
posthogHost: string | null; posthogHost: string | null;
isCloud: boolean;
telemetryEnabled: boolean;
} }
const PostHogProvider: FC<IPosthogWrapper> = (props) => { const PostHogProvider: FC<IPosthogWrapper> = (props) => {
const { children, user, workspaceIds, currentWorkspaceId, posthogAPIKey, posthogHost } = props; const {
children,
user,
workspaceIds,
currentWorkspaceId,
posthogAPIKey,
posthogHost,
isCloud = false,
telemetryEnabled = false,
} = props;
// states // states
const [lastWorkspaceId, setLastWorkspaceId] = useState(currentWorkspaceId); const [lastWorkspaceId, setLastWorkspaceId] = useState(currentWorkspaceId);
// router // router
const router = useRouter(); const router = useRouter();
if (!isCloud && !telemetryEnabled) return <>{children}</>;
useEffect(() => { useEffect(() => {
if (user) { if (user) {
// Identify sends an event, so you want may want to limit how often you call it // Identify sends an event, so you want may want to limit how often you call it
posthog?.identify(user.email, { posthog?.identify(
id: user.id, isCloud ? user.email : user.id,
first_name: user.first_name, isCloud
last_name: user.last_name, ? {
email: user.email, id: user.id,
use_case: user.use_case, first_name: user.first_name,
workspaces: workspaceIds, last_name: user.last_name,
}); email: user.email,
use_case: user.use_case,
workspaces: workspaceIds,
}
: undefined
);
} }
}, [user, workspaceIds]); }, [user, workspaceIds, isCloud]);
useEffect(() => { useEffect(() => {
if (posthogAPIKey && posthogHost) { if (posthogAPIKey && posthogHost && (isCloud || (!isCloud && telemetryEnabled))) {
posthog.init(posthogAPIKey, { posthog.init(posthogAPIKey, {
api_host: posthogHost || "https://app.posthog.com", api_host: posthogHost || "https://app.posthog.com",
debug: process.env.NEXT_PUBLIC_POSTHOG_DEBUG === "1", // Debug mode based on the environment variable debug: process.env.NEXT_PUBLIC_POSTHOG_DEBUG === "1", // Debug mode based on the environment variable
@ -46,16 +64,16 @@ const PostHogProvider: FC<IPosthogWrapper> = (props) => {
capture_pageview: false, // Disable automatic pageview capture, as we capture manually capture_pageview: false, // Disable automatic pageview capture, as we capture manually
}); });
} }
}, [posthogAPIKey, posthogHost]); }, [posthogAPIKey, posthogHost, isCloud, telemetryEnabled]);
useEffect(() => { useEffect(() => {
// Join workspace group on workspace change // Join workspace group on workspace change
if (lastWorkspaceId !== currentWorkspaceId && currentWorkspaceId && user) { if (lastWorkspaceId !== currentWorkspaceId && currentWorkspaceId && user) {
setLastWorkspaceId(currentWorkspaceId); setLastWorkspaceId(currentWorkspaceId);
posthog?.identify(user.email); posthog?.identify(isCloud ? user.email : user.id);
posthog?.group(GROUP_WORKSPACE, currentWorkspaceId); posthog?.group(GROUP_WORKSPACE, currentWorkspaceId);
} }
}, [currentWorkspaceId, lastWorkspaceId, user]); }, [currentWorkspaceId, lastWorkspaceId, user, isCloud]);
useEffect(() => { useEffect(() => {
// Track page views // Track page views