2024-09-08 14:10:02 +03:30
|
|
|
const useragent = require("useragent");
|
|
|
|
const geoip = require("geoip-lite");
|
2025-01-21 09:38:14 +01:00
|
|
|
const URL = require("node:url");
|
2019-11-02 19:26:26 +03:30
|
|
|
|
2024-12-09 18:43:00 +03:30
|
|
|
const { removeWww } = require("../utils");
|
2024-09-08 14:10:02 +03:30
|
|
|
const query = require("../queries");
|
2019-11-06 20:53:09 +03:30
|
|
|
|
2019-11-02 19:26:26 +03:30
|
|
|
const browsersList = ["IE", "Firefox", "Chrome", "Opera", "Safari", "Edge"];
|
|
|
|
const osList = ["Windows", "Mac OS", "Linux", "Android", "iOS"];
|
|
|
|
|
2024-09-08 14:10:02 +03:30
|
|
|
function filterInBrowser(agent) {
|
|
|
|
return function(item) {
|
|
|
|
return agent.family.toLowerCase().includes(item.toLocaleLowerCase());
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:16:00 +03:30
|
|
|
|
2024-09-08 14:10:02 +03:30
|
|
|
function filterInOs(agent) {
|
|
|
|
return function(item) {
|
|
|
|
return agent.os.family.toLowerCase().includes(item.toLocaleLowerCase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function({ data }) {
|
|
|
|
const tasks = [];
|
|
|
|
|
|
|
|
tasks.push(query.link.incrementVisit({ id: data.link.id }));
|
2024-09-15 08:22:54 +03:30
|
|
|
|
2024-12-04 09:17:18 +03:30
|
|
|
// the following line is for backward compatibility
|
|
|
|
// used to send the whole header to get the user agent
|
|
|
|
const userAgent = data.userAgent || data.headers?.["user-agent"];
|
|
|
|
const agent = useragent.parse(userAgent);
|
|
|
|
const [browser = "Other"] = browsersList.filter(filterInBrowser(agent));
|
|
|
|
const [os = "Other"] = osList.filter(filterInOs(agent));
|
|
|
|
const referrer =
|
|
|
|
data.referrer && removeWww(URL.parse(data.referrer).hostname);
|
|
|
|
|
2024-12-23 11:04:58 +03:30
|
|
|
const country = data.country || geoip.lookup(data.ip)?.country;
|
|
|
|
|
2024-12-04 09:17:18 +03:30
|
|
|
tasks.push(
|
|
|
|
query.visit.add({
|
|
|
|
browser: browser.toLowerCase(),
|
|
|
|
country: country || "Unknown",
|
2024-12-23 18:58:42 +03:30
|
|
|
link_id: data.link.id,
|
|
|
|
user_id: data.link.user_id,
|
2024-12-04 09:17:18 +03:30
|
|
|
os: os.toLowerCase().replace(/\s/gi, ""),
|
|
|
|
referrer: (referrer && referrer.replace(/\./gi, "[dot]")) || "Direct"
|
|
|
|
})
|
|
|
|
);
|
2019-11-02 20:16:00 +03:30
|
|
|
|
|
|
|
return Promise.all(tasks);
|
2024-09-08 14:10:02 +03:30
|
|
|
}
|