Skip to content

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

feat: add analytics to dashboard

Marc Seitz825b07045828
diff --git a/components/charts/bar-chart.tsx b/components/charts/bar-chart.tsx
new file mode 100644
index 00000000..7fcee5b3
--- /dev/null
+++ b/components/charts/bar-chart.tsx
@@ -0,0 +1,57 @@
+import { Card, Title, BarChart, Subtitle, BarList } from "@tremor/react";
+
+const chartdata = [
+ {
+ name: "Amphibians",
+ "Time spent per page": 2488,
+ },
+ {
+ name: "Birds",
+ "Time spent per page": 1445,
+ },
+ {
+ name: "Crustaceans",
+ "Time spent per page": 743,
+ },
+];
+
+const dataFormatter = (number: number) => {
+ return "$ " + Intl.NumberFormat("us").format(number).toString();
+};
+
+const timeFormatter = (number: number) => {
+ const totalSeconds = Math.floor(number / 1000);
+ const minutes = Math.floor(totalSeconds / 60);
+ const seconds = Math.round(totalSeconds % 60);
+
+ // Adding zero padding if seconds less than 10
+ const secondsFormatted = seconds < 10 ? `0${seconds}` : `${seconds}`;
+
+ return `${minutes}:${secondsFormatted}`;
+};
+
+const renameAvgDurationKey = (data: any[]) => {
+ return data.map((item) => {
+ return {
+ ...item,
+ "Time spent per page": item.avg_duration,
+ avg_duration: undefined,
+ };
+ });
+};
+
+export default function BarChartComponent({data}: {data: any}) {
+ const renamedData = renameAvgDurationKey(data);
+
+ return (
+ <BarChart
+ className="mt-6 rounded-tremor-small"
+ data={renamedData}
+ index="pageNumber"
+ categories={["Time spent per page"]}
+ colors={["gray"]}
+ valueFormatter={timeFormatter}
+ yAxisWidth={50}
+ showGridLines={false}
+ />
+ );}
diff --git a/components/documents/stats-chart.tsx b/components/documents/stats-chart.tsx
new file mode 100644
index 00000000..ff40fc2b
--- /dev/null
+++ b/components/documents/stats-chart.tsx
@@ -0,0 +1,44 @@
+import { useStats } from "@/lib/swr/use-stats";
+import ErrorPage from "next/error";
+import BarChartComponent from "../charts/bar-chart";
+
+
+export default function StatsChart({
+ documentId,
+ totalPages = 0,
+}: {
+ documentId: string;
+ totalPages?: number;
+}) {
+ const { stats, error } = useStats();
+
+ if (error && error.status === 404) {
+ return <ErrorPage statusCode={404} />;
+ }
+
+ if (!stats?.duration.data) {
+ return <div>No data</div>;
+ }
+
+ let durationData = {
+ data: Array.from({ length: totalPages }, (_, i) => ({
+ pageNumber: (i + 1).toString(),
+ avg_duration: 0,
+ })),
+ };
+
+ const swrData = stats?.duration;
+
+ durationData.data = durationData.data.map((item) => {
+ const swrItem = swrData.data.find(
+ (data) => data.pageNumber === item.pageNumber
+ );
+ return swrItem ? swrItem : item;
+ });
+
+ return (
+ <div className="p-5">
+ <BarChartComponent data={durationData.data} />
+ </div>
+ );
+}
diff --git a/pages/documents/[id].tsx b/pages/documents/[id].tsx
index 5bcf9f5c..3dcc1bba 100644
--- a/pages/documents/[id].tsx
+++ b/pages/documents/[id].tsx
@@ -5,6 +5,8 @@ import { useDocument } from "@/lib/swr/use-document";
import ErrorPage from "next/error";
import LinksContainer from "@/components/links/links-container";
import StatsCard from "@/components/documents/stats-card";
+import BarChartComponent from "@/components/charts/bar-chart";
+import StatsChart from "@/components/documents/stats-chart";
export default function DocumentPage() {
const { document, error } = useDocument();
@@ -48,6 +50,12 @@ export default function DocumentPage() {
</div>
{/* Stats */}
+ {document.numPages !== null && (
+ <StatsChart
+ documentId={document.id}
+ totalPages={document.numPages}
+ />
+ )}
<StatsCard />
</header>