Skip to content

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

fix: track last page before user leaves

Marc Seitzfc6cd717b54b
diff --git a/components/view/PagesViewer.tsx b/components/view/PagesViewer.tsx
index 367ec6df..ad4ab392 100644
--- a/components/view/PagesViewer.tsx
+++ b/components/view/PagesViewer.tsx
@@ -111,6 +111,22 @@ export default function PagesViewer({
};
}, [pageNumber, numPages]); // Track page view when the page number changes
+ // Send the last page view when the user leaves the page
+ useEffect(() => {
+ const handleBeforeUnload = () => {
+ if (pageNumber <= numPages && visibilityRef.current) {
+ const duration = Date.now() - startTimeRef.current;
+ trackPageView(duration);
+ }
+ };
+
+ window.addEventListener("beforeunload", handleBeforeUnload);
+
+ return () => {
+ window.removeEventListener("beforeunload", handleBeforeUnload);
+ };
+ }, [pageNumber, numPages]);
+
useEffect(() => {
setLoadedImages((prev) =>
prev.map((loaded, index) =>