Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add posthog

Marc Seitz60912ca8478c
diff --git a/components/providers/posthog-provider.tsx b/components/providers/posthog-provider.tsx
new file mode 100644
index 00000000..a5fcf409
--- /dev/null
+++ b/components/providers/posthog-provider.tsx
@@ -0,0 +1,54 @@
+import { getPostHogConfig } from "@/lib/posthog";
+import { CustomUser } from "@/lib/types";
+import { getSession } from "next-auth/react";
+import posthog from "posthog-js";
+// import { useEffect } from "react";
+// import { useRouter } from "next/router";
+import { PostHogProvider } from "posthog-js/react";
+
+export const PostHogCustomProvider = ({
+ children,
+}: {
+ children: React.ReactNode;
+}) => {
+ const posthogConfig = getPostHogConfig();
+ // const router = useRouter();
+
+ // Check that PostHog is client-side
+ if (typeof window !== "undefined" && posthogConfig) {
+ posthog.init(posthogConfig.key, {
+ api_host: posthogConfig.host,
+ disable_session_recording: true,
+ // Enable debug mode in development
+ loaded: (posthog) => {
+ if (process.env.NODE_ENV === "development") posthog.debug();
+ getSession()
+ .then((session) => {
+ if (session) {
+ posthog.identify(
+ (session.user as CustomUser).email ??
+ (session.user as CustomUser).id,
+ );
+ } else {
+ posthog.reset();
+ }
+ })
+ .catch(() => {
+ // Do nothing.
+ });
+ },
+ });
+ }
+
+ // useEffect(() => {
+ // // Track page views
+ // const handleRouteChange = () => posthog?.capture("$pageview");
+ // router.events.on("routeChangeComplete", handleRouteChange);
+
+ // return () => {
+ // router.events.off("routeChangeComplete", handleRouteChange);
+ // };
+ // }, []);
+
+ return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
+};
diff --git a/lib/posthog.ts b/lib/posthog.ts
new file mode 100644
index 00000000..abc69aad
--- /dev/null
+++ b/lib/posthog.ts
@@ -0,0 +1,13 @@
+export function getPostHogConfig(): { key: string; host: string } | null {
+ const postHogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY;
+ const postHogHost = `${process.env.NEXT_PUBLIC_BASE_URL}/ingest`;
+
+ if (!postHogKey || !postHogHost) {
+ return null;
+ }
+
+ return {
+ key: postHogKey,
+ host: postHogHost,
+ };
+}
diff --git a/middleware.ts b/middleware.ts
index 36ad3d9b..30a01912 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -8,12 +8,12 @@ export const config = {
* Match all paths except for:
* 1. /api/ routes
* 2. /_next/ (Next.js internals)
- * 3. /_proxy/, /_auth/ (special pages for OG tags proxying and password protection)
- * 4. /_static (inside /public)
- * 5. /_vercel (Vercel internals)
- * 6. /favicon.ico, /sitemap.xml (static files)
+ * 3. /_static (inside /public)
+ * 4. /_vercel (Vercel internals)
+ * 5. /favicon.ico, /sitemap.xml (static files)
+ * 6. ingest (analytics)
*/
- "/((?!api/|_next/|_proxy/|_auth/|_static|_vercel|favicon.ico|sitemap.xml).*)",
+ "/((?!api/|_next/|_static|_vercel|ingest|favicon.ico|sitemap.xml).*)",
],
};
diff --git a/next.config.js b/next.config.js
index 811b65d9..1ae99e19 100644
--- a/next.config.js
+++ b/next.config.js
@@ -62,6 +62,14 @@ const nextConfig = {
},
],
},
+ async rewrites() {
+ return [
+ {
+ source: "/ingest/:path*",
+ destination: "https://eu.posthog.com/:path*",
+ },
+ ];
+ },
experimental: {
outputFileTracingIncludes: {
"/api/mupdf/*": ["./node_modules/mupdf/lib/*.wasm"],
diff --git a/package-lock.json b/package-lock.json
index 41565e90..857db094 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -83,6 +83,7 @@
"notion-utils": "^6.16.0",
"openai": "^4.20.0",
"postcss": "^8.4.35",
+ "posthog-js": "^1.110.0",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dom": "^18.2.0",
@@ -8545,6 +8546,11 @@
"reusify": "^1.0.4"
}
},
+ "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/file-entry-cache": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
@@ -14070,10 +14076,19 @@
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
}
},
+ "node_modules/posthog-js": {
+ "version": "1.110.0",
+ "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.110.0.tgz",
+ "integrity": "sha512-A5d0vYKLjmVtwF2uPxoFidDkVC6bTQUebm4s1LgKDIE+hHVfSmhbZ4pHqvwWmeJXdQHoQdvzG5Ve4STiiizBGQ==",
+ "dependencies": {
+ "fflate": "^0.4.8",
+ "preact": "^10.19.3"
+ }
+ },
"node_modules/preact": {
- "version": "10.15.1",
- "resolved": "https://registry.npmjs.org/preact/-/preact-10.15.1.tgz",
- "integrity": "sha512-qs2ansoQEwzNiV5eAcRT1p1EC/dmEzaATVDJNiB3g2sRDWdA7b7MurXdJjB2+/WQktGWZwxvDrnuRFbWuIr64g==",
+ "version": "10.19.6",
+ "resolved": "https://registry.npmjs.org/preact/-/preact-10.19.6.tgz",
+ "integrity": "sha512-gympg+T2Z1fG1unB8NH29yHJwnEaCH37Z32diPDku316OTnRPeMbiRV9kTrfZpocXjdfnWuFUl/Mj4BHaf6gnw==",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/preact"
diff --git a/package.json b/package.json
index 02fdb2d1..7fb21fbf 100644
--- a/package.json
+++ b/package.json
@@ -91,6 +91,7 @@
"notion-utils": "^6.16.0",
"openai": "^4.20.0",
"postcss": "^8.4.35",
+ "posthog-js": "^1.110.0",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dom": "^18.2.0",
diff --git a/pages/_app.tsx b/pages/_app.tsx
index 214d0f88..e9040145 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -10,6 +10,7 @@ import PlausibleProvider from "next-plausible";
import { ThemeProvider } from "@/components/theme-provider";
import { TeamProvider } from "@/context/team-context";
import { TooltipProvider } from "@/components/ui/tooltip";
+import { PostHogCustomProvider } from "@/components/providers/posthog-provider";
const inter = Inter({ subsets: ["latin"] });
@@ -60,21 +61,23 @@ export default function App({
<link rel="icon" href="/favicon.ico" />
</Head>
<SessionProvider session={session}>
- <ThemeProvider attribute="class" defaultTheme="light" enableSystem>
- <PlausibleProvider
- domain="papermark.io"
- enabled={process.env.NEXT_PUBLIC_VERCEL_ENV === "production"}
- >
- <main className={inter.className}>
- <Toaster closeButton richColors />
- <TooltipProvider delayDuration={100}>
- <TeamProvider>
- <Component {...pageProps} />
- </TeamProvider>
- </TooltipProvider>
- </main>
- </PlausibleProvider>
- </ThemeProvider>
+ <PostHogCustomProvider>
+ <ThemeProvider attribute="class" defaultTheme="light" enableSystem>
+ <PlausibleProvider
+ domain="papermark.io"
+ enabled={process.env.NEXT_PUBLIC_VERCEL_ENV === "production"}
+ >
+ <main className={inter.className}>
+ <Toaster closeButton richColors />
+ <TooltipProvider delayDuration={100}>
+ <TeamProvider>
+ <Component {...pageProps} />
+ </TeamProvider>
+ </TooltipProvider>
+ </main>
+ </PlausibleProvider>
+ </ThemeProvider>
+ </PostHogCustomProvider>
<Analytics />
</SessionProvider>
</>