From 4303658ddbda508f58921f740efe0a5652cbac20 Mon Sep 17 00:00:00 2001 From: sriram veeraghanta Date: Thu, 14 Sep 2023 23:02:49 +0530 Subject: [PATCH] adding capture function for tacking events in posthog --- web/components/issues/modal.tsx | 3 - .../issues/peek-overview/layout.tsx | 278 +++++++++--------- web/hooks/use-user.tsx | 2 - web/services/issues.service.ts | 5 + web/services/posthog.service.ts | 27 ++ web/store/issues.ts | 3 - 6 files changed, 170 insertions(+), 148 deletions(-) create mode 100644 web/services/posthog.service.ts diff --git a/web/components/issues/modal.tsx b/web/components/issues/modal.tsx index 8fbcfcf24..8f4becda0 100644 --- a/web/components/issues/modal.tsx +++ b/web/components/issues/modal.tsx @@ -34,7 +34,6 @@ import { } from "constants/fetch-keys"; // constants import { INBOX_ISSUE_SOURCE } from "constants/inbox"; -import { usePostHog } from "posthog-js/react"; import { ISSUE_CREATE } from "constants/posthog-events"; export interface IssuesModalProps { @@ -69,7 +68,6 @@ export const CreateUpdateIssueModal: React.FC = ({ fieldsToShow = ["all"], onSubmit, }) => { - const posthog = usePostHog(); // states const [createMore, setCreateMore] = useState(false); const [activeProject, setActiveProject] = useState(null); @@ -241,7 +239,6 @@ export const CreateUpdateIssueModal: React.FC = ({ await issuesService .createIssues(workspaceSlug as string, activeProject ?? "", payload, user) .then(async (res) => { - posthog?.capture(ISSUE_CREATE); mutate(PROJECT_ISSUES_LIST_WITH_PARAMS(activeProject ?? "", params)); if (payload.cycle && payload.cycle !== "") await addIssueToCycle(res.id, payload.cycle); if (payload.module && payload.module !== "") diff --git a/web/components/issues/peek-overview/layout.tsx b/web/components/issues/peek-overview/layout.tsx index 57bcb904c..726560114 100644 --- a/web/components/issues/peek-overview/layout.tsx +++ b/web/components/issues/peek-overview/layout.tsx @@ -26,110 +26,121 @@ type Props = { export type TPeekOverviewModes = "side" | "modal" | "full"; -export const IssuePeekOverview: React.FC = observer( - ({ handleMutation, projectId, readOnly, workspaceSlug }) => { - const [isSidePeekOpen, setIsSidePeekOpen] = useState(false); - const [isModalPeekOpen, setIsModalPeekOpen] = useState(false); - const [peekOverviewMode, setPeekOverviewMode] = useState("side"); - const [deleteIssueModal, setDeleteIssueModal] = useState(false); +export const IssuePeekOverview: React.FC = observer((props) => { + const { handleMutation, projectId, readOnly, workspaceSlug } = props; + // states + const [isSidePeekOpen, setIsSidePeekOpen] = useState(false); + const [isModalPeekOpen, setIsModalPeekOpen] = useState(false); + const [peekOverviewMode, setPeekOverviewMode] = useState("side"); + const [deleteIssueModal, setDeleteIssueModal] = useState(false); - const router = useRouter(); - const { peekIssue } = router.query; + const router = useRouter(); + const { peekIssue } = router.query; - const { issues: issuesStore } = useMobxStore(); - const { deleteIssue, getIssueById, issues, updateIssue } = issuesStore; + const { issues: issuesStore } = useMobxStore(); + const { deleteIssue, getIssueById, issues, updateIssue } = issuesStore; - const issue = issues[peekIssue?.toString() ?? ""]; + const issue = issues[peekIssue?.toString() ?? ""]; - const { user } = useUser(); + const { user } = useUser(); - const handleClose = () => { - const { query } = router; - delete query.peekIssue; + const handleClose = () => { + const { query } = router; + delete query.peekIssue; - router.push({ - pathname: router.pathname, - query: { ...query }, - }); - }; + router.push({ + pathname: router.pathname, + query: { ...query }, + }); + }; - const handleUpdateIssue = async (formData: Partial) => { - if (!issue || !user) return; + const handleUpdateIssue = async (formData: Partial) => { + if (!issue || !user) return; - await updateIssue(workspaceSlug, projectId, issue.id, formData, user); - mutate(PROJECT_ISSUES_ACTIVITY(issue.id)); - handleMutation(); - }; + await updateIssue(workspaceSlug, projectId, issue.id, formData, user); + mutate(PROJECT_ISSUES_ACTIVITY(issue.id)); + handleMutation(); + }; - const handleDeleteIssue = async () => { - if (!issue || !user) return; + const handleDeleteIssue = async () => { + if (!issue || !user) return; + await deleteIssue(workspaceSlug, projectId, issue.id, user); + handleMutation(); + handleClose(); + }; - await deleteIssue(workspaceSlug, projectId, issue.id, user); - handleMutation(); + useEffect(() => { + if (!peekIssue) return; - handleClose(); - }; + getIssueById(workspaceSlug, projectId, peekIssue.toString()); + }, [getIssueById, peekIssue, projectId, workspaceSlug]); - useEffect(() => { - if (!peekIssue) return; - - getIssueById(workspaceSlug, projectId, peekIssue.toString()); - }, [getIssueById, peekIssue, projectId, workspaceSlug]); - - useEffect(() => { - if (peekIssue) { - if (peekOverviewMode === "side") { - setIsSidePeekOpen(true); - setIsModalPeekOpen(false); - } else { - setIsModalPeekOpen(true); - setIsSidePeekOpen(false); - } - } else { - setIsSidePeekOpen(false); + useEffect(() => { + if (peekIssue) { + if (peekOverviewMode === "side") { + setIsSidePeekOpen(true); setIsModalPeekOpen(false); + } else { + setIsModalPeekOpen(true); + setIsSidePeekOpen(false); } - }, [peekIssue, peekOverviewMode]); + } else { + setIsSidePeekOpen(false); + setIsModalPeekOpen(false); + } + }, [peekIssue, peekOverviewMode]); - return ( - <> - setDeleteIssueModal(false)} - data={issue ? { ...issue } : null} - onSubmit={handleDeleteIssue} - user={user} - /> - - -
- - - setDeleteIssueModal(true)} - handleUpdateIssue={handleUpdateIssue} - issue={issue} - mode={peekOverviewMode} - readOnly={readOnly} - setMode={(mode) => setPeekOverviewMode(mode)} - workspaceSlug={workspaceSlug} - /> - - -
-
-
- - + return ( + <> + setDeleteIssueModal(false)} + data={issue ? { ...issue } : null} + onSubmit={handleDeleteIssue} + user={user} + /> + + +
+ + + setDeleteIssueModal(true)} + handleUpdateIssue={handleUpdateIssue} + issue={issue} + mode={peekOverviewMode} + readOnly={readOnly} + setMode={(mode) => setPeekOverviewMode(mode)} + workspaceSlug={workspaceSlug} + /> + + +
+
+
+ + + +
+ +
= observer( leaveFrom="opacity-100" leaveTo="opacity-0" > -
+ +
+ {peekOverviewMode === "modal" && ( + setDeleteIssueModal(true)} + handleUpdateIssue={handleUpdateIssue} + issue={issue} + mode={peekOverviewMode} + readOnly={readOnly} + setMode={(mode) => setPeekOverviewMode(mode)} + workspaceSlug={workspaceSlug} + /> + )} + {peekOverviewMode === "full" && ( + setDeleteIssueModal(true)} + handleUpdateIssue={handleUpdateIssue} + issue={issue} + mode={peekOverviewMode} + readOnly={readOnly} + setMode={(mode) => setPeekOverviewMode(mode)} + workspaceSlug={workspaceSlug} + /> + )} +
+
-
- - -
- {peekOverviewMode === "modal" && ( - setDeleteIssueModal(true)} - handleUpdateIssue={handleUpdateIssue} - issue={issue} - mode={peekOverviewMode} - readOnly={readOnly} - setMode={(mode) => setPeekOverviewMode(mode)} - workspaceSlug={workspaceSlug} - /> - )} - {peekOverviewMode === "full" && ( - setDeleteIssueModal(true)} - handleUpdateIssue={handleUpdateIssue} - issue={issue} - mode={peekOverviewMode} - readOnly={readOnly} - setMode={(mode) => setPeekOverviewMode(mode)} - workspaceSlug={workspaceSlug} - /> - )} -
-
-
-
-
-
- - ); - } -); + +
+
+ + ); +}); diff --git a/web/hooks/use-user.tsx b/web/hooks/use-user.tsx index e2585ea5e..b3f999fdb 100644 --- a/web/hooks/use-user.tsx +++ b/web/hooks/use-user.tsx @@ -1,6 +1,5 @@ import { useEffect } from "react"; import { useRouter } from "next/router"; -import { usePostHog } from "posthog-js/react"; import useSWR from "swr"; // services import userService from "services/user.service"; @@ -11,7 +10,6 @@ import type { ICurrentUserResponse } from "types"; export default function useUser({ redirectTo = "", redirectIfFound = false, options = {} } = {}) { const router = useRouter(); - const posthog = usePostHog(); // API to fetch user information const { data, isLoading, error, mutate } = useSWR( CURRENT_USER, diff --git a/web/services/issues.service.ts b/web/services/issues.service.ts index c3108f62a..efac68e7c 100644 --- a/web/services/issues.service.ts +++ b/web/services/issues.service.ts @@ -11,6 +11,10 @@ import type { ISubIssueResponse, } from "types"; import { API_BASE_URL } from "helpers/common.helper"; +import PosthogService from "./posthog.service"; +import { ISSUE_CREATE } from "constants/posthog-events"; + +const posthogService = new PosthogService(); class ProjectIssuesServices extends APIService { constructor() { @@ -26,6 +30,7 @@ class ProjectIssuesServices extends APIService { return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/issues/`, data) .then((response) => { trackEventServices.trackIssueEvent(response.data, "ISSUE_CREATE", user); + posthogService.capture(ISSUE_CREATE, response.data, user); return response?.data; }) .catch((error) => { diff --git a/web/services/posthog.service.ts b/web/services/posthog.service.ts new file mode 100644 index 000000000..526c979fe --- /dev/null +++ b/web/services/posthog.service.ts @@ -0,0 +1,27 @@ +import APIService from "./api.service"; + +class PosthogService extends APIService { + constructor() { + super(""); + } + capture(event: string, data: any = {}, user: any = {}) { + this.request({ + method: "post", + url: `${process.env.NEXT_PUBLIC_POSTHOG_HOST || ""}/capture/`, + headers: { + "Content-Type": "application/json", + }, + data: { + api_key: process.env.NEXT_PUBLIC_POSTHOG_KEY, + properties: { + user, + ...data, + }, + distinct_id: user?.email, + event, + }, + }); + } +} + +export default PosthogService; diff --git a/web/store/issues.ts b/web/store/issues.ts index 5400a1d8c..b455fbc99 100644 --- a/web/store/issues.ts +++ b/web/store/issues.ts @@ -1,7 +1,4 @@ -// mobx -import { ISSUE_CREATE } from "constants/posthog-events"; import { action, observable, runInAction, makeAutoObservable } from "mobx"; -import { usePostHog } from "posthog-js/react"; // services import issueService from "services/issues.service"; // types