Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add api route to record page view in tinybird
Marc Seitz93a9238c0529
diff --git a/lib/id-helper.ts b/lib/id-helper.tsnew file mode 100644index 00000000..caf6ebe1--- /dev/null+++ b/lib/id-helper.ts@@ -0,0 +1,37 @@+import baseX from "base-x";++function encodeBase58(buf: Buffer): string {+ const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";++ return baseX(alphabet).encode(buf);+}+/**+ * Generate ids similar to stripe+ */+export class IdGenerator<TPrefixes extends string> {+ private prefixes: Record<TPrefixes, string>;++ /**+ * Create a new id generator with fully typed prefixes+ * @param prefixes - Relevant prefixes for your domain+ */+ constructor(prefixes: Record<TPrefixes, string>) {+ this.prefixes = prefixes;+ }++ /**+ * Generate a new unique base58 encoded uuid with a defined prefix+ *+ * @returns xxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx+ */+ public id = (prefix: TPrefixes): string => {+ return [+ this.prefixes[prefix],+ encodeBase58(Buffer.from(crypto.randomUUID().replace(/-/g, ""), "hex")),+ ].join("_");+ };+}++export const newId = new IdGenerator({+ view: "view",+}).id;diff --git a/lib/utils.ts b/lib/utils.tsindex 0abbfe5c..c81d50db 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -71,3 +71,32 @@ export function bytesToSize(bytes: number) {}return `${Math.round(sizeInCurrentUnit)} ${sizes[i]}`;}+++const isValidUrl = (url: string) => {+ try {+ new URL(url);+ return true;+ } catch (e) {+ return false;+ }+};++export const getDomainWithoutWWW = (url: string) => {+ if (isValidUrl(url)) {+ return new URL(url).hostname.replace(/^www\./, "");+ }+ try {+ if (url.includes(".") && !url.includes(" ")) {+ return new URL(`https://${url}`).hostname.replace(/^www\./, "");+ }+ } catch (e) {+ return "(direct)"; // Not a valid URL, but cannot return null+ }+};++export function capitalize(str: string) {+ if (!str || typeof str !== "string") return str;+ return str.charAt(0).toUpperCase() + str.slice(1);+}+diff --git a/package-lock.json b/package-lock.jsonindex 0a8ca7c7..2fe11bb4 100644--- a/package-lock.json+++ b/package-lock.json@@ -22,6 +22,7 @@"@vercel/analytics": "^1.0.1","@vercel/blob": "^0.8.3","autoprefixer": "10.4.14",+ "base-x": "^4.0.0","class-variance-authority": "^0.6.0","clsx": "^1.2.1","date-fns": "^2.30.0",@@ -2357,6 +2358,11 @@"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz","integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="},+ "node_modules/base-x": {+ "version": "4.0.0",+ "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz",+ "integrity": "sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw=="+ },"node_modules/base64-js": {"version": "1.5.1","resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",@@ -9976,6 +9982,11 @@"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz","integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="},+ "base-x": {+ "version": "4.0.0",+ "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz",+ "integrity": "sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw=="+ },"base64-js": {"version": "1.5.1","resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",@@ -13946,6 +13957,11 @@"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz","integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw=="},+ "ua-parser-js": {+ "version": "1.0.35",+ "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-1.0.35.tgz",+ "integrity": "sha512-fKnGuqmTBnIE+/KXSzCn4db8RTigUzw1AN0DmdU6hJovUTbYJKyqj+8Mt1c4VfRDnOVJnENmfYkIPZ946UrSAA=="+ },"unbox-primitive": {"version": "1.0.2","resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz",diff --git a/package.json b/package.jsonindex f332cde3..9c7c2b8a 100644--- a/package.json+++ b/package.json@@ -27,6 +27,7 @@"@vercel/analytics": "^1.0.1","@vercel/blob": "^0.8.3","autoprefixer": "10.4.14",+ "base-x": "^4.0.0","class-variance-authority": "^0.6.0","clsx": "^1.2.1","date-fns": "^2.30.0",diff --git a/pages/api/record_view.ts b/pages/api/record_view.tsnew file mode 100644index 00000000..823ae11e--- /dev/null+++ b/pages/api/record_view.ts@@ -0,0 +1,109 @@+import { NextApiRequest, NextApiResponse } from "next";+import { newId } from "@/lib/id-helper";+import { publishPageView } from "@/lib/tinybird";+import { capitalize, getDomainWithoutWWW, log } from "@/lib/utils";+import { userAgentFromString } from "@/lib/utils/user-agent";+import { Geo } from "@/lib/types";+import { LOCALHOST_GEO_DATA, getGeoData } from "@/lib/utils/geo";++import { z } from "zod";+++const bodyValidation = z.object({+ id: z.string(),+ linkId: z.string(),+ documentId: z.string(),+ viewId: z.string(),+ time: z.number().int(),+ duration: z.number().int(),+ pageNumber: z.string(),+ country: z.string().optional(),+ city: z.string().optional(),+ region: z.string().optional(),+ latitude: z.string().optional(),+ longitude: z.string().optional(),+ ua: z.string().optional(),+ browser: z.string().optional(),+ browser_version: z.string().optional(),+ engine: z.string().optional(),+ engine_version: z.string().optional(),+ os: z.string().optional(),+ os_version: z.string().optional(),+ device: z.string().optional(),+ device_vendor: z.string().optional(),+ device_model: z.string().optional(),+ cpu_architecture: z.string().optional(),+ bot: z.boolean().optional(),+ referer: z.string().optional(),+ referer_url: z.string().optional(),+});++export default async function handle(+ req: NextApiRequest,+ res: NextApiResponse+) {+ // We only allow POST requests+ if (req.method !== "POST") {+ res.status(405).json({ message: "Method Not Allowed" });+ return;+ }++ const geo: Geo =+ process.env.VERCEL === "1" ? getGeoData(req.headers) : LOCALHOST_GEO_DATA;+++ const referer = req.headers.referer;+ const ua = userAgentFromString(req.headers["user-agent"]);++ const { linkId, documentId, viewId, duration, pageNumber } = req.body;++ const time = Date.now(); // in milliseconds++ const pageViewId = newId("view");+++ const pageViewObject = {+ id: pageViewId,+ linkId,+ documentId,+ viewId,+ time,+ duration,+ pageNumber: pageNumber.toString(),+ country: geo?.country || "Unknown",+ city: geo?.city || "Unknown",+ region: geo?.region || "Unknown",+ latitude: geo?.latitude || "Unknown",+ longitude: geo?.longitude || "Unknown",+ ua: ua.ua || "Unknown",+ browser: ua.browser.name || "Unknown",+ browser_version: ua.browser.version || "Unknown",+ engine: ua.engine.name || "Unknown",+ engine_version: ua.engine.version || "Unknown",+ os: ua.os.name || "Unknown",+ os_version: ua.os.version || "Unknown",+ device: ua.device.type ? capitalize(ua.device.type) : "Desktop",+ device_vendor: ua.device.vendor || "Unknown",+ device_model: ua.device.model || "Unknown",+ cpu_architecture: ua.cpu?.architecture || "Unknown",+ bot: ua.isBot,+ referer: referer ? getDomainWithoutWWW(referer) : "(direct)",+ referer_url: referer || "(direct)",+ };++ const result = bodyValidation.safeParse(pageViewObject);+ if (!result.success) {+ return res.status(400).json(+ { error: `Invalid body: ${result.error.message}` }+ );+ }++ try {+ await publishPageView(result.data);++ res.status(200).json({ message: "View recorded" });+ } catch (error) {+ log(`Failed to record view for ${linkId}. Error: \n\n ${error}`);+ res.status(500).json({ message: (error as Error).message });+ }+}