Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Umami

Edward Cuid0223b81420d
diff --git a/server/src/services/import/mappings/umami.ts b/server/src/services/import/mappings/umami.ts
index cb5549b73..f15ca5657 100644
--- a/server/src/services/import/mappings/umami.ts
+++ b/server/src/services/import/mappings/umami.ts
@@ -1,6 +1,7 @@
import { clearSelfReferrer, getAllUrlParams } from "../../tracker/utils.js";
import { getChannel } from "../../tracker/getChannel.js";
import { RybbitEvent } from "./rybbit.js";
+import { z } from "zod";
export interface UmamiEvent {
website_id: string; // Ignore
@@ -25,8 +26,8 @@ export interface UmamiEvent {
utm_campaign: string; // Ignore, part of url_query
utm_content: string; // Ignore, part of url_query
utm_term: string; // Ignore, part of url_query
- referrer_path: string; // Ignore
- referrer_query: string; // Ignore
+ referrer_path: string;
+ referrer_query: string;
referrer_domain: string;
page_title: string;
@@ -85,40 +86,122 @@ export const umamiHeaders = [
];
export class UmamiImportMapper {
- static transform(rows: UmamiEvent[], site: string, importId: string): RybbitEvent[] {
- return rows.map(row => {
- const querystring = row.url_query ? `?${row.url_query}` : "";
- const referrer = row.referrer_domain ? `https://${row.referrer_domain}` : "";
- const [screenWidth, screenHeight] = /^\d+x\d+$/.test(row.screen) ? row.screen.split("x") : ["0", "0"];
-
- // TODO: Handle empty values in Umami data for required columns (e.g., site_id, timestamp, etc.)
- return {
+ private static readonly browserMap: Record<string, string> = {
+ chrome: "Chrome",
+ opera: "Opera",
+ crios: "Mobile Chrome",
+ firefox: "Firefox",
+ facebook: "Facebook",
+ safari: "Safari",
+ ios: "Mobile Safari",
+ "ios-webview": "Mobile Safari",
+ "edge-chromium": "Edge",
+ samsung: "Samsung Internet",
+ yandexbrowser: "Yandex",
+ "edge-ios": "Edge",
+ "chromium-webview": "Chrome WebView",
+ fxios: "Mobile Firefox",
+ edge: "Edge",
+ };
+
+ private static readonly browserSchema = z.string().transform((browser) => {
+ const key = browser.toLowerCase();
+ return UmamiImportMapper.browserMap[key] ?? browser;
+ });
+
+ private static readonly osMap: Record<string, string> = {
+ "windows 10": "Windows",
+ "windows 7": "Windows",
+ "windows server 2003": "Windows",
+ "mac os": "macOS",
+ "ios": "iOS",
+ "android os": "Android",
+ "linux": "Linux",
+ "chrome os": "Chrome OS",
+ };
+
+ private static readonly osSchema = z.string().transform((os) => {
+ const key = os.toLowerCase();
+ return UmamiImportMapper.osMap[key] ?? os;
+ });
+
+ private static readonly deviceMap: Record<string, "Desktop" | "Mobile"> = {
+ laptop: "Desktop",
+ desktop: "Desktop",
+ mobile: "Mobile",
+ tablet: "Mobile",
+ };
+
+ private static readonly deviceSchema = z.string().transform((device) => {
+ const key = device.toLowerCase();
+ return UmamiImportMapper.deviceMap[key] ?? device;
+ });
+
+ private static readonly umamiEventSchema = z.object({
+ session_id: z.string().uuid(),
+
+ hostname: z.string().max(253),
+ browser: UmamiImportMapper.browserSchema,
+ os: UmamiImportMapper.osSchema,
+ device: UmamiImportMapper.deviceSchema,
+ screen: z.string().regex(/^\d{1,5}x\d{1,5}$/).or(z.literal("")),
+ language: z.string().max(35),
+ country: z.string().regex(/^[A-Z]{2}$/).or(z.literal("")),
+ region: z.string().regex(/^[A-Z]{2}-[A-Z0-9]{1,3}$/).or(z.literal("")),
+ city: z.string().max(60),
+
+ url_path: z.string().max(2048),
+ url_query: z.string().max(2048).transform((querystring) => querystring ? `?${querystring}` : ""),
+ referrer_path: z.string().max(2048),
+ referrer_query: z.string().max(2048).transform((querystring) => querystring ? `?${querystring}` : ""),
+ referrer_domain: z.string().max(253).transform((url) => url ? `https://${url}` : ""),
+ page_title: z.string().max(512),
+
+ event_type: z.enum(["1", "2"]),
+ event_name: z.string().max(256),
+ distinct_id: z.string().max(64),
+ created_at: z.string().regex(/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) ([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/),
+ });
+
+ static transform(events: UmamiEvent[], site: string, importId: string): RybbitEvent[] {
+ return events.reduce<RybbitEvent[]>((acc, event) => {
+ const parsed = UmamiImportMapper.umamiEventSchema.safeParse(event);
+ if (!parsed.success) {
+ console.error(`Invalid Umami event for site ${site} during import ${importId}:`, parsed.error.flatten());
+ return acc;
+ }
+
+ const data = parsed.data;
+ const referrer = data.referrer_domain + data.referrer_path + data.referrer_query;
+ const [screenWidth, screenHeight] = data.screen ? data.screen.split("x") : ["0", "0"];
+
+ acc.push({
site_id: Number(site),
- timestamp: row.created_at,
- session_id: row.session_id,
- user_id: row.distinct_id,
- hostname: row.hostname,
- pathname: row.url_path,
- querystring: querystring,
- url_parameters: getAllUrlParams(row.url_query),
- page_title: row.page_title,
- referrer: clearSelfReferrer(referrer, row.hostname.replace(/^www\./, "")),
- channel: getChannel(referrer, querystring, row.hostname),
- browser: row.browser ? row.browser.charAt(0).toUpperCase() + row.browser.slice(1) : "",
+ timestamp: data.created_at,
+ session_id: data.session_id,
+ user_id: data.distinct_id,
+ hostname: data.hostname,
+ pathname: data.url_path,
+ querystring: data.url_query,
+ url_parameters: getAllUrlParams(data.url_query),
+ page_title: data.page_title,
+ referrer: clearSelfReferrer(referrer, data.hostname.replace(/^www\./, "")),
+ channel: getChannel(referrer, data.referrer_query, data.hostname),
+ browser: data.browser,
browser_version: "",
- operating_system: row.os ? row.os.trim().split(/\s+/)[0] : "",
- operating_system_version: row.os === "Windows 10" ? "10" : "",
- language: row.language,
- country: row.country,
- region: row.region,
- city: row.city,
+ operating_system: data.os,
+ operating_system_version: event.os === "Windows 10" ? "10" : event.os === "Windows 7" ? "7" : "",
+ language: data.language,
+ country: data.country,
+ region: data.region,
+ city: data.city,
lat: 0,
lon: 0,
screen_width: parseInt(screenWidth, 10),
screen_height: parseInt(screenHeight, 10),
- device_type: row.device ? row.device.charAt(0).toUpperCase() + row.device.slice(1) : "",
- type: row.event_type === 1 ? "pageview" : "custom_event",
- event_name: row.event_type === 1 ? "" : row.event_name,
+ device_type: data.device,
+ type: data.event_type === "1" ? "pageview" : "custom_event",
+ event_name: data.event_type === "1" ? "" : data.event_name,
props: JSON.parse("{}"),
lcp: null,
cls: null,
@@ -126,7 +209,9 @@ export class UmamiImportMapper {
fcp: null,
ttfb: null,
import_id: importId,
- };
- });
+ });
+
+ return acc;
+ }, []);
}
}