umami/queries/analytics/event/saveEvent.ts

171 lines
4.1 KiB
TypeScript
Raw Normal View History

import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka';
import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto';
2023-03-26 13:15:08 +02:00
import { saveEventData } from 'queries/analytics/eventData/saveEventData';
export async function saveEvent(args: {
2023-03-26 13:15:08 +02:00
sessionId: string;
websiteId: string;
2023-03-15 02:59:19 +01:00
urlPath: string;
urlQuery?: string;
2023-03-29 01:34:14 +02:00
referrerPath?: string;
referrerQuery?: string;
referrerDomain?: string;
2023-03-01 19:53:57 +01:00
pageTitle?: string;
eventName?: string;
eventData?: any;
hostname?: string;
browser?: string;
os?: string;
device?: string;
screen?: string;
language?: string;
country?: string;
subdivision1?: string;
subdivision2?: string;
city?: string;
}) {
return runQuery({
[PRISMA]: () => relationalQuery(args),
[CLICKHOUSE]: () => clickhouseQuery(args),
});
}
async function relationalQuery(data: {
2023-03-26 13:15:08 +02:00
sessionId: string;
websiteId: string;
2023-03-15 02:59:19 +01:00
urlPath: string;
urlQuery?: string;
2023-03-29 01:34:14 +02:00
referrerPath?: string;
referrerQuery?: string;
referrerDomain?: string;
2023-03-01 19:53:57 +01:00
pageTitle?: string;
eventName?: string;
eventData?: any;
}) {
2023-03-29 01:34:14 +02:00
const {
websiteId,
sessionId,
urlPath,
urlQuery,
referrerPath,
referrerQuery,
referrerDomain,
eventName,
eventData,
pageTitle,
} = data;
const websiteEventId = uuid();
const websiteEvent = prisma.client.websiteEvent.create({
data: {
id: websiteEventId,
websiteId,
sessionId,
2023-03-15 02:59:19 +01:00
urlPath: urlPath?.substring(0, URL_LENGTH),
urlQuery: urlQuery?.substring(0, URL_LENGTH),
2023-03-29 01:34:14 +02:00
referrerPath: referrerPath?.substring(0, URL_LENGTH),
referrerQuery: referrerQuery?.substring(0, URL_LENGTH),
referrerDomain: referrerDomain?.substring(0, URL_LENGTH),
2023-03-26 13:15:08 +02:00
pageTitle,
eventType: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
eventName: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
},
});
if (eventData) {
await saveEventData({
websiteId,
sessionId,
eventId: websiteEventId,
urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,
});
}
return websiteEvent;
}
async function clickhouseQuery(data: {
2023-03-26 13:15:08 +02:00
sessionId: string;
websiteId: string;
2023-03-15 02:59:19 +01:00
urlPath: string;
urlQuery?: string;
2023-03-29 01:34:14 +02:00
referrerPath?: string;
referrerQuery?: string;
referrerDomain?: string;
pageTitle?: string;
eventName?: string;
eventData?: any;
hostname?: string;
browser?: string;
os?: string;
device?: string;
screen?: string;
language?: string;
country?: string;
subdivision1?: string;
subdivision2?: string;
city?: string;
}) {
const {
websiteId,
2023-03-26 13:15:08 +02:00
sessionId,
2023-03-15 02:59:19 +01:00
urlPath,
urlQuery,
2023-03-29 01:34:14 +02:00
referrerPath,
referrerQuery,
referrerDomain,
2023-03-01 19:53:57 +01:00
pageTitle,
eventName,
eventData,
country,
subdivision1,
subdivision2,
city,
2023-03-29 20:06:12 +02:00
...args
} = data;
const { getDateFormat, sendMessage } = kafka;
const eventId = uuid();
const createdAt = getDateFormat(new Date());
const message = {
website_id: websiteId,
session_id: sessionId,
event_id: eventId,
country: country ? country : null,
2023-04-14 07:40:48 +02:00
subdivision1: subdivision1 ? country.concat('-', subdivision1) : null,
subdivision2: subdivision2 ? subdivision2 : null,
city: city ? city : null,
url_path: urlPath?.substring(0, URL_LENGTH),
url_query: urlQuery?.substring(0, URL_LENGTH),
2023-03-29 01:34:14 +02:00
referrer_path: referrerPath?.substring(0, URL_LENGTH),
referrer_query: referrerQuery?.substring(0, URL_LENGTH),
referrer_domain: referrerDomain?.substring(0, URL_LENGTH),
2023-03-01 19:53:57 +01:00
page_title: pageTitle,
2023-03-26 13:15:08 +02:00
event_type: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
created_at: createdAt,
2023-03-29 20:06:12 +02:00
...args,
};
await sendMessage(message, 'event');
if (eventData) {
await saveEventData({
websiteId,
sessionId,
eventId,
urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,
createdAt,
});
}
return data;
}