Skip to content

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

refactor(analytics): document / link added

Marc Seitz9e0f2b9cc98e
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index 4ad26537..076138fd 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -24,6 +24,7 @@ import {
createNewDocumentVersion,
} from "@/lib/documents/create-document";
import { set } from "ts-pattern/dist/patterns";
+import { useAnalytics } from "@/lib/analytics";
export function AddDocumentModal({
newVersion,
@@ -34,6 +35,7 @@ export function AddDocumentModal({
}) {
const router = useRouter();
const plausible = usePlausible();
+ const analytics = useAnalytics();
const [uploading, setUploading] = useState<boolean>(false);
const [isOpen, setIsOpen] = useState<boolean | undefined>(undefined);
const [currentFile, setCurrentFile] = useState<File | null>(null);
@@ -94,12 +96,35 @@ export function AddDocumentModal({
// track the event
plausible("documentUploaded");
+ analytics.capture("Document Added", {
+ documentId: document.id,
+ name: document.name,
+ numPages: document.numPages,
+ path: router.asPath,
+ type: "pdf",
+ teamId: teamId,
+ });
+ analytics.capture("Link Added", {
+ linkId: document.links[0].id,
+ documentId: document.id,
+ customDomain: null,
+ teamId: teamId,
+ });
// redirect to the document page
router.push("/documents/" + document.id);
} else {
// track the event
plausible("documentVersionUploaded");
+ analytics.capture("Document Added", {
+ documentId: document.id,
+ name: document.name,
+ numPages: document.numPages,
+ path: router.asPath,
+ type: "pdf",
+ newVersion: true,
+ teamId: teamId,
+ });
toast.success("New document version uploaded.");
// reload to the document page
@@ -177,6 +202,20 @@ export function AddDocumentModal({
// track the event
plausible("documentUploaded");
plausible("notionDocumentUploaded");
+ analytics.capture("Document Added", {
+ documentId: document.id,
+ name: document.name,
+ fileSize: null,
+ path: router.asPath,
+ type: "notion",
+ teamId: teamId,
+ });
+ analytics.capture("Link Added", {
+ linkId: document.links[0].id,
+ documentId: document.id,
+ customDomain: null,
+ teamId: teamId,
+ });
// redirect to the document page
router.push("/documents/" + document.id);
diff --git a/components/welcome/notion-form.tsx b/components/welcome/notion-form.tsx
index 0bae9d09..fcb01cdc 100644
--- a/components/welcome/notion-form.tsx
+++ b/components/welcome/notion-form.tsx
@@ -1,6 +1,6 @@
import { useRouter } from "next/router";
import { motion } from "framer-motion";
-import { useState, type FormEvent } from "react";
+import { use, useState, type FormEvent } from "react";
import { toast } from "sonner";
import Skeleton from "../Skeleton";
import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";
@@ -25,10 +25,12 @@ import {
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
+import { useAnalytics } from "@/lib/analytics";
export default function NotionForm() {
const router = useRouter();
const plausible = usePlausible();
+ const analytics = useAnalytics();
const [uploading, setUploading] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [currentLinkId, setCurrentLinkId] = useState<string | null>(null);
@@ -93,6 +95,20 @@ export default function NotionForm() {
// track the event
plausible("documentUploaded");
plausible("notionDocumentUploaded");
+ analytics.capture("Document Added", {
+ documentId: document.id,
+ name: document.name,
+ fileSize: null,
+ path: router.asPath,
+ type: "notion",
+ teamId: teamInfo?.currentTeam?.id,
+ });
+ analytics.capture("Link Added", {
+ linkId: document.links[0].id,
+ documentId: document.id,
+ customDomain: null,
+ teamId: teamInfo?.currentTeam?.id,
+ });
// redirect to the document page
setTimeout(() => {
diff --git a/components/welcome/upload.tsx b/components/welcome/upload.tsx
index 823c16f0..82d27c20 100644
--- a/components/welcome/upload.tsx
+++ b/components/welcome/upload.tsx
@@ -25,12 +25,14 @@ import {
} from "../ui/accordion";
import { LinkOptions } from "../links/link-sheet/link-options";
import { DEFAULT_LINK_PROPS, DEFAULT_LINK_TYPE } from "../links/link-sheet";
+import { useAnalytics } from "@/lib/analytics";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
export default function Upload() {
const router = useRouter();
const plausible = usePlausible();
+ const analytics = useAnalytics();
const [uploading, setUploading] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [currentFile, setCurrentFile] = useState<File | null>(null);
@@ -77,6 +79,20 @@ export default function Upload() {
// track the event
plausible("documentUploaded");
+ analytics.capture("Document Added", {
+ documentId: document.id,
+ name: document.name,
+ numPages: document.numPages,
+ path: router.asPath,
+ type: "pdf",
+ teamId: teamInfo?.currentTeam?.id,
+ });
+ analytics.capture("Link Added", {
+ linkId: document.links[0].id,
+ documentId: document.id,
+ customDomain: null,
+ teamId: teamInfo?.currentTeam?.id,
+ });
setTimeout(() => {
setCurrentDocId(document.id);
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index 096ad626..c14b3022 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -4,7 +4,6 @@ import prisma from "@/lib/prisma";
import { authOptions } from "../../../auth/[...nextauth]";
import { CustomUser } from "@/lib/types";
import { getExtension, log } from "@/lib/utils";
-import { identifyUser, trackAnalytics } from "@/lib/analytics";
import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { errorhandler } from "@/lib/errorHandler";
import { client } from "@/trigger";
@@ -132,29 +131,6 @@ export default async function handle(
},
});
- // calculate the path of the page where the document was added
- const referer = req.headers.referer;
- let pathWithQuery = null;
- if (referer) {
- const url = new URL(referer);
- pathWithQuery = url.pathname + url.search;
- }
-
- await identifyUser((session.user as CustomUser).id);
- await trackAnalytics({
- event: "Document Added",
- documentId: document.id,
- name: document.name,
- fileSize: null,
- path: pathWithQuery,
- });
- await trackAnalytics({
- event: "Link Added",
- linkId: document.links[0].id,
- documentId: document.id,
- customDomain: null,
- });
-
// skip triggering convert-pdf-to-image job for "notion" documents
if (type !== "notion") {
// trigger document uploaded event to trigger convert-pdf-to-image job