Track visitor activity presence

This commit is contained in:
rajchales-monito
2026-07-24 12:19:41 +02:00
parent 5adb220b6e
commit f052bca3ad
4 changed files with 113 additions and 15 deletions
+21 -1
View File
@@ -7,6 +7,7 @@
const session = loadSession();
const allowedImageTypes = new Set(["image/jpeg", "image/png", "image/webp", "image/gif", "image/avif"]);
const maxAttachmentBytes = 3_000_000;
let lastActivityAt = Date.now();
fetch(`${apiBase}/api/widget/config?siteKey=${encodeURIComponent(siteKey)}`)
.then((res) => res.json())
@@ -164,10 +165,14 @@
});
rememberPage();
trackVisitorActivity(root);
pingVisitor();
setInterval(pingVisitor, 25_000);
document.addEventListener("visibilitychange", () => {
if (!document.hidden) pingVisitor();
if (!document.hidden) {
noteActivity();
pingVisitor();
}
});
if (session.conversationId) {
loadExistingConversation(messages).finally(() => connectEvents(messages, typing, panel, launcher, settings));
@@ -340,6 +345,7 @@
currentUrl: location.href,
referrer: document.referrer,
browsingHistory: rememberPage(),
lastActivityAt: new Date(lastActivityAt).toISOString(),
language: navigator.language,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
device: {
@@ -354,6 +360,20 @@
postJson(`${apiBase}/api/widget/visitors/ping`, withVisitorContext({})).catch(() => {});
}
function trackVisitorActivity(root) {
const passive = { passive: true, capture: true };
for (const eventName of ["pointerdown", "keydown", "mousemove", "touchstart", "scroll"]) {
document.addEventListener(eventName, noteActivity, passive);
root.addEventListener(eventName, noteActivity, passive);
}
}
function noteActivity() {
const now = Date.now();
if (now - lastActivityAt < 1_000) return;
lastActivityAt = now;
}
function showFormError(root, message) {
const note = root.querySelector(".mf-note");
note.textContent = message;