From d0d6a70250221cc6b3d9534452ac4a004cd263f4 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:32:44 +0000 Subject: [PATCH] Metrics changes (#2273) * Update downloader.js * Update downloader.js * Update common.html * Update downloader.js --- src/main/resources/static/js/downloader.js | 41 +++++++++++++++++-- .../resources/templates/fragments/common.html | 3 +- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/resources/static/js/downloader.js b/src/main/resources/static/js/downloader.js index c9a828c1..2ccbc093 100644 --- a/src/main/resources/static/js/downloader.js +++ b/src/main/resources/static/js/downloader.js @@ -96,14 +96,31 @@ }); }); + async function getPDFPageCount(file) { + try { + const arrayBuffer = await file.arrayBuffer(); + pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdfjs-legacy/pdf.worker.mjs' + const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise; + return pdf.numPages; + } catch (error) { + console.error('Error getting PDF page count:', error); + return null; + } + } + async function handleSingleDownload(url, formData, isMulti = false, isZip = false) { + const startTime = performance.now(); + const file = formData.get('fileInput'); + let success = false; + let errorMessage = null; + try { const response = await fetch(url, { method: "POST", body: formData }); const contentType = response.headers.get("content-type"); if (!response.ok) { + errorMessage = response.status; if (response.status === 401) { - // Handle 401 Unauthorized error showSessionExpiredPrompt(); return; } @@ -118,6 +135,8 @@ let filename = getFilenameFromContentDisposition(contentDisposition); const blob = await response.blob(); + success = true; + if (contentType.includes("application/pdf") || contentType.includes("image/")) { clearFileInput(); return handleResponse(blob, filename, !isMulti, isZip); @@ -127,13 +146,29 @@ } } catch (error) { + success = false; + errorMessage = error.message; clearFileInput(); console.error("Error in handleSingleDownload:", error); throw error; + } finally { + const processingTime = performance.now() - startTime; + + // Capture analytics + const pageCount = file && file.type === 'application/pdf' ? await getPDFPageCount(file) : null; + + posthog.capture('file_processing', { + success: success, + file_type: file ? file.type || 'unknown' : 'unknown', + file_size: file ? file.size : 0, + processing_time: processingTime, + error_message: errorMessage, + pdf_pages: pageCount + }); } } - function getFilenameFromContentDisposition(contentDisposition) { + function getFilenameFromContentDisposition(contentDisposition) { let filename; if (contentDisposition && contentDisposition.indexOf("attachment") !== -1) { @@ -145,7 +180,7 @@ return filename; } - + async function handleJsonResponse(response) { const json = await response.json(); const errorMessage = JSON.stringify(json, null, 2); diff --git a/src/main/resources/templates/fragments/common.html b/src/main/resources/templates/fragments/common.html index dd74d388..ac94030a 100644 --- a/src/main/resources/templates/fragments/common.html +++ b/src/main/resources/templates/fragments/common.html @@ -201,6 +201,7 @@ window.stirlingPDF.error = /*[[#{error}]]*/ "Error"; })(); +