diff --git a/apps/app/services/track-event.service.ts b/apps/app/services/track-event.service.ts index a0f50afac..73d79483d 100644 --- a/apps/app/services/track-event.service.ts +++ b/apps/app/services/track-event.service.ts @@ -14,6 +14,7 @@ import type { IPageBlock, IProject, IState, + IView, IWorkspace, } from "types"; @@ -37,6 +38,8 @@ type ModuleEventType = "MODULE_CREATE" | "MODULE_UPDATE" | "MODULE_DELETE"; type PagesEventType = "PAGE_CREATE" | "PAGE_UPDATE" | "PAGE_DELETE"; +type ViewEventType = "VIEW_CREATE" | "VIEW_UPDATE" | "VIEW_DELETE"; + type PageBlocksEventType = | "PAGE_BLOCK_CREATE" | "PAGE_BLOCK_UPDATE" @@ -365,6 +368,30 @@ class TrackEventServices extends APIService { }, }); } + + async trackViewEvent(data: IView, eventName: ViewEventType): Promise { + let payload: any; + if (eventName === "VIEW_DELETE") payload = data; + else + payload = { + labels: Boolean(data.query_data.labels), + assignees: Boolean(data.query_data.assignees), + priority: Boolean(data.query_data.priority), + state: Boolean(data.query_data.state), + created_by: Boolean(data.query_data.created_by), + }; + + return this.request({ + url: "/api/track-event", + method: "POST", + data: { + eventName, + extra: { + ...payload, + }, + }, + }); + } } const trackEventServices = new TrackEventServices(); diff --git a/apps/app/services/views.service.ts b/apps/app/services/views.service.ts index 80699d994..af2426a7c 100644 --- a/apps/app/services/views.service.ts +++ b/apps/app/services/views.service.ts @@ -1,10 +1,15 @@ // services import APIService from "services/api.service"; +import trackEventServices from "services/track-event.service"; + // types import { IView } from "types/views"; const { NEXT_PUBLIC_API_BASE_URL } = process.env; +const trackEvent = + process.env.NEXT_PUBLIC_TRACK_EVENTS === "true" || process.env.NEXT_PUBLIC_TRACK_EVENTS === "1"; + class ViewServices extends APIService { constructor() { super(NEXT_PUBLIC_API_BASE_URL || "http://localhost:8000"); @@ -12,7 +17,10 @@ class ViewServices extends APIService { async createView(workspaceSlug: string, projectId: string, data: IView): Promise { return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/views/`, data) - .then((response) => response?.data) + .then((response) => { + if (trackEvent) trackEventServices.trackViewEvent(response?.data, "VIEW_CREATE"); + return response?.data; + }) .catch((error) => { throw error?.response?.data; }); @@ -25,7 +33,10 @@ class ViewServices extends APIService { data: IView ): Promise { return this.put(`/api/workspaces/${workspaceSlug}/projects/${projectId}/views/${viewId}/`, data) - .then((response) => response?.data) + .then((response) => { + if (trackEvent) trackEventServices.trackViewEvent(response?.data, "VIEW_UPDATE"); + return response?.data; + }) .catch((error) => { throw error?.response?.data; }); @@ -41,7 +52,10 @@ class ViewServices extends APIService { `/api/workspaces/${workspaceSlug}/projects/${projectId}/views/${viewId}/`, data ) - .then((response) => response?.data) + .then((response) => { + if (trackEvent) trackEventServices.trackViewEvent(response?.data, "VIEW_UPDATE"); + return response?.data; + }) .catch((error) => { throw error?.response?.data; }); @@ -49,7 +63,10 @@ class ViewServices extends APIService { async deleteView(workspaceSlug: string, projectId: string, viewId: string): Promise { return this.delete(`/api/workspaces/${workspaceSlug}/projects/${projectId}/views/${viewId}/`) - .then((response) => response?.data) + .then((response) => { + if (trackEvent) trackEventServices.trackViewEvent(response?.data, "VIEW_DELETE"); + return response?.data; + }) .catch((error) => { throw error?.response?.data; }); @@ -111,7 +128,6 @@ class ViewServices extends APIService { throw error?.response?.data; }); } - } export default new ViewServices();