Skip to content

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

add tinybird analytics to stats

Marc Seitz811fac22580d
diff --git a/components/documents/stats-card.tsx b/components/documents/stats-card.tsx
index 3ff163cf..b0825b06 100644
--- a/components/documents/stats-card.tsx
+++ b/components/documents/stats-card.tsx
@@ -23,9 +23,9 @@ export default function StatsCard() {
},
{
name: "Average view duration",
- value: "3.65",
- unit: "mins",
- active: false,
+ value: Math.round(stats?.total_duration / 1000),
+ unit: "seconds",
+ active: true,
},
{ name: "TBD", value: "98.5%", active: false },
];
diff --git a/pages/api/documents/[id]/stats.ts b/pages/api/documents/[id]/stats.ts
index 46fe0f25..896f7c5d 100644
--- a/pages/api/documents/[id]/stats.ts
+++ b/pages/api/documents/[id]/stats.ts
@@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import prisma from "@/lib/prisma";
import { authOptions } from "../../auth/[...nextauth]";
+import { getTotalAvgPageDuration } from "@/lib/tinybird";
export default async function handle(
req: NextApiRequest,
@@ -27,7 +28,17 @@ export default async function handle(
_count: { id: true },
});
- const stats = { views, groupedViews };
+ const duration = await getTotalAvgPageDuration({
+ documentId: id,
+ since: 0,
+ });
+
+ const total_duration = duration.data.reduce(
+ (totalDuration, data) => totalDuration + data.avg_duration,
+ 0
+ );
+
+ const stats = { views, groupedViews, duration, total_duration };
// TODO: Check that the user is owner of the document, otherwise return 401