From 547f23fe78361550f7cc0cc1ae376889a41239db Mon Sep 17 00:00:00 2001 From: reecebrowne <74901996+reecebrowne@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:38:44 +0000 Subject: [PATCH] Posthog multitool (#2301) * Posthog functionality * Posthog in multitool * check if anylitics enabled --- .../static/js/multitool/PdfContainer.js | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/resources/static/js/multitool/PdfContainer.js b/src/main/resources/static/js/multitool/PdfContainer.js index 41d96ac4..d87fd6cb 100644 --- a/src/main/resources/static/js/multitool/PdfContainer.js +++ b/src/main/resources/static/js/multitool/PdfContainer.js @@ -122,12 +122,24 @@ class PdfContainer { async addFilesFromFiles(files, nextSiblingElement) { this.fileName = files[0].name; for (var i = 0; i < files.length; i++) { + const startTime = Date.now(); + let processingTime, errorMessage = null, pageCount = 0; + try { const file = files[i]; if (file.type === "application/pdf") { - await this.addPdfFile(file, nextSiblingElement); + const { renderer, pdfDocument } = await this.loadFile(file); + pageCount = renderer.pageCount || 0; + await this.addPdfFile(renderer, pdfDocument, nextSiblingElement); } else if (file.type.startsWith("image/")) { await this.addImageFile(file, nextSiblingElement); } + processingTime = Date.now() - startTime; + this.captureFileProcessingEvent(true, file, processingTime, null, pageCount); + } catch (error) { + processingTime = Date.now() - startTime; + errorMessage = error.message || "Unknown error"; + this.captureFileProcessingEvent(false, files[i], processingTime, errorMessage, pageCount); + } } document.querySelectorAll(".enable-on-file").forEach((element) => { @@ -135,6 +147,23 @@ class PdfContainer { }); } + captureFileProcessingEvent(success, file, processingTime, errorMessage, pageCount) { + try{ + if(analyticsEnabled){ + posthog.capture('file_processing', { + success, + file_type: file?.type || 'unknown', + file_size: file?.size || 0, + processing_time: processingTime, + error_message: errorMessage, + pdf_pages: pageCount, + }); +} +}catch{ +} +} + + async addFilesBlank(nextSiblingElement) { const pdfContent = ` %PDF-1.4 @@ -188,9 +217,7 @@ class PdfContainer { } - async addPdfFile(file, nextSiblingElement) { - const { renderer, pdfDocument } = await this.loadFile(file); - + async addPdfFile(renderer, pdfDocument, nextSiblingElement) { for (var i = 0; i < renderer.pageCount; i++) { const div = document.createElement("div");