From 12aaef2419228da8c5aaaab7f35ab973aa17f0f1 Mon Sep 17 00:00:00 2001 From: LAKHAN BAHETI Date: Thu, 23 May 2024 13:19:47 +0530 Subject: [PATCH] chore: added telemetry configuration for cloud & selfhosted --- web/lib/app-provider.tsx | 12 ++++--- web/lib/posthog-provider.tsx | 61 ++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/web/lib/app-provider.tsx b/web/lib/app-provider.tsx index 1044af012..ef87b0dea 100644 --- a/web/lib/app-provider.tsx +++ b/web/lib/app-provider.tsx @@ -32,12 +32,12 @@ export interface IAppProvider { export const AppProvider: FC = observer((props) => { const { children } = props; // store hooks - const { config } = useInstance(); + const { config, instance } = useInstance(); const { data: currentUser, - membership: { currentProjectRole, currentWorkspaceRole }, + userProfile: { data: userProfile }, } = useUser(); - const { currentWorkspace } = useWorkspace(); + const { currentWorkspace, workspaces } = useWorkspace(); // themes const { resolvedTheme } = useTheme(); @@ -50,9 +50,11 @@ export const AppProvider: FC = observer((props) => { diff --git a/web/lib/posthog-provider.tsx b/web/lib/posthog-provider.tsx index 3517fb7a7..1b65b8447 100644 --- a/web/lib/posthog-provider.tsx +++ b/web/lib/posthog-provider.tsx @@ -2,25 +2,34 @@ import { FC, ReactNode, useEffect, useState } from "react"; import { useRouter } from "next/router"; import posthog from "posthog-js"; import { PostHogProvider as PHProvider } from "posthog-js/react"; -import { IUser } from "@plane/types"; +import { IUser, TUserProfile } from "@plane/types"; // constants import { GROUP_WORKSPACE } from "@/constants/event-tracker"; -// helpers -import { getUserRole } from "@/helpers/user.helper"; -// types export interface IPosthogWrapper { children: ReactNode; user: IUser | undefined; + userProfile: TUserProfile | undefined; + joinedWorkspaceIds: string[]; currentWorkspaceId: string | undefined; - workspaceRole: number | undefined; - projectRole: number | undefined; posthogAPIKey: string | undefined; posthogHost: string | undefined; + isCloud: boolean; + telemetryEnabled: boolean; } const PostHogProvider: FC = (props) => { - const { children, user, workspaceRole, currentWorkspaceId, projectRole, posthogAPIKey, posthogHost } = props; + const { + children, + user, + userProfile, + joinedWorkspaceIds, + currentWorkspaceId, + posthogAPIKey, + posthogHost, + isCloud, + telemetryEnabled, + } = props; // states const [lastWorkspaceId, setLastWorkspaceId] = useState(currentWorkspaceId); // router @@ -29,37 +38,42 @@ const PostHogProvider: FC = (props) => { useEffect(() => { if (user) { // Identify sends an event, so you want may want to limit how often you call it - posthog?.identify(user.email, { + posthog?.identify(isCloud ? user.email : user.id, { id: user.id, - first_name: user.first_name, - last_name: user.last_name, - email: user.email, - // use_case: user.use_case, FIXME: - workspace_role: workspaceRole ? getUserRole(workspaceRole) : undefined, - project_role: projectRole ? getUserRole(projectRole) : undefined, + first_name: isCloud ? user.first_name : undefined, + last_name: isCloud ? user.last_name : undefined, + email: isCloud ? user.email : undefined, + use_case: userProfile?.use_case, + role: userProfile?.role, + workspaces: joinedWorkspaceIds, }); } - }, [user, workspaceRole, projectRole]); + }, [user, joinedWorkspaceIds, isCloud, userProfile]); useEffect(() => { - if (posthogAPIKey && (process.env.NEXT_PUBLIC_POSTHOG_HOST || posthogHost)) { + if ( + posthogAPIKey && + (process.env.NEXT_PUBLIC_POSTHOG_HOST || posthogHost) && + (isCloud || (!isCloud && telemetryEnabled)) + ) { posthog.init(posthogAPIKey, { api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || posthogHost || "https://app.posthog.com", debug: process.env.NEXT_PUBLIC_POSTHOG_DEBUG === "1", // Debug mode based on the environment variable autocapture: false, capture_pageview: false, // Disable automatic pageview capture, as we capture manually }); + posthog?.opt_in_capturing(); } - }, [posthogAPIKey, posthogHost]); + }, [posthogAPIKey, posthogHost, isCloud, telemetryEnabled]); useEffect(() => { // Join workspace group on workspace change if (lastWorkspaceId !== currentWorkspaceId && currentWorkspaceId && user) { setLastWorkspaceId(currentWorkspaceId); - posthog?.identify(user.email); + posthog?.identify(isCloud ? user.email : user.id); posthog?.group(GROUP_WORKSPACE, currentWorkspaceId); } - }, [currentWorkspaceId, lastWorkspaceId, user]); + }, [currentWorkspaceId, lastWorkspaceId, user, isCloud]); useEffect(() => { // Track page views @@ -74,7 +88,14 @@ const PostHogProvider: FC = (props) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); - if (posthogAPIKey) return {children}; + if (!isCloud && !telemetryEnabled) { + posthog?.opt_out_capturing(); + return <>{children}; + } + + if (posthogAPIKey) { + return {children}; + } return <>{children}; };