diff --git a/apps/app/services/app-installations.service.ts b/apps/app/services/app-installations.service.ts index 8ee04dcfe..c335ecad3 100644 --- a/apps/app/services/app-installations.service.ts +++ b/apps/app/services/app-installations.service.ts @@ -1,9 +1,13 @@ // services import axios from "axios"; import APIService from "services/api.service"; +import trackEventServices from "services/track-event.service"; 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 AppInstallationsService extends APIService { constructor() { super(NEXT_PUBLIC_API_BASE_URL || "http://localhost:8000"); @@ -11,24 +15,67 @@ class AppInstallationsService extends APIService { async addInstallationApp(workspaceSlug: string, provider: string, data: any): Promise { return this.post(`/api/workspaces/${workspaceSlug}/workspace-integrations/${provider}/`, data) - .then((response) => response?.data) + .then((response) => { + if (!trackEvent) return response?.data; + + const eventType = + provider === "slack" + ? "SLACK_INTEGRATION_CREATE" + : provider === "github" + ? "GITHUB_INTEGRATION_CREATE" + : null; + + if (!eventType) return response?.data; + + trackEventServices.trackAppIntegrationEvent( + { + workspaceSlug, + provider, + }, + eventType + ); + + return response?.data; + }) .catch((error) => { throw error?.response; }); } - async addSlackChannel(workspaceSlug: string, projectId: string, integrationId: string | null | undefined, data: any): Promise { + async addSlackChannel( + workspaceSlug: string, + projectId: string, + integrationId: string | null | undefined, + data: any + ): Promise { return this.post( `/api/workspaces/${workspaceSlug}/projects/${projectId}/workspace-integrations/${integrationId}/project-slack-sync/`, data ) - .then((response) => response?.data) + .then((response) => { + if (!trackEvent) return response?.data; + + trackEventServices.trackAppIntegrationEvent( + { + workspaceSlug, + projectId, + integrationId, + }, + "SLACK_ADD_CHANNEL" + ); + + return response?.data; + }) .catch((error) => { throw error?.response; }); } - async getSlackChannelDetail(workspaceSlug: string, projectId: string, integrationId: string | null | undefined): Promise { + async getSlackChannelDetail( + workspaceSlug: string, + projectId: string, + integrationId: string | null | undefined + ): Promise { return this.get( `/api/workspaces/${workspaceSlug}/projects/${projectId}/workspace-integrations/${integrationId}/project-slack-sync/` ) @@ -38,7 +85,12 @@ class AppInstallationsService extends APIService { }); } - async removeSlackChannel(workspaceSlug: string, projectId: string, integrationId: string | null | undefined, slackSyncId: string | undefined): Promise { + async removeSlackChannel( + workspaceSlug: string, + projectId: string, + integrationId: string | null | undefined, + slackSyncId: string | undefined + ): Promise { return this.delete( `/api/workspaces/${workspaceSlug}/projects/${projectId}/workspace-integrations/${integrationId}/project-slack-sync/${slackSyncId}` ) diff --git a/apps/app/services/track-event.service.ts b/apps/app/services/track-event.service.ts index 54de7119b..78964f1af 100644 --- a/apps/app/services/track-event.service.ts +++ b/apps/app/services/track-event.service.ts @@ -63,7 +63,18 @@ export type MiscellaneousEventType = | "TOGGLE_STATE_ON" | "TOGGLE_STATE_OFF"; -type IntegrationEventType = "ADD_WORKSPACE_INTEGRATION" | "REMOVE_WORKSPACE_INTEGRATION"; +type TProviders = "SLACK" | "GITHUB"; + +type TProviderEvents = "INTEGRATION_CREATE" | "INTEGRATION_DELETE"; + +type TFinalIntegrationEvents = `${TProviders}_${TProviderEvents}`; + +type IntegrationEventType = + | "ADD_WORKSPACE_INTEGRATION" + | "REMOVE_WORKSPACE_INTEGRATION" + | TFinalIntegrationEvents + | "SLACK_ADD_CHANNEL" + | "GITHUB_ADD_REPO"; type GitHubSyncEventType = "GITHUB_REPO_SYNC"; @@ -84,6 +95,7 @@ type ImporterEventType = | "GITHUB_IMPORTER_DELETE" | "JIRA_IMPORTER_CREATE" | "JIRA_IMPORTER_DELETE"; + class TrackEventServices extends APIService { constructor() { super("/");