Skip to content

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

feat: add analytics hooks (client and server)

Marc Seitzd29b1435662f
diff --git a/lib/analytics/index.ts b/lib/analytics/index.ts
index b79edcf2..39017171 100644
--- a/lib/analytics/index.ts
+++ b/lib/analytics/index.ts
@@ -1,70 +1,142 @@
import { emptyAnalytics, jitsuAnalytics } from "@jitsu/js";
-import { AnalyticsEvents } from "../types";
+import { AnalyticsEvents } from "@/lib/types";
import prisma from "@/lib/prisma";
+import { getPostHogConfig, getPostHogServerClient } from "@/lib/posthog";
+import { posthog } from "posthog-js";
-export const analytics =
- process.env.JITSU_HOST && process.env.JITSU_WRITE_KEY
- ? jitsuAnalytics({
- host: process.env.JITSU_HOST,
- writeKey: process.env.JITSU_WRITE_KEY,
- })
- : emptyAnalytics;
-
-const fetchUserWithCounts = async (userId: string) => {
- return await prisma.user.findUnique({
- where: { id: userId },
- select: {
- id: true,
- email: true,
- _count: {
- select: {
- documents: true,
- domains: true,
- },
- },
- documents: {
- select: {
- _count: {
- select: {
- links: true,
- views: true,
- },
- },
- },
- },
- },
- });
-};
-
-const getUserProperties = (user: any) => {
- let linkCount = user.documents.reduce(
- (acc: any, document: { _count: { links: number; views: number } }) =>
- acc + document._count.links,
- 0,
- );
-
- let viewCount = user.documents.reduce(
- (acc: any, document: { _count: { links: number; views: number } }) =>
- acc + document._count.views,
- 0,
- );
-
- const userData = {
- userId: user.id,
- email: user.email,
- documentCount: user._count.documents,
- domainCount: user._count.domains,
- linkCount: linkCount,
- viewCount: viewCount,
+export function useAnalytics() {
+ const isPostHogEnabled = getPostHogConfig();
+
+ /**
+ * Capture an analytic event.
+ *
+ * @param event The event name.
+ * @param properties Properties to attach to the event.
+ */
+ const capture = (event: string, properties?: Record<string, unknown>) => {
+ if (!isPostHogEnabled) {
+ return;
+ }
+
+ posthog.capture(event, properties);
+ console.log("tracked pageview", event, properties);
+ };
+
+ const identify = (
+ distinctId?: string,
+ properties?: Record<string, unknown>,
+ ) => {
+ if (!isPostHogEnabled) {
+ return;
+ }
+
+ posthog.identify(distinctId, properties);
+ console.log("identified user", distinctId, properties);
+ };
+
+ return {
+ capture,
+ identify,
+ };
+}
+
+export function useAnalyticsServer() {
+ const postHogClient = getPostHogServerClient();
+
+ const capture = (
+ distinctId: string, // email or user id
+ event: string,
+ properties?: Record<string, unknown>,
+ ) => {
+ if (!postHogClient) {
+ return;
+ }
+
+ postHogClient.capture({ distinctId, event, properties });
+ console.log("tracked pageview", distinctId, event, properties);
};
- return userData;
-};
+ const identify = (
+ distinctId: string, // email or user id
+ properties?: Record<string, unknown>,
+ ) => {
+ if (!postHogClient) {
+ return;
+ }
+
+ postHogClient.identify({ distinctId, properties });
+ };
+
+ return {
+ capture,
+ identify,
+ };
+}
+
+// DEPRECATED
+// export const analytics =
+// process.env.JITSU_HOST && process.env.JITSU_WRITE_KEY
+// ? jitsuAnalytics({
+// host: process.env.JITSU_HOST,
+// writeKey: process.env.JITSU_WRITE_KEY,
+// })
+// : emptyAnalytics;
+
+// const fetchUserWithCounts = async (userId: string) => {
+// return await prisma.user.findUnique({
+// where: { id: userId },
+// select: {
+// id: true,
+// email: true,
+// _count: {
+// select: {
+// documents: true,
+// domains: true,
+// },
+// },
+// documents: {
+// select: {
+// _count: {
+// select: {
+// links: true,
+// views: true,
+// },
+// },
+// },
+// },
+// },
+// });
+// };
+
+// const getUserProperties = (user: any) => {
+// let linkCount = user.documents.reduce(
+// (acc: any, document: { _count: { links: number; views: number } }) =>
+// acc + document._count.links,
+// 0,
+// );
+
+// let viewCount = user.documents.reduce(
+// (acc: any, document: { _count: { links: number; views: number } }) =>
+// acc + document._count.views,
+// 0,
+// );
+
+// const userData = {
+// userId: user.id,
+// email: user.email,
+// documentCount: user._count.documents,
+// domainCount: user._count.domains,
+// linkCount: linkCount,
+// viewCount: viewCount,
+// };
+
+// return userData;
+// };
-export const identifyUser = async (userId: string) => {
- const user = await fetchUserWithCounts(userId);
- const userProperties = getUserProperties(user);
- analytics.identify(userId, userProperties);
-};
+// export const identifyUser = async (userId: string) => {
+// const user = await fetchUserWithCounts(userId);
+// const userProperties = getUserProperties(user);
+// analytics.identify(userId, userProperties);
+// };
-export const trackAnalytics = (args: AnalyticsEvents) => analytics.track(args);
+// export const trackAnalytics = (args: AnalyticsEvents) => analytics.track(args);