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.

Add Posthog for client side events (#106)

crudmachine15018660046c
diff --git a/.env.example b/.env.example
index b57eb137e5..71f54842f8 100644
--- a/.env.example
+++ b/.env.example
@@ -1,3 +1,6 @@
+POSTHOG_API_HOST="https://us.posthog.com"
+POSTHOG_PROJECT_PUBLIC_KEY="********************"
+
SESSION_SECRET="************************"
SUPABASE_ANON_PUBLIC="******************"
diff --git a/apps/carbon/app/config/env.ts b/apps/carbon/app/config/env.ts
index 5f3fb4501b..13918301ef 100644
--- a/apps/carbon/app/config/env.ts
+++ b/apps/carbon/app/config/env.ts
@@ -5,6 +5,8 @@ declare global {
env: {
SUPABASE_URL: string;
SUPABASE_ANON_PUBLIC: string;
+ POSTHOG_API_HOST: string;
+ POSTHOG_PROJECT_PUBLIC_KEY: string;
};
}
}
@@ -12,13 +14,15 @@ declare global {
declare global {
namespace NodeJS {
interface ProcessEnv {
- SUPABASE_URL: string;
- SUPABASE_SERVICE_ROLE: string;
- VERCEL_URL: string;
+ POSTHOG_API_HOST: string;
+ POSTHOG_PROJECT_PUBLIC_KEY: string;
SUPABASE_ANON_PUBLIC: string;
SESSION_SECRET: string;
SESSION_KEY: string;
SESSION_ERROR_KEY: string;
+ SUPABASE_URL: string;
+ SUPABASE_SERVICE_ROLE: string;
+ VERCEL_URL: string;
}
}
}
@@ -63,6 +67,12 @@ export const NODE_ENV = getEnv("NODE_ENV", {
isSecret: false,
isRequired: false,
});
+export const POSTHOG_API_HOST = getEnv("POSTHOG_API_HOST", {
+ isSecret: false,
+});
+export const POSTHOG_PROJECT_PUBLIC_KEY = getEnv("POSTHOG_PROJECT_PUBLIC_KEY", {
+ isSecret: false,
+});
export const SUPABASE_API_URL = getEnv("SUPABASE_API_URL", { isSecret: false });
export const SUPABASE_ANON_PUBLIC = getEnv("SUPABASE_ANON_PUBLIC", {
isSecret: false,
@@ -72,6 +82,8 @@ export function getBrowserEnv() {
return {
SUPABASE_API_URL,
SUPABASE_ANON_PUBLIC,
+ POSTHOG_API_HOST,
+ POSTHOG_PROJECT_PUBLIC_KEY,
};
}
diff --git a/apps/carbon/app/entry.client.tsx b/apps/carbon/app/entry.client.tsx
index 0b42fdafdd..b7481b01e2 100644
--- a/apps/carbon/app/entry.client.tsx
+++ b/apps/carbon/app/entry.client.tsx
@@ -1,13 +1,37 @@
import { RemixBrowser } from "@remix-run/react";
+import { startTransition, StrictMode, useEffect } from "react";
import { hydrateRoot } from "react-dom/client";
+import posthog from "posthog-js";
+import { POSTHOG_API_HOST, POSTHOG_PROJECT_PUBLIC_KEY } from "~/config/env";
-hydrateRoot(
- document,
- // <OperatingSystemContextProvider
- // platform={window.navigator.userAgent.includes("Mac") ? "mac" : "windows"}
- // >
- // <LocaleContextProvider locales={window.navigator.languages as string[]}>
- <RemixBrowser />
- // </LocaleContextProvider>
- // </OperatingSystemContextProvider>
-);
+function PosthogInit() {
+ useEffect(() => {
+ posthog.init(POSTHOG_PROJECT_PUBLIC_KEY, {
+ api_host: POSTHOG_API_HOST,
+ autocapture: false,
+ capture_pageview: false,
+ });
+ }, []);
+ return null;
+}
+
+//hydrateRoot(
+//document,
+// <OperatingSystemContextProvider
+// platform={window.navigator.userAgent.includes("Mac") ? "mac" : "windows"}
+// >
+// <LocaleContextProvider locales={window.navigator.languages as string[]}>
+//<RemixBrowser />
+// </LocaleContextProvider>
+// </OperatingSystemContextProvider>
+//);
+
+startTransition(() => {
+ hydrateRoot(
+ document,
+ <StrictMode>
+ <RemixBrowser />
+ <PosthogInit />
+ </StrictMode>
+ );
+});
diff --git a/apps/carbon/app/root.tsx b/apps/carbon/app/root.tsx
index 89ebeee062..521e542d49 100644
--- a/apps/carbon/app/root.tsx
+++ b/apps/carbon/app/root.tsx
@@ -52,9 +52,16 @@ export const meta: MetaFunction = () => {
};
export async function loader({ request }: LoaderFunctionArgs) {
- const { SUPABASE_API_URL, SUPABASE_ANON_PUBLIC } = getBrowserEnv();
+ const {
+ POSTHOG_API_HOST,
+ POSTHOG_PROJECT_PUBLIC_KEY,
+ SUPABASE_API_URL,
+ SUPABASE_ANON_PUBLIC,
+ } = getBrowserEnv();
return json({
env: {
+ POSTHOG_API_HOST,
+ POSTHOG_PROJECT_PUBLIC_KEY,
SUPABASE_API_URL,
SUPABASE_ANON_PUBLIC,
},
diff --git a/apps/carbon/app/routes/_public+/login.tsx b/apps/carbon/app/routes/_public+/login.tsx
index 57bbc81706..8355901cb0 100644
--- a/apps/carbon/app/routes/_public+/login.tsx
+++ b/apps/carbon/app/routes/_public+/login.tsx
@@ -4,6 +4,7 @@ import {
validationError,
validator,
} from "@carbon/remix-validated-form";
+import posthog from "posthog-js";
import type {
ActionFunctionArgs,
LoaderFunctionArgs,
@@ -72,6 +73,10 @@ export default function LoginRoute() {
const [searchParams] = useSearchParams();
const redirectTo = searchParams.get("redirectTo") ?? undefined;
+ const handleClick = () => {
+ posthog.capture("sign_in_clicked");
+ };
+
return (
<>
<img
@@ -102,7 +107,7 @@ export default function LoginRoute() {
<Input name="email" label="Email" />
<Password name="password" label="Password" type="password" />
<Hidden name="redirectTo" value={redirectTo} type="hidden" />
- <Submit size="lg" className="w-full">
+ <Submit size="lg" className="w-full" onClick={handleClick}>
Sign In
</Submit>
<Button variant="link" asChild className="w-full">
diff --git a/apps/carbon/package.json b/apps/carbon/package.json
index ff592ceac4..ccd8f7d6e5 100644
--- a/apps/carbon/package.json
+++ b/apps/carbon/package.json
@@ -52,6 +52,7 @@
"nanostores": "^0.9.3",
"nprogress": "0.2.0",
"performant-array-to-tree": "1.11.0",
+ "posthog-js": "^1.116.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.12.0",
diff --git a/package-lock.json b/package-lock.json
index 0f371a29c3..bd62d1f10f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -81,6 +81,7 @@
"nanostores": "^0.9.3",
"nprogress": "0.2.0",
"performant-array-to-tree": "1.11.0",
+ "posthog-js": "^1.116.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.12.0",
@@ -14899,6 +14900,11 @@
"node": "^12.20 || >= 14.13"
}
},
+ "node_modules/fflate": {
+ "version": "0.4.8",
+ "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.4.8.tgz",
+ "integrity": "sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA=="
+ },
"node_modules/figures": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz",
@@ -24617,6 +24623,15 @@
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
}
},
+ "node_modules/posthog-js": {
+ "version": "1.116.6",
+ "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.116.6.tgz",
+ "integrity": "sha512-rvt8HxzJD4c2B/xsUa4jle8ApdqljeBI2Qqjp4XJMohQf18DXRyM6b96H5/UMs8jxYuZG14Er0h/kEIWeU6Fmw==",
+ "dependencies": {
+ "fflate": "^0.4.8",
+ "preact": "^10.19.3"
+ }
+ },
"node_modules/posthog-node": {
"version": "3.6.3",
"resolved": "https://registry.npmjs.org/posthog-node/-/posthog-node-3.6.3.tgz",
@@ -24629,6 +24644,15 @@
"node": ">=15.0.0"
}
},
+ "node_modules/preact": {
+ "version": "10.20.1",
+ "resolved": "https://registry.npmjs.org/preact/-/preact-10.20.1.tgz",
+ "integrity": "sha512-JIFjgFg9B2qnOoGiYMVBtrcFxHqn+dNXbq76bVmcaHYJFYR4lW67AOcXgAYQQTDYXDOg/kTZrKPNCdRgJ2UJmw==",
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/preact"
+ }
+ },
"node_modules/prelude-ls": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",