Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

✨ feat: add posthog

Dillion Vermabf68524bc4eb
diff --git a/app/layout.tsx b/app/layout.tsx
index 1b18bee4..6838fe4d 100644
--- a/app/layout.tsx
+++ b/app/layout.tsx
@@ -1,4 +1,6 @@
import { Analytics } from "@/components/analytics";
+import PosthogIdentify from "@/components/posthog-identify";
+import { PHProvider } from "@/components/posthog-provider";
import SessionProvider from "@/components/session-provider";
import { ThemeProvider } from "@/components/theme-provider";
import { Toaster } from "@/components/ui/toaster";
@@ -29,14 +31,18 @@ export default function RootLayout({
fontSans.className,
)}
>
- <SessionProvider>
- <ThemeProvider attribute="class" defaultTheme="dark" enableSystem>
- {children}
- <Toaster />
- <Analytics />
- {/* <TailwindIndicator /> */}
- </ThemeProvider>
- </SessionProvider>
+ <PHProvider>
+ <SessionProvider>
+ <ThemeProvider attribute="class" defaultTheme="dark" enableSystem>
+ {children}
+ <PosthogIdentify />
+
+ <Toaster />
+ <Analytics />
+ {/* <TailwindIndicator /> */}
+ </ThemeProvider>
+ </SessionProvider>
+ </PHProvider>
</body>
</html>
);
diff --git a/components/posthog-identify.tsx b/components/posthog-identify.tsx
new file mode 100644
index 00000000..ff3b5860
--- /dev/null
+++ b/components/posthog-identify.tsx
@@ -0,0 +1,22 @@
+"use client";
+
+import { useSession } from "next-auth/react";
+import { usePostHog } from "posthog-js/react";
+import { useEffect } from "react";
+
+export default function PosthogIdentify() {
+ const posthog = usePostHog();
+ const { data: session } = useSession();
+
+ useEffect(() => {
+ if (!session) return;
+ if (session.user && posthog) {
+ posthog.identify(session.user.id, {
+ email: session.user.email,
+ name: session.user.name,
+ });
+ }
+ }, [session, posthog]);
+
+ return null;
+}
diff --git a/components/posthog-provider.tsx b/components/posthog-provider.tsx
new file mode 100644
index 00000000..cb3c3ba8
--- /dev/null
+++ b/components/posthog-provider.tsx
@@ -0,0 +1,47 @@
+"use client";
+
+import { usePathname, useSearchParams } from "next/navigation";
+import posthog from "posthog-js";
+import { PostHogProvider } from "posthog-js/react";
+import { useEffect } from "react";
+
+if (
+ typeof window !== "undefined" &&
+ (!window.location.href.includes("automatic.chat/chats/") ||
+ window.location.href.includes("share")) &&
+ process.env.NODE_ENV !== "development"
+) {
+ posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY!, {
+ api_host: "https://app.posthog.com",
+ session_recording: {
+ maskAllInputs: false,
+ },
+ // Enable debug mode in development
+ loaded: (posthog) => {
+ if (process.env.NODE_ENV === "development") posthog.debug();
+ },
+ });
+}
+
+export function PostHogPageview(): JSX.Element {
+ const pathname = usePathname();
+ const searchParams = useSearchParams();
+
+ useEffect(() => {
+ if (pathname) {
+ let url = window.origin + pathname;
+ if (searchParams && searchParams.toString()) {
+ url = url + `?${searchParams.toString()}`;
+ }
+ posthog.capture("$pageview", {
+ $current_url: url,
+ });
+ }
+ }, [pathname, searchParams]);
+
+ return <></>;
+}
+
+export function PHProvider({ children }: { children: React.ReactNode }) {
+ return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
+}
diff --git a/env.mjs b/env.mjs
index 13ebf172..b8343153 100644
--- a/env.mjs
+++ b/env.mjs
@@ -2,6 +2,10 @@ import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";
export const env = createEnv({
+ /*
+ * Serverside Environment variables, not available on the client.
+ * Will throw if you access these variables on the client.
+ */
server: {
// This is optional because it's only used in development.
// See https://next-auth.js.org/deployment.
@@ -22,10 +26,23 @@ export const env = createEnv({
// STRIPE_WEBHOOK_SECRET: z.string().min(1),
// STRIPE_PRO_MONTHLY_PLAN_ID: z.string().min(1),
},
+ /*
+ * Environment variables available on the client (and server).
+ *
+ * 💡 You'll get type errors if these are not prefixed with NEXT_PUBLIC_.
+ */
client: {
NEXT_PUBLIC_APP_URL: z.string().min(1),
NEXT_PUBLIC_STRIPE_PAYMENT_LINK: z.string().min(1),
+ NEXT_PUBLIC_POSTHOG_API_KEY: z.string().optional(),
},
+
+ /*
+ * Due to how Next.js bundles environment variables on Edge and Client,
+ * we need to manually destructure them to make sure all are included in bundle.
+ *
+ * 💡 You'll get type errors if not all variables from `server` & `client` are included here.
+ */
runtimeEnv: {
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
@@ -46,5 +63,7 @@ export const env = createEnv({
NEXT_PUBLIC_APP_URL: process.env.NEXT_PUBLIC_APP_URL,
NEXT_PUBLIC_STRIPE_PAYMENT_LINK:
process.env.NEXT_PUBLIC_STRIPE_PAYMENT_LINK,
+
+ NEXT_PUBLIC_POSTHOG_API_KEY: process.env.NEXT_PUBLIC_POSTHOG_API_KEY,
},
});
diff --git a/package.json b/package.json
index a9a90d2d..77d90b37 100644
--- a/package.json
+++ b/package.json
@@ -79,6 +79,7 @@
"next-themes": "^0.2.1",
"nodemailer": "^6.9.3",
"postcss": "8.4.32",
+ "posthog-js": "^1.96.1",
"react": "18.2.0",
"react-day-picker": "^8.9.1",
"react-dom": "18.2.0",
diff --git a/yarn.lock b/yarn.lock
index d7049968..3727e966 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3682,6 +3682,11 @@ fetch-blob@^3.1.2, fetch-blob@^3.1.4:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"
+fflate@^0.4.1:
+ version "0.4.8"
+ resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae"
+ integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==
+
fflate@^0.7.3:
version "0.7.4"
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.7.4.tgz#61587e5d958fdabb5a9368a302c25363f4f69f50"
@@ -6497,6 +6502,13 @@ [email protected]:
picocolors "^1.0.0"
source-map-js "^1.0.2"
+posthog-js@^1.96.1:
+ version "1.96.1"
+ resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.96.1.tgz#4f9719a24e4e14037b0e72d430194d7cdb576447"
+ integrity sha512-kv1vQqYMt2BV3YHS+wxsbGuP+tz+M3y1AzNhz8TfkpY1HT8W/ONT0i0eQpeRr9Y+d4x/fZ6M4cXG5GMvi9lRCA==
+ dependencies:
+ fflate "^0.4.1"
+
version "5.2.3"
resolved "https://registry.yarnpkg.com/preact-render-to-string/-/preact-render-to-string-5.2.3.tgz#23d17376182af720b1060d5a4099843c7fe92fe4"