2022-12-13 04:45:38 +01:00
|
|
|
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';
|
|
|
|
import cache from 'lib/cache';
|
|
|
|
|
|
|
|
export async function saveEvent(args: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
url: string;
|
|
|
|
referrer?: string;
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle?: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
eventName?: string;
|
|
|
|
hostname?: string;
|
|
|
|
browser?: string;
|
|
|
|
os?: string;
|
|
|
|
device?: string;
|
|
|
|
screen?: string;
|
|
|
|
language?: string;
|
|
|
|
country?: string;
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1?: string;
|
|
|
|
subdivision2?: string;
|
|
|
|
city?: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
}) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(data: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
url: string;
|
|
|
|
referrer?: string;
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle?: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
eventName?: string;
|
|
|
|
}) {
|
2023-03-02 01:42:47 +01:00
|
|
|
const { websiteId, id: sessionId, url, eventName, referrer, pageTitle } = data;
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return prisma.client.websiteEvent.create({
|
2023-02-20 18:04:20 +01:00
|
|
|
data: {
|
|
|
|
id: uuid(),
|
|
|
|
websiteId,
|
|
|
|
sessionId,
|
|
|
|
url: url?.substring(0, URL_LENGTH),
|
|
|
|
referrer: referrer?.substring(0, URL_LENGTH),
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle: pageTitle,
|
2023-02-20 18:04:20 +01:00
|
|
|
eventType: EVENT_TYPE.customEvent,
|
|
|
|
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
|
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:04:20 +01:00
|
|
|
async function clickhouseQuery(data) {
|
|
|
|
const {
|
|
|
|
websiteId,
|
|
|
|
id: sessionId,
|
|
|
|
url,
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle,
|
2023-02-20 18:04:20 +01:00
|
|
|
eventName,
|
|
|
|
country,
|
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
|
|
|
...args
|
|
|
|
} = data;
|
2022-12-13 04:45:38 +01:00
|
|
|
const { getDateFormat, sendMessage } = kafka;
|
|
|
|
const website = await cache.fetchWebsite(websiteId);
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
website_id: websiteId,
|
|
|
|
session_id: sessionId,
|
|
|
|
event_id: uuid(),
|
2023-02-20 18:04:20 +01:00
|
|
|
country: country ? country : null,
|
|
|
|
subdivision1: subdivision1 ? subdivision1 : null,
|
|
|
|
subdivision2: subdivision2 ? subdivision2 : null,
|
|
|
|
city: city ? city : null,
|
2022-12-13 04:45:38 +01:00
|
|
|
url: url?.substring(0, URL_LENGTH),
|
2023-03-01 19:53:57 +01:00
|
|
|
page_title: pageTitle,
|
2022-12-13 04:45:38 +01:00
|
|
|
event_type: EVENT_TYPE.customEvent,
|
|
|
|
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
|
2023-03-02 01:42:47 +01:00
|
|
|
rev_id: website?.revId || 0,
|
2022-12-13 04:45:38 +01:00
|
|
|
created_at: getDateFormat(new Date()),
|
|
|
|
...args,
|
|
|
|
};
|
|
|
|
|
|
|
|
await sendMessage(message, 'event');
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|