2024-06-20 13:58:42 +05:30
|
|
|
"use client";
|
|
|
|
|
|
2024-06-10 12:37:38 +05:30
|
|
|
import { FC, ReactNode, useEffect } from "react";
|
|
|
|
|
import { observer } from "mobx-react";
|
2024-06-20 16:52:05 +05:30
|
|
|
import dynamic from "next/dynamic";
|
2024-09-11 17:10:15 +05:30
|
|
|
import { useParams } from "next/navigation";
|
2023-11-20 13:29:54 +05:30
|
|
|
import posthog from "posthog-js";
|
2024-02-05 13:19:07 +05:30
|
|
|
import { PostHogProvider as PHProvider } from "posthog-js/react";
|
2024-02-09 16:22:08 +05:30
|
|
|
// constants
|
2025-06-27 19:01:00 +05:30
|
|
|
import { GROUP_WORKSPACE_TRACKER_EVENT } from "@plane/constants";
|
2024-03-06 18:39:14 +05:30
|
|
|
// helpers
|
2025-06-16 17:18:41 +05:30
|
|
|
import { getUserRole } from "@plane/utils";
|
2024-06-10 12:37:38 +05:30
|
|
|
// hooks
|
2025-07-02 15:23:18 +05:30
|
|
|
import { captureClick, joinEventGroup } from "@/helpers/event-tracker.helper";
|
2025-08-19 07:36:42 -07:00
|
|
|
import { useInstance } from "@/hooks/store/use-instance";
|
2025-08-18 08:57:16 -07:00
|
|
|
import { useWorkspace } from "@/hooks/store/use-workspace";
|
|
|
|
|
import { useUser, useUserPermissions } from "@/hooks/store/user";
|
2024-06-20 16:52:05 +05:30
|
|
|
// dynamic imports
|
|
|
|
|
const PostHogPageView = dynamic(() => import("@/lib/posthog-view"), { ssr: false });
|
2023-11-20 13:29:54 +05:30
|
|
|
|
|
|
|
|
export interface IPosthogWrapper {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 12:37:38 +05:30
|
|
|
const PostHogProvider: FC<IPosthogWrapper> = observer((props) => {
|
|
|
|
|
const { children } = props;
|
2024-09-11 17:10:15 +05:30
|
|
|
const { data: user } = useUser();
|
2024-06-10 12:37:38 +05:30
|
|
|
const { currentWorkspace } = useWorkspace();
|
2024-06-20 13:58:42 +05:30
|
|
|
const { instance } = useInstance();
|
2024-09-11 17:10:15 +05:30
|
|
|
const { workspaceSlug, projectId } = useParams();
|
2025-05-30 19:57:07 +05:30
|
|
|
const { getWorkspaceRoleByWorkspaceSlug, getProjectRoleByWorkspaceSlugAndProjectId } = useUserPermissions();
|
2024-06-20 13:58:42 +05:30
|
|
|
|
2025-05-30 19:57:07 +05:30
|
|
|
const currentProjectRole = getProjectRoleByWorkspaceSlugAndProjectId(
|
|
|
|
|
workspaceSlug?.toString(),
|
|
|
|
|
projectId?.toString()
|
|
|
|
|
);
|
|
|
|
|
const currentWorkspaceRole = getWorkspaceRoleByWorkspaceSlug(workspaceSlug?.toString());
|
2024-06-20 13:58:42 +05:30
|
|
|
const is_telemetry_enabled = instance?.is_telemetry_enabled || false;
|
2025-07-02 15:23:18 +05:30
|
|
|
const is_posthog_enabled =
|
|
|
|
|
process.env.NEXT_PUBLIC_POSTHOG_KEY && process.env.NEXT_PUBLIC_POSTHOG_HOST && is_telemetry_enabled;
|
2023-11-20 13:29:54 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (user) {
|
|
|
|
|
// Identify sends an event, so you want may want to limit how often you call it
|
|
|
|
|
posthog?.identify(user.email, {
|
2024-02-09 16:22:08 +05:30
|
|
|
id: user.id,
|
2023-11-20 13:29:54 +05:30
|
|
|
first_name: user.first_name,
|
|
|
|
|
last_name: user.last_name,
|
2024-02-09 16:22:08 +05:30
|
|
|
email: user.email,
|
2024-06-10 12:37:38 +05:30
|
|
|
workspace_role: currentWorkspaceRole ? getUserRole(currentWorkspaceRole) : undefined,
|
|
|
|
|
project_role: currentProjectRole ? getUserRole(currentProjectRole) : undefined,
|
2023-11-20 13:29:54 +05:30
|
|
|
});
|
2024-06-20 13:58:42 +05:30
|
|
|
if (currentWorkspace) {
|
2025-07-02 15:23:18 +05:30
|
|
|
joinEventGroup(GROUP_WORKSPACE_TRACKER_EVENT, currentWorkspace?.id, {
|
|
|
|
|
date: new Date().toDateString(),
|
|
|
|
|
workspace_id: currentWorkspace?.id,
|
|
|
|
|
});
|
2024-06-20 13:58:42 +05:30
|
|
|
}
|
2023-11-20 13:29:54 +05:30
|
|
|
}
|
2024-06-20 13:58:42 +05:30
|
|
|
}, [user, currentProjectRole, currentWorkspaceRole, currentWorkspace]);
|
2023-11-20 13:29:54 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-28 13:08:37 +05:30
|
|
|
if (process.env.NEXT_PUBLIC_POSTHOG_KEY && process.env.NEXT_PUBLIC_POSTHOG_HOST) {
|
|
|
|
|
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
|
|
|
|
api_host: "/ingest",
|
|
|
|
|
ui_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
|
2024-03-06 14:25:15 +05:30
|
|
|
debug: process.env.NEXT_PUBLIC_POSTHOG_DEBUG === "1", // Debug mode based on the environment variable
|
2023-11-20 13:29:54 +05:30
|
|
|
autocapture: false,
|
|
|
|
|
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
|
2024-06-20 13:58:42 +05:30
|
|
|
capture_pageleave: true,
|
2024-06-26 14:28:15 +05:30
|
|
|
disable_session_recording: true,
|
2023-11-20 13:29:54 +05:30
|
|
|
});
|
|
|
|
|
}
|
2024-05-28 13:08:37 +05:30
|
|
|
}, []);
|
2023-11-20 13:29:54 +05:30
|
|
|
|
2025-07-02 15:23:18 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
const clickHandler = (event: MouseEvent) => {
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
// Use closest to find the nearest parent element with data-ph-element attribute
|
|
|
|
|
const elementWithAttribute = target.closest("[data-ph-element]") as HTMLElement;
|
|
|
|
|
if (elementWithAttribute) {
|
|
|
|
|
const element = elementWithAttribute.getAttribute("data-ph-element");
|
|
|
|
|
if (element) {
|
|
|
|
|
captureClick({ elementName: element });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (is_posthog_enabled) {
|
|
|
|
|
document.addEventListener("click", clickHandler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener("click", clickHandler);
|
|
|
|
|
};
|
|
|
|
|
}, [is_posthog_enabled]);
|
|
|
|
|
|
|
|
|
|
if (is_posthog_enabled)
|
2024-06-20 16:52:05 +05:30
|
|
|
return (
|
|
|
|
|
<PHProvider client={posthog}>
|
|
|
|
|
<PostHogPageView />
|
|
|
|
|
{children}
|
|
|
|
|
</PHProvider>
|
|
|
|
|
);
|
2024-05-28 15:11:54 +05:30
|
|
|
|
2023-11-20 13:29:54 +05:30
|
|
|
return <>{children}</>;
|
2024-06-10 12:37:38 +05:30
|
|
|
});
|
2023-11-20 13:29:54 +05:30
|
|
|
|
2024-02-05 13:19:07 +05:30
|
|
|
export default PostHogProvider;
|