From 8f934c7e6cbf7a12af22fc2654febc256933e90b Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Fri, 29 Jul 2022 22:30:09 -0700 Subject: [PATCH] checkpoint --- components/metrics/EventsChart.js | 6 +-- components/metrics/RealtimeLog.js | 7 ++-- components/pages/TestConsole.js | 3 +- .../02_add_event_data/migration.sql | 3 +- db/mysql/schema.prisma | 33 +++++++++------- .../02_add_event_data/migration.sql | 4 +- lib/db.js | 8 ++-- pages/api/collect.js | 4 +- pages/api/website/[id]/events.js | 4 +- pages/api/website/[id]/metrics.js | 3 +- queries/analytics/event/getEventMetrics.js | 2 +- queries/analytics/event/getEvents.js | 2 +- queries/analytics/event/saveEvent.js | 39 ++++++++++--------- tracker/index.js | 18 ++++----- 14 files changed, 73 insertions(+), 63 deletions(-) diff --git a/components/metrics/EventsChart.js b/components/metrics/EventsChart.js index ef9a142f..50dc940d 100644 --- a/components/metrics/EventsChart.js +++ b/components/metrics/EventsChart.js @@ -12,7 +12,7 @@ export default function EventsChart({ websiteId, className, token }) { const [{ startDate, endDate, unit, modified }] = useDateRange(websiteId); const [timezone] = useTimezone(); const { - query: { url, eventType }, + query: { url, eventName }, } = usePageQuery(); const { data, loading } = useFetch( @@ -24,11 +24,11 @@ export default function EventsChart({ websiteId, className, token }) { unit, tz: timezone, url, - event_type: eventType, + event_name: eventName, token, }, }, - [modified, eventType], + [modified, eventName], ); const datasets = useMemo(() => { diff --git a/components/metrics/RealtimeLog.js b/components/metrics/RealtimeLog.js index f4e87834..278f64a4 100644 --- a/components/metrics/RealtimeLog.js +++ b/components/metrics/RealtimeLog.js @@ -92,8 +92,7 @@ export default function RealtimeLog({ data, websites, websiteId }) { } function getDetail({ - event_type, - event_value, + event_name, view_id, session_id, url, @@ -103,10 +102,10 @@ export default function RealtimeLog({ data, websites, websiteId }) { device, website_id, }) { - if (event_type) { + if (event_name) { return (
- {event_type} {event_value} + {event_name}
); } diff --git a/components/pages/TestConsole.js b/components/pages/TestConsole.js index 9f4d7ca2..8a2afa88 100644 --- a/components/pages/TestConsole.js +++ b/components/pages/TestConsole.js @@ -37,7 +37,8 @@ export default function TestConsole() { function handleClick() { window.umami('event (default)'); window.umami.trackView('/page-view', 'https://www.google.com'); - window.umami.trackEvent('event (custom)', 'custom-type'); + window.umami.trackEvent('event (custom)', null, 'custom-type'); + window.umami.trackEvent('event (custom)', { test: 'test-data' }, 'custom-data-type'); } return ( diff --git a/db/mysql/migrations/02_add_event_data/migration.sql b/db/mysql/migrations/02_add_event_data/migration.sql index 80ff8e26..d8bfa0b0 100644 --- a/db/mysql/migrations/02_add_event_data/migration.sql +++ b/db/mysql/migrations/02_add_event_data/migration.sql @@ -36,8 +36,7 @@ ALTER TABLE `website` RENAME INDEX `website_uuid` TO `website_website_uuid_key`; */ -- Populate event_name update `event` -set event_name = concat(event_value, "-", event_type) -where 1 = 1; +set event_name = event_value; -- Set event_name not null ALTER TABLE `event` diff --git a/db/mysql/schema.prisma b/db/mysql/schema.prisma index e2985142..3d920c53 100644 --- a/db/mysql/schema.prisma +++ b/db/mysql/schema.prisma @@ -9,7 +9,7 @@ datasource db { model account { user_id Int @id @default(autoincrement()) @db.UnsignedInt - username String @unique(map: "username") @db.VarChar(255) + username String @unique() @db.VarChar(255) password String @db.VarChar(60) is_admin Boolean @default(false) created_at DateTime? @default(now()) @db.Timestamp(0) @@ -18,21 +18,28 @@ model account { } model event { - event_id Int @id @default(autoincrement()) @db.UnsignedInt - website_id Int @db.UnsignedInt - session_id Int @db.UnsignedInt - created_at DateTime? @default(now()) @db.Timestamp(0) - url String @db.VarChar(500) - event_type String @db.VarChar(50) - event_value String @db.VarChar(50) - session session @relation(fields: [session_id], references: [session_id], onDelete: Cascade, onUpdate: NoAction, map: "event_ibfk_2") - website website @relation(fields: [website_id], references: [website_id], onDelete: Cascade, onUpdate: NoAction, map: "event_ibfk_1") + event_id Int @id @default(autoincrement()) @db.UnsignedInt + website_id Int @db.UnsignedInt + session_id Int @db.UnsignedInt + created_at DateTime? @default(now()) @db.Timestamp(0) + url String @db.VarChar(500) + event_name String @db.VarChar(50) + session session @relation(fields: [session_id], references: [session_id], onDelete: Cascade, onUpdate: NoAction, map: "event_ibfk_2") + website website @relation(fields: [website_id], references: [website_id], onDelete: Cascade, onUpdate: NoAction, map: "event_ibfk_1") + event_data event_data? @@index([created_at]) @@index([session_id]) @@index([website_id]) } +model event_data { + id Int @id @default(autoincrement()) @map("event_data_id") + eventId Int @unique @map("event_id") @db.UnsignedInt + eventData Json @map("event_data") + event event @relation(fields: [eventId], references: [event_id]) +} + model pageview { view_id Int @id @default(autoincrement()) @db.UnsignedInt website_id Int @db.UnsignedInt @@ -52,7 +59,7 @@ model pageview { model session { session_id Int @id @default(autoincrement()) @db.UnsignedInt - session_uuid String @unique(map: "session_uuid") @db.VarChar(36) + session_uuid String @unique() @db.VarChar(36) website_id Int @db.UnsignedInt created_at DateTime? @default(now()) @db.Timestamp(0) hostname String? @db.VarChar(100) @@ -72,11 +79,11 @@ model session { model website { website_id Int @id @default(autoincrement()) @db.UnsignedInt - website_uuid String @unique(map: "website_uuid") @db.VarChar(36) + website_uuid String @unique() @db.VarChar(36) user_id Int @db.UnsignedInt name String @db.VarChar(100) domain String? @db.VarChar(500) - share_id String? @unique(map: "share_id") @db.VarChar(64) + share_id String? @unique() @db.VarChar(64) created_at DateTime? @default(now()) @db.Timestamp(0) account account @relation(fields: [user_id], references: [user_id], onDelete: Cascade, onUpdate: NoAction, map: "website_ibfk_1") event event[] diff --git a/db/postgresql/migrations/02_add_event_data/migration.sql b/db/postgresql/migrations/02_add_event_data/migration.sql index 703bca38..0574abbd 100644 --- a/db/postgresql/migrations/02_add_event_data/migration.sql +++ b/db/postgresql/migrations/02_add_event_data/migration.sql @@ -1,5 +1,5 @@ -- AlterTable -ALTER TABLE "event" ADD COLUMN "event_name" VARCHAR(50); +ALTER TABLE event RENAME TO event_old; -- CreateTable CREATE TABLE "event_data" ( @@ -37,7 +37,7 @@ ALTER INDEX "website.website_uuid_unique" RENAME TO "website_website_uuid_key"; */ -- Populate event_name update event -set "event_name" = event.event_value || '-' || event.event_type; +set "event_name" = event.event_value; -- Set event_name not null ALTER TABLE "event" ALTER COLUMN "event_name" SET NOT NULL; diff --git a/lib/db.js b/lib/db.js index cb9237bb..1a5f8b2c 100644 --- a/lib/db.js +++ b/lib/db.js @@ -182,7 +182,7 @@ export function getFilterQuery(table, column, filters = {}, params = []) { } break; - case 'event_type': + case 'event_name': if (table === 'event') { arr.push(`and ${table}.${key}=$${params.length + 1}`); params.push(decodeURIComponent(filter)); @@ -212,17 +212,17 @@ export function getFilterQuery(table, column, filters = {}, params = []) { } export function parseFilters(table, column, filters = {}, params = [], sessionKey = 'session_id') { - const { domain, url, event_url, referrer, os, browser, device, country, event_type } = filters; + const { domain, url, event_url, referrer, os, browser, device, country, event_name } = filters; const pageviewFilters = { domain, url, referrer }; const sessionFilters = { os, browser, device, country }; - const eventFilters = { url: event_url, event_type }; + const eventFilters = { url: event_url, event_name }; return { pageviewFilters, sessionFilters, eventFilters, - event: { event_type }, + event: { event_name }, joinSession: os || browser || device || country ? `inner join session on ${table}.${sessionKey} = session.${sessionKey}` diff --git a/pages/api/collect.js b/pages/api/collect.js index 4ff533b9..92451016 100644 --- a/pages/api/collect.js +++ b/pages/api/collect.js @@ -65,7 +65,7 @@ export default async (req, res) => { const { type, payload } = getJsonBody(req); - let { url, referrer, event_type, event_value } = payload; + let { url, referrer, event_name, event_data } = payload; if (process.env.REMOVE_TRAILING_SLASH) { url = removeTrailingSlash(url); @@ -74,7 +74,7 @@ export default async (req, res) => { if (type === 'pageview') { await savePageView(website_id, { session_id, session_uuid, url, referrer }); } else if (type === 'event') { - await saveEvent(website_id, { session_id, session_uuid, url, event_type, event_value }); + await saveEvent(website_id, { session_id, session_uuid, url, event_name, event_data }); } else { return badRequest(res); } diff --git a/pages/api/website/[id]/events.js b/pages/api/website/[id]/events.js index e4fc1e2a..ab4ddc7d 100644 --- a/pages/api/website/[id]/events.js +++ b/pages/api/website/[id]/events.js @@ -14,7 +14,7 @@ export default async (req, res) => { return unauthorized(res); } - const { id, start_at, end_at, unit, tz, url, event_type } = req.query; + const { id, start_at, end_at, unit, tz, url, event_name } = req.query; if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) { return badRequest(res); @@ -26,7 +26,7 @@ export default async (req, res) => { const events = await getEventMetrics(websiteId, startDate, endDate, tz, unit, { url, - event_type, + event_name, }); return ok(res, events); diff --git a/pages/api/website/[id]/metrics.js b/pages/api/website/[id]/metrics.js index b12566fd..50f568d3 100644 --- a/pages/api/website/[id]/metrics.js +++ b/pages/api/website/[id]/metrics.js @@ -22,7 +22,8 @@ function getTable(type) { function getColumn(type) { if (type === 'event') { - return `concat(event_type, '\t', event_value)`; + //return `concat(event_type, '\t', event_value)`; + return `event_name`; } return type; } diff --git a/queries/analytics/event/getEventMetrics.js b/queries/analytics/event/getEventMetrics.js index 73f7e469..1dbe10f4 100644 --- a/queries/analytics/event/getEventMetrics.js +++ b/queries/analytics/event/getEventMetrics.js @@ -29,7 +29,7 @@ async function relationalQuery( return rawQuery( ` select - event_value x, + event_name x, ${getDateQuery('created_at', unit, timezone)} t, count(*) y from event diff --git a/queries/analytics/event/getEvents.js b/queries/analytics/event/getEvents.js index fc5c0aeb..7969877a 100644 --- a/queries/analytics/event/getEvents.js +++ b/queries/analytics/event/getEvents.js @@ -40,7 +40,7 @@ function clickhouseQuery(websites, start_at) { session_id, created_at, url, - event_type + event_name from event where website_id in (${websites.join[',']} and created_at >= ${getDateFormatClickhouse(start_at)}) diff --git a/queries/analytics/event/saveEvent.js b/queries/analytics/event/saveEvent.js index 41c068c3..3e71c61c 100644 --- a/queries/analytics/event/saveEvent.js +++ b/queries/analytics/event/saveEvent.js @@ -14,33 +14,36 @@ export async function saveEvent(...args) { }); } -async function relationalQuery(website_id, { session_id, url, event_type, event_value }) { +async function relationalQuery(website_id, { session_id, url, event_name, event_data }) { + const data = { + website_id, + session_id, + url: url?.substr(0, URL_LENGTH), + event_name: event_name?.substr(0, 50), + }; + + if (event_data) { + data.eventData = { + create: { + eventData: event_data, + }, + }; + } + return runQuery( prisma.event.create({ - data: { - website_id, - session_id, - url: url?.substr(0, URL_LENGTH), - event_type: event_type?.substr(0, 50), - event_value: event_value?.substr(0, 50), - }, + data, }), ); } -async function clickhouseQuery(website_id, { session_uuid, url, event_type, event_value }) { - const params = [ - website_id, - session_uuid, - url?.substr(0, URL_LENGTH), - event_type?.substr(0, 50), - event_value?.substr(0, 50), - ]; +async function clickhouseQuery(website_id, { session_uuid, url, event_name }) { + const params = [website_id, session_uuid, url?.substr(0, URL_LENGTH), event_name?.substr(0, 50)]; return rawQueryClickhouse( ` - insert into umami_dev.event (created_at, website_id, session_uuid, url, event_type, event_value) - values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4, $5);`, + insert into umami_dev.event (created_at, website_id, session_uuid, url, event_name) + values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4);`, params, ); } diff --git a/tracker/index.js b/tracker/index.js index f6329633..f0584288 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -97,25 +97,24 @@ import { removeTrailingSlash } from '../lib/url'; ); }; - const trackEvent = (event_value, event_type = 'custom', url = currentUrl, uuid = website) => { + const trackEvent = (event_name = 'custom', event_data, url = currentUrl, uuid = website) => { collect( 'event', assign(getPayload(), { website: uuid, url, - event_type, - event_value, + event_name, + event_data, }), ); }; /* Handle events */ - const sendEvent = (value, type) => { + const sendEvent = name => { const payload = getPayload(); - payload.event_type = type; - payload.event_value = value; + payload.event_name = name; const data = JSON.stringify({ type: 'event', @@ -138,14 +137,15 @@ import { removeTrailingSlash } from '../lib/url'; (element.getAttribute('class') || '').split(' ').forEach(className => { if (!eventClass.test(className)) return; - const [, type, value] = className.split('--'); + const [, type, name] = className.split('--'); + const listener = listeners[className] ? listeners[className] : (listeners[className] = () => { if (element.tagName === 'A') { - sendEvent(value, type); + sendEvent(name); } else { - trackEvent(value, type); + trackEvent(name); } });