plane/web/lib/posthog-provider.tsx
Lakhan Baheti 0165abab3e
chore: posthog events improved (#3554)
* chore: events naming convention changed

* chore: track element added for project related events

* chore: track element added for cycle related events

* chore: track element added for module related events

* chore: issue related events updated

* refactor: event tracker store

* refactor: event-tracker store

* fix: posthog changes

---------

Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
2024-02-05 13:19:07 +05:30

68 lines
2.0 KiB
TypeScript

import { FC, ReactNode, useEffect } from "react";
import { useRouter } from "next/router";
import posthog from "posthog-js";
import { PostHogProvider as PHProvider } from "posthog-js/react";
// mobx store provider
import { IUser } from "@plane/types";
// helpers
import { getUserRole } from "helpers/user.helper";
export interface IPosthogWrapper {
children: ReactNode;
user: IUser | null;
workspaceRole: number | undefined;
projectRole: number | undefined;
posthogAPIKey: string | null;
posthogHost: string | null;
}
const PostHogProvider: FC<IPosthogWrapper> = (props) => {
const { children, user, workspaceRole, projectRole, posthogAPIKey, posthogHost } = props;
// router
const router = useRouter();
useEffect(() => {
if (user) {
// Identify sends an event, so you want may want to limit how often you call it
posthog?.identify(user.email, {
email: user.email,
first_name: user.first_name,
last_name: user.last_name,
id: user.id,
workspace_role: workspaceRole ? getUserRole(workspaceRole) : undefined,
project_role: projectRole ? getUserRole(projectRole) : undefined,
});
}
}, [user, workspaceRole, projectRole]);
useEffect(() => {
if (posthogAPIKey && posthogHost) {
posthog.init(posthogAPIKey, {
api_host: posthogHost || "https://app.posthog.com",
autocapture: false,
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
});
}
}, [posthogAPIKey, posthogHost]);
useEffect(() => {
// Track page views
const handleRouteChange = () => {
posthog?.capture("$pageview");
};
router.events.on("routeChangeComplete", handleRouteChange);
return () => {
router.events.off("routeChangeComplete", handleRouteChange);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (posthogAPIKey) {
return <PHProvider client={posthog}>{children}</PHProvider>;
}
return <>{children}</>;
};
export default PostHogProvider;