🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
improve device type detection
Bill Yang92bfc4382938
diff --git a/client/src/app/components/MainSection/Chart.tsx b/client/src/app/components/MainSection/Chart.tsxindex b9e579a50..06f77b669 100644--- a/client/src/app/components/MainSection/Chart.tsx+++ b/client/src/app/components/MainSection/Chart.tsx@@ -121,9 +121,7 @@ export function Chart({tickRotation: 0,truncateTickAt: 0,tickValues:- time.mode === "day"- ? Math.min(24, data?.data?.length ?? 0)- : Math.min(12, data?.data?.length ?? 0),+ time.mode === "day" ? 24 : Math.min(12, data?.data?.length ?? 0),format: (value) => {if (time.mode === "day") {return DateTime.fromJSDate(value).toFormat("ha");diff --git a/client/src/app/components/shared/StandardCard.tsx b/client/src/app/components/shared/StandardCard.tsxindex 369c92930..3af3af188 100644--- a/client/src/app/components/shared/StandardCard.tsx+++ b/client/src/app/components/shared/StandardCard.tsx@@ -59,7 +59,9 @@ export function StandardCard<T extends { percentage: number; count: number }>({))}{data?.data?.length && data?.data?.length > 10 && (<Dialog>- <Button variant="outline">View All</Button>+ <DialogTrigger asChild>+ <Button variant="outline">View All</Button>+ </DialogTrigger><DialogContent className="max-w-[90vw] w-[1000px]"><DialogHeader><DialogTitle>{title}</DialogTitle>diff --git a/server/src/actions/pageviewQueue.ts b/server/src/actions/pageviewQueue.tsindex a60f2ae78..2651e3847 100644--- a/server/src/actions/pageviewQueue.ts+++ b/server/src/actions/pageviewQueue.ts@@ -65,7 +65,11 @@ class PageviewQueue {language: pv.language || "",screen_width: pv.screenWidth || 0,screen_height: pv.screenHeight || 0,- device_type: getDeviceType(pv.screenWidth, pv.screenHeight),+ device_type: getDeviceType(+ pv.screenWidth,+ pv.screenHeight,+ pv.ua.os.name+ ),country: geoData[pv.ipAddress]?.data?.countryIso || "",// countryIso: geoData[pv.ip]?.data?.countryIso || null,}));diff --git a/server/src/actions/trackPageView.ts b/server/src/actions/trackPageView.tsindex 0cc2ae302..2cb876b28 100644--- a/server/src/actions/trackPageView.ts+++ b/server/src/actions/trackPageView.ts@@ -6,6 +6,7 @@ import clickhouse from "../db/clickhouse/clickhouse";import { DateTime } from "luxon";import { sql } from "../db/postgres/postgres";import UAParser, { UAParser as userAgentParser } from "ua-parser-js";+import { Pageview } from "../db/clickhouse/types";import { pageviewQueue } from "./pageviewQueue";@@ -18,42 +19,42 @@ type TotalPayload = TrackingPayload & {ipAddress: string;};-const insertPageview = async (pageview: TotalPayload) => {- try {- const formattedTimestamp = DateTime.fromISO(pageview.timestamp).toFormat(- "yyyy-MM-dd HH:mm:ss"- );- await clickhouse.insert({- table: "pageviews",- values: [- {- timestamp: formattedTimestamp,- session_id: pageview.sessionId,- user_id: pageview.userId,- hostname: pageview.hostname || "",- pathname: pageview.pathname || "",- querystring: pageview.querystring || "",- page_title: pageview.page_title || "",- referrer: pageview.referrer || "",- browser: pageview.ua.browser.name,- operating_system: pageview.ua.os.name,- language: pageview.language || "",- screen_width: pageview.screenWidth || 0,- screen_height: pageview.screenHeight || 0,- device_type: getDeviceType(- pageview.screenWidth,- pageview.screenHeight- ),- },- ],- format: "JSONEachRow",- });- return true;- } catch (error) {- console.error("Error inserting pageview:", error);- return false;- }-};+// const insertPageview = async (pageview: TotalPayload) => {+// try {+// const formattedTimestamp = DateTime.fromISO(pageview.timestamp).toFormat(+// "yyyy-MM-dd HH:mm:ss"+// );+// await clickhouse.insert({+// table: "pageviews",+// values: [+// {+// timestamp: formattedTimestamp,+// session_id: pageview.sessionId,+// user_id: pageview.userId,+// hostname: pageview.hostname || "",+// pathname: pageview.pathname || "",+// querystring: pageview.querystring || "",+// page_title: pageview.page_title || "",+// referrer: pageview.referrer || "",+// browser: pageview.ua.browser.name,+// operating_system: pageview.ua.os.name,+// language: pageview.language || "",+// screen_width: pageview.screenWidth || 0,+// screen_height: pageview.screenHeight || 0,+// device_type: getDeviceType(+// pageview.screenWidth,+// pageview.screenHeight+// ),+// },+// ],+// format: "JSONEachRow",+// });+// return true;+// } catch (error) {+// console.error("Error inserting pageview:", error);+// return false;+// }+// };const getExistingSession = async (userId: string): Promise<Pageview | null> => {const [existingSession] = await sql<Pageview[]>`@@ -82,7 +83,11 @@ const updateSession = async (pageviews: 1,entry_page: pageview.pathname || "",// exit_page: pageview.pathname,- device_type: getDeviceType(pageview.screenWidth, pageview.screenHeight),+ device_type: getDeviceType(+ pageview.screenWidth,+ pageview.screenHeight,+ pageview.ua.os.name+ ),screen_width: pageview.screenWidth || 0,screen_height: pageview.screenHeight || 0,browser: pageview.ua.browser.name || "",@@ -103,6 +108,7 @@ export async function trackPageView(const ipAddress = getIpAddress(request);const userId = getUserId(ipAddress, userAgent);const existingSession = await getExistingSession(userId);+const payload = {...request.body,ipAddress: ipAddress,@@ -111,6 +117,8 @@ export async function trackPageView(userId: userId,sessionId: existingSession?.session_id || crypto.randomUUID(),};+ console.info(payload.ua.device);+ console.info(payload.ua.os);pageviewQueue.add(payload);// insertPageview(payload);diff --git a/server/src/utils.ts b/server/src/utils.tsindex 51540e80b..afbc93099 100644--- a/server/src/utils.ts+++ b/server/src/utils.ts@@ -8,16 +8,115 @@ export function getUserId(ip: string, userAgent: string) {.digest("hex");}+export const desktopOS = new Set([+ "Windows",+ "macOS",+ "Linux",+ "Ubuntu",+ "Fedora",+ "Debian",+ "Mint",+ "Arch",+ "CentOS",+ "elementary OS",+ "Gentoo",+ "Kubuntu",+ "Manjaro",+ "RedHat",+ "SUSE",+ "Slackware",+ "Deepin",+ "FreeBSD",+ "OpenBSD",+ "NetBSD",+ "GhostBSD",+ "PC-BSD",+ "Solaris",+ "AIX",+ "HP-UX",+ "OS/2",+ "BeOS",+ "Haiku",+ "Amiga OS",+ "Morph OS",+ "SerenityOS",+]);++export const mobileOS = new Set([+ "iOS",+ "Android",+ "Windows Phone",+ "Windows Mobile",+ "BlackBerry",+ "Symbian",+ "Firefox OS",+ "Ubuntu Touch",+ "Sailfish",+ "Tizen",+ "KaiOS",+ "HarmonyOS",+ "OpenHarmony",+ "watchOS",+ "Android-x86",+ "RIM Tablet OS",+ "Bada",+ "WebOS",+ "Maemo",+ "MeeGo",+]);++export const tvOS = new Set([+ "Chromecast",+ "Chromecast Android",+ "Chromecast Fuchsia",+ "Chromecast Linux",+ "Chromecast SmartSpeaker",+ "NetTV",+ "NetRange",+]);++export const gamingOS = new Set(["PlayStation", "Xbox", "Nintendo"]);++export const otherOS = new Set([+ "Fuchsia",+ "GNU",+ "Hurd",+ "Plan9",+ "Contiki",+ "Pico",+ "Minix",+ "Unix",+ "OpenVMS",+ "RISC OS",+ "QNX",+ "Series40",+ "PCLinuxOS",+ "Linpus",+ "Linspire",+ "Mageia",+ "Mandriva",+ "Raspbian",+ "Sabayon",+ "VectorLinux",+ "Zenwalk",+ "DragonFly",+]);+export function getDeviceType(screenWidth: number,- screenHeight: number+ screenHeight: number,+ os = ""): string {- if (screenWidth < 768) {+ if (desktopOS.has(os)) {+ return "Desktop";+ } else if (mobileOS.has(os)) {return "Mobile";- } else if (screenWidth >= 768 && screenWidth < 1024) {- return "Tablet";+ } else if (tvOS.has(os)) {+ return "TV";+ } else if (gamingOS.has(os)) {+ return "Gaming";} else {- return "Desktop";+ return "Other";}}