Skip to content

Commit 8f942bd

Browse files
authored
Merge pull request #347 from mfts/feat/use-visibility-analytics
feat: only track analytics if documents are visible
2 parents 49ee84f + a702003 commit 8f942bd

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

Diff for: components/view/PagesViewer.tsx

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
22
import { useEffect, useRef, useState } from "react";
33
import Image from "next/image";
4-
import LoadingSpinner from "../ui/loading-spinner";
4+
import LoadingSpinner from "@/components/ui/loading-spinner";
55
import BlankImg from "@/public/_static/blank.gif";
66
import Nav from "./nav";
77
import Toolbar from "./toolbar";
@@ -45,37 +45,43 @@ export default function PagesViewer({
4545

4646
const startTimeRef = useRef(Date.now());
4747
const pageNumberRef = useRef<number>(pageNumber);
48+
const visibilityRef = useRef<boolean>(true);
4849

4950
// Update the previous page number after the effect hook has run
5051
useEffect(() => {
5152
pageNumberRef.current = pageNumber;
5253
}, [pageNumber]);
5354

5455
useEffect(() => {
55-
startTimeRef.current = Date.now(); // update the start time for the new page
56+
const handleVisibilityChange = () => {
57+
if (document.visibilityState === "visible") {
58+
visibilityRef.current = true;
59+
startTimeRef.current = Date.now(); // Reset start time when the page becomes visible again
60+
} else {
61+
visibilityRef.current = false;
62+
const duration = Date.now() - startTimeRef.current;
63+
trackPageView(duration);
64+
}
65+
};
66+
67+
document.addEventListener("visibilitychange", handleVisibilityChange);
5668

57-
// when component unmounts, calculate duration and track page view
5869
return () => {
59-
const endTime = Date.now();
60-
const duration = Math.round(endTime - startTimeRef.current);
61-
trackPageView(duration);
70+
document.removeEventListener("visibilitychange", handleVisibilityChange);
6271
};
63-
}, [pageNumber]); // monitor pageNumber for changes
72+
}, []); // track page view when the page becomes visible or hidden on mount and unmount
6473

65-
// Send the last page view when the user leaves the page
6674
useEffect(() => {
67-
const handleBeforeUnload = () => {
68-
const endTime = Date.now();
69-
const duration = Math.round(endTime - startTimeRef.current);
70-
trackPageView(duration);
71-
};
72-
73-
window.addEventListener("beforeunload", handleBeforeUnload);
75+
startTimeRef.current = Date.now();
7476

7577
return () => {
76-
window.removeEventListener("beforeunload", handleBeforeUnload);
78+
if (visibilityRef.current) {
79+
// Only track the page view if the page is visible
80+
const duration = Date.now() - startTimeRef.current;
81+
trackPageView(duration);
82+
}
7783
};
78-
}, []);
84+
}, [pageNumber]); // Track page view when the page number changes
7985

8086
useEffect(() => {
8187
setLoadedImages((prev) =>

0 commit comments

Comments
 (0)