Skip to content

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

fix(analytics): remove posthog-node

Marc Seitz3f881b53995f
diff --git a/lib/analytics/index.ts b/lib/analytics/index.ts
index a1ca268c..9ae8672d 100644
--- a/lib/analytics/index.ts
+++ b/lib/analytics/index.ts
@@ -1,9 +1,7 @@
import { emptyAnalytics, jitsuAnalytics } from "@jitsu/js";
import { AnalyticsEvents } from "@/lib/types";
-import prisma from "@/lib/prisma";
-import { getPostHogConfig, getPostHogServerClient } from "@/lib/posthog";
+import { getPostHogConfig } from "@/lib/posthog";
import { posthog } from "posthog-js";
-import { log } from "../utils";
export function useAnalytics() {
const isPostHogEnabled = getPostHogConfig();
@@ -39,102 +37,14 @@ export function useAnalytics() {
};
}
-export function getAnalyticsServer() {
- const postHogClient = getPostHogServerClient();
-
- const capture = async (
- distinctId: string, // email or user id
- event: string,
- properties?: Record<string, unknown>,
- ) => {
- if (!postHogClient) {
- log({
- message: `Posthog client not enabled for server event: *${event}*`,
- type: "error",
- });
- return;
- }
-
- try {
- postHogClient.capture({ distinctId, event, properties });
- await postHogClient.shutdownAsync();
- } catch (e) {
- log({
- message: `Failed to record posthog for event: *${event}*`,
- type: "error",
- });
- }
- };
-
- return {
- capture,
- };
-}
-
-// 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 trackAnalytics = (args: AnalyticsEvents) => analytics.track(args);
+// For server-side tracking
+const analytics =
+ process.env.JITSU_HOST && process.env.JITSU_WRITE_KEY
+ ? jitsuAnalytics({
+ host: process.env.JITSU_HOST,
+ writeKey: process.env.JITSU_WRITE_KEY,
+ })
+ : emptyAnalytics;
+
+export const identifyUser = (userId: string) => analytics.identify(userId);
+export const trackAnalytics = (args: AnalyticsEvents) => analytics.track(args);
diff --git a/lib/posthog.ts b/lib/posthog.ts
index dbdc742c..abc69aad 100644
--- a/lib/posthog.ts
+++ b/lib/posthog.ts
@@ -1,5 +1,3 @@
-import { PostHog } from "posthog-node";
-
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`;
@@ -13,18 +11,3 @@ export function getPostHogConfig(): { key: string; host: string } | null {
host: postHogHost,
};
}
-
-export function getPostHogServerClient() {
- const posthogConfig = getPostHogConfig();
- const isPostHogEnabled = !!posthogConfig;
-
- if (!isPostHogEnabled) {
- return null;
- }
-
- const client = new PostHog(posthogConfig.key, {
- host: posthogConfig.host,
- });
-
- return client;
-}
diff --git a/lib/types.ts b/lib/types.ts
index 17ad2410..e51a93e7 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -163,6 +163,10 @@ export type AnalyticsEvents =
| {
event: "Domain Deleted";
slug: string;
+ }
+ | {
+ event: "Team Member Invitation Accepted";
+ teamId: string;
};
export interface Team {
diff --git a/package-lock.json b/package-lock.json
index 94ce752d..857db094 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -84,7 +84,6 @@
"openai": "^4.20.0",
"postcss": "^8.4.35",
"posthog-js": "^1.110.0",
- "posthog-node": "^3.6.3",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dom": "^18.2.0",
@@ -6194,16 +6193,6 @@
"node": ">=4"
}
},
- "node_modules/axios": {
- "version": "1.6.7",
- "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz",
- "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==",
- "dependencies": {
- "follow-redirects": "^1.15.4",
- "form-data": "^4.0.0",
- "proxy-from-env": "^1.1.0"
- }
- },
"node_modules/axobject-query": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.2.1.tgz",
@@ -8646,25 +8635,6 @@
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ=="
},
- "node_modules/follow-redirects": {
- "version": "1.15.5",
- "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
- "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==",
- "funding": [
- {
- "type": "individual",
- "url": "https://github.com/sponsors/RubenVerborgh"
- }
- ],
- "engines": {
- "node": ">=4.0"
- },
- "peerDependenciesMeta": {
- "debug": {
- "optional": true
- }
- }
- },
"node_modules/for-each": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
@@ -14115,18 +14085,6 @@
"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",
- "integrity": "sha512-JB+ei0LkwE+rKHyW5z79Nd1jUaGxU6TvkfjFqY9vQaHxU5aU8dRl0UUaEmZdZbHwjp3WmXCBQQRNyimwbNQfCw==",
- "dependencies": {
- "axios": "^1.6.2",
- "rusha": "^0.8.14"
- },
- "engines": {
- "node": ">=15.0.0"
- }
- },
"node_modules/preact": {
"version": "10.19.6",
"resolved": "https://registry.npmjs.org/preact/-/preact-10.19.6.tgz",
@@ -14243,11 +14201,6 @@
"resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz",
"integrity": "sha512-vtK/94akxsTMhe0/cbfpR+syPuszcuwhqVjJq26CuNDgFGj682oRBXOP5MJpv2r7JtE8MsiepGIqvvOTBwn2vA=="
},
- "node_modules/proxy-from-env": {
- "version": "1.1.0",
- "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
- "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
- },
"node_modules/pump": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
@@ -15824,11 +15777,6 @@
"queue-microtask": "^1.2.2"
}
},
- "node_modules/rusha": {
- "version": "0.8.14",
- "resolved": "https://registry.npmjs.org/rusha/-/rusha-0.8.14.tgz",
- "integrity": "sha512-cLgakCUf6PedEu15t8kbsjnwIFFR2D4RfL+W3iWFJ4iac7z4B0ZI8fxy4R3J956kAI68HclCFGL8MPoUVC3qVA=="
- },
"node_modules/sade": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/sade/-/sade-1.8.1.tgz",
diff --git a/package.json b/package.json
index f47545e4..7fb21fbf 100644
--- a/package.json
+++ b/package.json
@@ -92,7 +92,6 @@
"openai": "^4.20.0",
"postcss": "^8.4.35",
"posthog-js": "^1.110.0",
- "posthog-node": "^3.6.3",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-dom": "^18.2.0",
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts
index c7025bb3..2298125b 100644
--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -7,7 +7,7 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "@/lib/prisma";
import { CreateUserEmailProps, CustomUser } from "@/lib/types";
import { sendWelcomeEmail } from "@/lib/emails/send-welcome";
-import { getAnalyticsServer } from "@/lib/analytics";
+import { identifyUser, trackAnalytics } from "@/lib/analytics";
import { sendVerificationRequestEmail } from "@/lib/emails/send-verification-request";
import hanko from "@/lib/hanko";
@@ -113,20 +113,21 @@ export const authOptions: NextAuthOptions = {
},
};
- const analytics = getAnalyticsServer();
- await analytics.capture(
- message.user.email ?? message.user.id,
- "User Signed Up",
- );
+ await identifyUser(message.user.email ?? message.user.id);
+ await trackAnalytics({
+ event: "User Signed Up",
+ email: message.user.email,
+ userId: message.user.id,
+ });
await sendWelcomeEmail(params);
},
async signIn(message) {
- const analytics = getAnalyticsServer();
- await analytics.capture(
- message.user.email ?? message.user.id,
- "User Signed In",
- );
+ await identifyUser(message.user.email ?? message.user.id);
+ await trackAnalytics({
+ event: "User Signed In",
+ email: message.user.email,
+ });
},
},
};
diff --git a/pages/api/stripe/webhook.ts b/pages/api/stripe/webhook.ts
index be9d1837..fa5297a7 100644
--- a/pages/api/stripe/webhook.ts
+++ b/pages/api/stripe/webhook.ts
@@ -6,7 +6,6 @@ import { stripe } from "@/lib/stripe";
import { getPlanFromPriceId, isNewCustomer } from "@/lib/stripe/utils";
import { log } from "@/lib/utils";
import { sendUpgradePlanEmail } from "@/lib/emails/send-upgrade-plan";
-import { getAnalyticsServer } from "@/lib/analytics";
// Stripe requires the raw body to construct the event.
export const config = {
@@ -43,8 +42,6 @@ interface SubscriptionInfo {
const pendingSubscriptionUpdates = new Map<string, SubscriptionInfo>();
-const analytics = getAnalyticsServer();
-
export default async function webhookHandler(
req: NextApiRequest,
res: NextApiResponse,
@@ -120,16 +117,6 @@ export default async function webhookHandler(
});
}
- analytics.capture(
- team.users[0].user.email ?? team.users[0].user.id,
- "User Upgraded",
- {
- teamId: team.id,
- teamPlan: plan.slug,
- $set: { teamId: team.id, teamPlan: plan.slug },
- },
- );
-
// Remove the pending update from the store
pendingSubscriptionUpdates.delete(stripeId);
}
diff --git a/pages/api/teams/[teamId]/domains/[domain]/verify.ts b/pages/api/teams/[teamId]/domains/[domain]/verify.ts
index 82320884..b1555338 100644
--- a/pages/api/teams/[teamId]/domains/[domain]/verify.ts
+++ b/pages/api/teams/[teamId]/domains/[domain]/verify.ts
@@ -6,7 +6,7 @@ import {
} from "@/lib/domains";
import { DomainVerificationStatusProps } from "@/lib/types";
import prisma from "@/lib/prisma";
-import { getAnalyticsServer } from "@/lib/analytics";
+import { identifyUser, trackAnalytics } from "@/lib/analytics";
export default async function handle(
req: NextApiRequest,
@@ -76,10 +76,8 @@ export default async function handle(
});
if (!currentDomain!.verified && updatedDomain.verified) {
- const analytics = getAnalyticsServer();
- analytics.capture(updatedDomain.userId, "Domain Verified", {
- slug: domain,
- });
+ await identifyUser(updatedDomain.userId);
+ await trackAnalytics({ event: "Domain Verified", slug: domain });
}
}
diff --git a/pages/api/teams/[teamId]/invitations/accept.ts b/pages/api/teams/[teamId]/invitations/accept.ts
index afb2b6b0..8280549c 100644
--- a/pages/api/teams/[teamId]/invitations/accept.ts
+++ b/pages/api/teams/[teamId]/invitations/accept.ts
@@ -4,7 +4,7 @@ import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { errorhandler } from "@/lib/errorHandler";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
-import { getAnalyticsServer } from "@/lib/analytics";
+import { identifyUser, trackAnalytics } from "@/lib/analytics";
export default async function handle(
req: NextApiRequest,
@@ -71,8 +71,9 @@ export default async function handle(
},
});
- const analytics = getAnalyticsServer();
- analytics.capture(invitation.email, "Team Member Invitation Accepted", {
+ await identifyUser(invitation.email);
+ await trackAnalytics({
+ event: "Team Member Invitation Accepted",
teamId: teamId,
});