From e551dacb7ecd72e915736f5d65a342702cfdd799 Mon Sep 17 00:00:00 2001 From: LAKHAN BAHETI Date: Tue, 12 Mar 2024 22:07:23 +0530 Subject: [PATCH] chore: telemetry update & refactoring --- web/components/onboarding/invite-members.tsx | 19 +++++++----- web/components/project/member-list-item.tsx | 2 +- web/lib/posthog-provider.tsx | 29 +++++++++---------- .../[workspaceSlug]/settings/members.tsx | 26 ++++++++--------- web/pages/invitations/index.tsx | 3 +- web/store/event-tracker.store.ts | 4 ++- 6 files changed, 44 insertions(+), 39 deletions(-) diff --git a/web/components/onboarding/invite-members.tsx b/web/components/onboarding/invite-members.tsx index d0540a5a4..f260e6aef 100644 --- a/web/components/onboarding/invite-members.tsx +++ b/web/components/onboarding/invite-members.tsx @@ -25,7 +25,7 @@ import { EUserWorkspaceRoles, ROLE } from "constants/workspace"; // helpers import { getUserRole } from "helpers/user.helper"; // hooks -import { useEventTracker } from "hooks/store"; +import { useApplication, useEventTracker } from "hooks/store"; import useDynamicDropdownPosition from "hooks/use-dynamic-dropdown"; // assets import userDark from "public/onboarding/user-dark.svg"; @@ -271,6 +271,9 @@ export const InviteMembers: React.FC = (props) => { const { resolvedTheme } = useTheme(); // store hooks const { captureEvent } = useEventTracker(); + const { + instance: { instance }, + } = useApplication(); const { control, @@ -310,12 +313,14 @@ export const InviteMembers: React.FC = (props) => { }) .then(async () => { captureEvent(MEMBER_INVITED, { - emails: [ - ...payload.emails.map((email) => ({ - email: email.email, - role: getUserRole(email.role), - })), - ], + emails: !instance?.is_telemetry_anonymous + ? [ + ...payload.emails.map((email) => ({ + email: email.email, + role: getUserRole(email.role), + })), + ] + : undefined, project_id: undefined, state: "SUCCESS", element: "Onboarding", diff --git a/web/components/project/member-list-item.tsx b/web/components/project/member-list-item.tsx index 74df587fe..b2c847e58 100644 --- a/web/components/project/member-list-item.tsx +++ b/web/components/project/member-list-item.tsx @@ -69,7 +69,7 @@ export const ProjectMemberListItem: React.FC = observer((props) => { captureEvent(PROJECT_MEMBER_REMOVED, { member_id: userDetails.member.id, role: getUserRole(userDetails.role as number), - removed_by_role: getUserRole(currentProjectRole as number), + removed_by_role: currentProjectRole ? getUserRole(currentProjectRole as number) : undefined, state: "SUCCESS", element: "Project settings members page", }); diff --git a/web/lib/posthog-provider.tsx b/web/lib/posthog-provider.tsx index 80f0cfd55..527cb0e18 100644 --- a/web/lib/posthog-provider.tsx +++ b/web/lib/posthog-provider.tsx @@ -26,24 +26,17 @@ const PostHogProvider: FC = (props) => { // router const router = useRouter(); - if (!isCloud && !telemetryEnabled) return <>{children}; - useEffect(() => { if (user) { // Identify sends an event, so you want may want to limit how often you call it - posthog?.identify( - isCloud ? user.email : user.id, - isCloud - ? { - id: user.id, - first_name: user.first_name, - last_name: user.last_name, - email: user.email, - use_case: user.use_case, - workspaces: workspaceIds, - } - : undefined - ); + posthog?.identify(isCloud ? user.email : user.id, { + id: user.id, + first_name: isCloud ? user.first_name : undefined, + last_name: isCloud ? user.last_name : undefined, + email: isCloud ? user.email : undefined, + use_case: user.use_case, + workspaces: workspaceIds, + }); } }, [user, workspaceIds, isCloud]); @@ -55,6 +48,7 @@ const PostHogProvider: FC = (props) => { autocapture: false, capture_pageview: false, // Disable automatic pageview capture, as we capture manually }); + posthog?.opt_in_capturing(); } }, [posthogAPIKey, posthogHost, isCloud, telemetryEnabled]); @@ -80,6 +74,11 @@ const PostHogProvider: FC = (props) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + if (!isCloud && !telemetryEnabled) { + posthog?.opt_out_capturing(); + return <>{children}; + } + if (posthogAPIKey) { return {children}; } diff --git a/web/pages/[workspaceSlug]/settings/members.tsx b/web/pages/[workspaceSlug]/settings/members.tsx index e1be1d889..fe431f156 100644 --- a/web/pages/[workspaceSlug]/settings/members.tsx +++ b/web/pages/[workspaceSlug]/settings/members.tsx @@ -10,7 +10,7 @@ import { SendWorkspaceInvitationModal, WorkspaceMembersList } from "components/w import { MEMBER_INVITED } from "constants/event-tracker"; import { EUserWorkspaceRoles } from "constants/workspace"; import { getUserRole } from "helpers/user.helper"; -import { useEventTracker, useMember, useUser, useWorkspace } from "hooks/store"; +import { useApplication, useEventTracker, useMember, useUser, useWorkspace } from "hooks/store"; // layouts import { AppLayout } from "layouts/app-layout"; import { WorkspaceSettingLayout } from "layouts/settings-layout"; @@ -31,6 +31,9 @@ const WorkspaceMembersSettingsPage: NextPageWithLayout = observer(() => { const { workspaceSlug } = router.query; // store hooks const { captureEvent } = useEventTracker(); + const { + instance: { instance }, + } = useApplication(); const { membership: { currentWorkspaceRole }, } = useUser(); @@ -42,16 +45,18 @@ const WorkspaceMembersSettingsPage: NextPageWithLayout = observer(() => { const handleWorkspaceInvite = (data: IWorkspaceBulkInviteFormData) => { if (!workspaceSlug) return; + const emailsEventPayload = [ + ...data.emails.map((email) => ({ + email: email.email, + role: getUserRole(email.role), + })), + ]; return inviteMembersToWorkspace(workspaceSlug.toString(), data) .then(() => { setInviteModal(false); + captureEvent(MEMBER_INVITED, { - emails: [ - ...data.emails.map((email) => ({ - email: email.email, - role: getUserRole(email.role), - })), - ], + emails: !instance?.is_telemetry_anonymous ? emailsEventPayload : undefined, project_id: undefined, state: "SUCCESS", element: "Workspace settings member page", @@ -64,12 +69,7 @@ const WorkspaceMembersSettingsPage: NextPageWithLayout = observer(() => { }) .catch((err) => { captureEvent(MEMBER_INVITED, { - emails: [ - ...data.emails.map((email) => ({ - email: email.email, - role: getUserRole(email.role), - })), - ], + emails: !instance?.is_telemetry_anonymous ? emailsEventPayload : undefined, project_id: undefined, state: "FAILED", element: "Workspace settings member page", diff --git a/web/pages/invitations/index.tsx b/web/pages/invitations/index.tsx index 7f976865b..dc7abdb83 100644 --- a/web/pages/invitations/index.tsx +++ b/web/pages/invitations/index.tsx @@ -85,8 +85,7 @@ const UserInvitationsPage: NextPageWithLayout = observer(() => { joinWorkspaceMetricGroup(redirectWorkspace?.id); captureEvent(MEMBER_ACCEPTED, { member_id: invitation?.id, - // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain - role: getUserRole(invitation?.role!), + role: invitation?.role ? getUserRole(invitation?.role) : undefined, project_id: undefined, accepted_from: "App", state: "SUCCESS", diff --git a/web/store/event-tracker.store.ts b/web/store/event-tracker.store.ts index 6054494ff..612aed346 100644 --- a/web/store/event-tracker.store.ts +++ b/web/store/event-tracker.store.ts @@ -101,7 +101,9 @@ export class EventTrackerStore implements IEventTrackerStore { return { workspace_id: currentWorkspaceDetails?.id, project_id: currentProjectDetails?.id, - user_project_role: getUserRole(currentProjectDetails?.member_role as number), + user_project_role: currentProjectDetails?.member_role + ? getUserRole(currentProjectDetails?.member_role as number) + : undefined, user_workspace_role: getUserRole(currentWorkspaceRole as number), }; }