add tag migrations and update send / saveEvent

This commit is contained in:
Francis Cao 2024-10-15 11:29:27 -07:00
parent 6ce9a8cef4
commit bffb98cd51
7 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,78 @@
-- add tag column
ALTER TABLE umami.website_event ADD COLUMN "tag" String AFTER "event_name";
ALTER TABLE umami.website_event_stats_hourly ADD COLUMN "tag" String AFTER "max_time";
-- update materialized view
DROP TABLE umami.website_event_stats_hourly_mv;
CREATE MATERIALIZED VIEW umami.website_event_stats_hourly_mv
TO umami.website_event_stats_hourly
AS
SELECT
website_id,
session_id,
visit_id,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
city,
entry_url,
exit_url,
url_paths as url_path,
url_query,
referrer_domain,
page_title,
event_type,
event_name,
views,
min_time,
max_time,
tag,
timestamp as created_at
FROM (SELECT
website_id,
session_id,
visit_id,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
city,
argMinState(url_path, created_at) entry_url,
argMaxState(url_path, created_at) exit_url,
arrayFilter(x -> x != '', groupArray(url_path)) as url_paths,
arrayFilter(x -> x != '', groupArray(url_query)) url_query,
arrayFilter(x -> x != '', groupArray(referrer_domain)) referrer_domain,
arrayFilter(x -> x != '', groupArray(page_title)) page_title,
event_type,
if(event_type = 2, groupArray(event_name), []) event_name,
sumIf(1, event_type = 1) views,
min(created_at) min_time,
max(created_at) max_time,
tag,
toStartOfHour(created_at) timestamp
FROM umami.website_event
GROUP BY website_id,
session_id,
visit_id,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
city,
event_type,
tag,
timestamp);

View File

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE `website_event` ADD COLUMN `tag` VARCHAR(50) NULL;
-- CreateIndex
CREATE INDEX `website_event_website_id_created_at_tag_idx` ON `website_event`(`website_id`, `created_at`, `tag`);

View File

@ -102,6 +102,7 @@ model WebsiteEvent {
pageTitle String? @map("page_title") @db.VarChar(500)
eventType Int @default(1) @map("event_type") @db.UnsignedInt
eventName String? @map("event_name") @db.VarChar(50)
tag String? @db.VarChar(50)
eventData EventData[]
session Session @relation(fields: [sessionId], references: [id])
@ -116,6 +117,7 @@ model WebsiteEvent {
@@index([websiteId, createdAt, referrerDomain])
@@index([websiteId, createdAt, pageTitle])
@@index([websiteId, createdAt, eventName])
@@index([websiteId, createdAt, tag])
@@index([websiteId, sessionId, createdAt])
@@index([websiteId, visitId, createdAt])
@@map("website_event")

View File

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "website_event" ADD COLUMN "tag" VARCHAR(50);
-- CreateIndex
CREATE INDEX "website_event_website_id_created_at_tag_idx" ON "website_event"("website_id", "created_at", "tag");

View File

@ -102,6 +102,7 @@ model WebsiteEvent {
pageTitle String? @map("page_title") @db.VarChar(500)
eventType Int @default(1) @map("event_type") @db.Integer
eventName String? @map("event_name") @db.VarChar(50)
tag String? @db.VarChar(50)
eventData EventData[]
session Session @relation(fields: [sessionId], references: [id])
@ -111,11 +112,13 @@ model WebsiteEvent {
@@index([visitId])
@@index([websiteId])
@@index([websiteId, createdAt])
@@index([websiteId, createdAt, urlPath])
@@index([websiteId, createdAt, urlQuery])
@@index([websiteId, createdAt, referrerDomain])
@@index([websiteId, createdAt, pageTitle])
@@index([websiteId, createdAt, eventName])
@@index([websiteId, createdAt, tag])
@@index([websiteId, sessionId, createdAt])
@@index([websiteId, visitId, createdAt])
@@map("website_event")

View File

@ -96,7 +96,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
}
const { type, payload } = req.body;
const { url, referrer, name: eventName, data, title } = payload;
const { url, referrer, name: eventName, data, title, tag } = payload;
const pageTitle = safeDecodeURI(title);
await useSession(req, res);
@ -143,6 +143,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
eventData: data,
...session,
sessionId: session.id,
tag,
});
} else if (type === COLLECTION_TYPE.identify) {
if (!data) {

View File

@ -28,6 +28,7 @@ export async function saveEvent(args: {
subdivision1?: string;
subdivision2?: string;
city?: string;
tag?: string;
}) {
return runQuery({
[PRISMA]: () => relationalQuery(args),
@ -47,6 +48,7 @@ async function relationalQuery(data: {
pageTitle?: string;
eventName?: string;
eventData?: any;
tag?: string;
}) {
const {
websiteId,
@ -60,6 +62,7 @@ async function relationalQuery(data: {
eventName,
eventData,
pageTitle,
tag,
} = data;
const websiteEventId = uuid();
@ -77,6 +80,7 @@ async function relationalQuery(data: {
pageTitle: pageTitle?.substring(0, PAGE_TITLE_LENGTH),
eventType: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
eventName: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
tag,
},
});
@ -116,6 +120,7 @@ async function clickhouseQuery(data: {
subdivision1?: string;
subdivision2?: string;
city?: string;
tag?: string;
}) {
const {
websiteId,
@ -133,6 +138,7 @@ async function clickhouseQuery(data: {
subdivision1,
subdivision2,
city,
tag,
...args
} = data;
const { insert, getUTCString } = clickhouse;
@ -163,6 +169,7 @@ async function clickhouseQuery(data: {
page_title: pageTitle?.substring(0, PAGE_TITLE_LENGTH),
event_type: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
tag: tag,
created_at: createdAt,
};