Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat(analytics): attach companyId to PostHog events in ERP and MES

Raula70904dc4321
diff --git a/apps/erp/app/routes/x+/_layout.tsx b/apps/erp/app/routes/x+/_layout.tsx
index e09362bec2..4d17814696 100644
--- a/apps/erp/app/routes/x+/_layout.tsx
+++ b/apps/erp/app/routes/x+/_layout.tsx
@@ -258,8 +258,14 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
export default function AuthenticatedRoute() {
- const { session, user, companySettings, openClockEntry, printerRoutes } =
- useLoaderData<typeof loader>();
+ const {
+ company,
+ session,
+ user,
+ companySettings,
+ openClockEntry,
+ printerRoutes
+ } = useLoaderData<typeof loader>();
const navigate = useNavigate();
const { isOpen, training, dismiss } = useTrainingPanel();
@@ -283,6 +289,15 @@ export default function AuthenticatedRoute() {
email: user.email,
name: `${user.firstName} ${user.lastName}`
});
+
+ if (!company?.companyId) return;
+
+ // Adoption is measured per customer, and a user can belong to more than one
+ // company — so the company rides on the events rather than on the person.
+ // register() puts companyId on every event including autocapture; group()
+ // is what lets PostHog aggregate by customer.
+ posthog.register({ companyId: company.companyId });
+ posthog.group("company", company.companyId, { name: company.name });
});
return (
diff --git a/apps/mes/app/routes/x+/_layout.tsx b/apps/mes/app/routes/x+/_layout.tsx
index a96cbc64c4..0c4d693147 100644
--- a/apps/mes/app/routes/x+/_layout.tsx
+++ b/apps/mes/app/routes/x+/_layout.tsx
@@ -253,10 +253,21 @@ export default function AuthenticatedRoute() {
});
useMount(() => {
- posthog.identify(user?.id, {
- email: user?.email,
- name: `${user?.firstName} ${user?.lastName}`
+ if (!user) return;
+
+ posthog.identify(user.id, {
+ email: user.email,
+ name: `${user.firstName} ${user.lastName}`
});
+
+ if (!company?.companyId) return;
+
+ // Adoption is measured per customer, and a user can belong to more than one
+ // company — so the company rides on the events rather than on the person.
+ // register() puts companyId on every event including autocapture; group()
+ // is what lets PostHog aggregate by customer.
+ posthog.register({ companyId: company.companyId });
+ posthog.group("company", company.companyId, { name: company.name });
});
// Scroll stays unlocked until lg, where the controls dock beside the content