2023-11-25 15:56:26 +00:00
|
|
|
import { action, makeObservable, observable } from "mobx";
|
|
|
|
import posthog from "posthog-js";
|
|
|
|
import { RootStore } from "./root";
|
|
|
|
|
|
|
|
export interface ITrackEventStore {
|
|
|
|
trackElement: string;
|
|
|
|
setTrackElement: (element: string) => void;
|
|
|
|
postHogEventTracker: (
|
|
|
|
eventName: string,
|
2023-12-06 11:45:14 +00:00
|
|
|
payload: object | [] | null,
|
|
|
|
group?: { isGrouping: boolean | null; groupType: string | null; gorupId: string | null } | null
|
2023-11-25 15:56:26 +00:00
|
|
|
) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TrackEventStore implements ITrackEventStore {
|
|
|
|
trackElement: string = "";
|
|
|
|
rootStore;
|
|
|
|
constructor(_rootStore: RootStore) {
|
|
|
|
makeObservable(this, {
|
|
|
|
trackElement: observable,
|
|
|
|
setTrackElement: action,
|
|
|
|
postHogEventTracker: action,
|
|
|
|
});
|
|
|
|
this.rootStore = _rootStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTrackElement = (element: string) => {
|
|
|
|
this.trackElement = element;
|
|
|
|
};
|
|
|
|
|
|
|
|
postHogEventTracker = (
|
|
|
|
eventName: string,
|
2023-12-06 11:45:14 +00:00
|
|
|
payload: object | [] | null,
|
|
|
|
group?: { isGrouping: boolean | null; groupType: string | null; gorupId: string | null } | null
|
2023-11-25 15:56:26 +00:00
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
let extras: any = {
|
|
|
|
workspace_name: this.rootStore.workspace.currentWorkspace?.name ?? "",
|
|
|
|
workspace_id: this.rootStore.workspace.currentWorkspace?.id ?? "",
|
|
|
|
workspace_slug: this.rootStore.workspace.currentWorkspace?.slug ?? "",
|
|
|
|
project_name: this.rootStore.project.currentProjectDetails?.name ?? "",
|
|
|
|
project_id: this.rootStore.project.currentProjectDetails?.id ?? "",
|
|
|
|
project_identifier: this.rootStore.project.currentProjectDetails?.identifier ?? "",
|
|
|
|
};
|
2023-12-06 11:45:14 +00:00
|
|
|
if (["PROJECT_CREATED", "PROJECT_UPDATED"].includes(eventName)) {
|
2023-11-25 15:56:26 +00:00
|
|
|
const project_details: any = payload as object;
|
|
|
|
extras = {
|
|
|
|
...extras,
|
|
|
|
project_name: project_details?.name ?? "",
|
|
|
|
project_id: project_details?.id ?? "",
|
|
|
|
project_identifier: project_details?.identifier ?? "",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-12-06 11:45:14 +00:00
|
|
|
if (group && group!.isGrouping === true) {
|
|
|
|
posthog?.group(group!.groupType!, group!.gorupId!, {
|
|
|
|
date: new Date(),
|
|
|
|
workspace_id: group!.gorupId,
|
|
|
|
});
|
|
|
|
posthog?.capture(eventName, {
|
|
|
|
...payload,
|
|
|
|
extras: extras,
|
|
|
|
element: this.trackElement ?? "",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
posthog?.capture(eventName, {
|
|
|
|
...payload,
|
|
|
|
extras: extras,
|
|
|
|
element: this.trackElement ?? "",
|
|
|
|
});
|
|
|
|
}
|
2023-11-25 15:56:26 +00:00
|
|
|
} catch (error) {
|
2023-12-07 12:43:27 +00:00
|
|
|
throw error;
|
2023-11-25 15:56:26 +00:00
|
|
|
}
|
|
|
|
this.setTrackElement("");
|
|
|
|
};
|
|
|
|
}
|