Add visitor bot visibility toggle

This commit is contained in:
rajchales-monito
2026-07-24 11:51:36 +02:00
parent 79826cd26d
commit ce99d7114f
4 changed files with 51 additions and 6 deletions
+19
View File
@@ -639,6 +639,7 @@ function currentVisitors() {
function formatVisitorSession(row) {
const history = parseJson(row.browsing_history_json, []);
const device = parseJson(row.device_json, {});
const bot = botInfo(device.userAgent);
const countryCode = row.country_code || countryFromTimezone(row.timezone) || countryFromLanguage(row.language);
return {
id: row.visitor_token,
@@ -652,6 +653,8 @@ function formatVisitorSession(row) {
visitCount: Math.max(1, history.length),
conversationCount: row.visitor_conversation_count || 0,
lastConversationId: row.last_conversation_id,
isBot: bot.isBot,
botReason: bot.reason,
device,
language: row.language,
timezone: row.timezone,
@@ -915,6 +918,22 @@ function countryFromLanguage(language) {
return match ? match[1].toUpperCase() : null;
}
function botInfo(userAgent) {
const ua = String(userAgent || "");
const patterns = [
"googlebot", "bingbot", "slurp", "duckduckbot", "baiduspider", "yandexbot",
"ahrefsbot", "semrushbot", "mj12bot", "dotbot", "petalbot", "bytespider",
"facebookexternalhit", "twitterbot", "linkedinbot", "slackbot", "discordbot",
"whatsapp", "telegrambot", "preview", "crawler", "spider", "headlesschrome",
"pingdom", "uptimerobot", "curl", "wget", "python-requests", "axios", "httpclient"
];
const lower = ua.toLowerCase();
const match = patterns.find((pattern) => lower.includes(pattern));
if (match) return { isBot: true, reason: match };
if (/\bbot\b/i.test(ua)) return { isBot: true, reason: "bot" };
return { isBot: false, reason: "" };
}
function visitorInitials(visitorToken) {
const hash = crypto.createHash("sha1").update(String(visitorToken || "")).digest("hex").toUpperCase();
return hash.slice(0, 2);