From d85b2be5f8a4dc6d360faf6a53126b11c368a62b Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Tue, 22 Nov 2022 15:06:52 -0800 Subject: [PATCH] Add event type to event. --- pages/api/collect.ts | 16 ++++++++++++---- pages/api/websites/[id]/index.ts | 4 ++-- queries/analytics/event/saveEvent.ts | 5 +++-- queries/analytics/pageview/savePageView.ts | 5 +++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pages/api/collect.ts b/pages/api/collect.ts index 2aa2bde1..254689fe 100644 --- a/pages/api/collect.ts +++ b/pages/api/collect.ts @@ -29,6 +29,18 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => { return unauthorized(res); } + const { type, payload } = getJsonBody(req); + + const { referrer, event_name: eventName, event_data: eventData } = payload; + let { url } = payload; + + // Validate eventData is JSON + const valid = eventData && typeof eventData === 'object' && !Array.isArray(eventData); + + if (!valid) { + return badRequest(res, 'Event Data must be in the form of a JSON Object.'); + } + const ignoreIps = process.env.IGNORE_IP; const ignoreHostnames = process.env.IGNORE_HOSTNAME; @@ -75,10 +87,6 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => { const session = req.session; - const { type, payload } = getJsonBody(req); - - let { url, referrer, event_name: eventName, event_data: eventData } = payload; - if (process.env.REMOVE_TRAILING_SLASH) { url = url.replace(/\/$/, ''); } diff --git a/pages/api/websites/[id]/index.ts b/pages/api/websites/[id]/index.ts index e525f237..356feb0a 100644 --- a/pages/api/websites/[id]/index.ts +++ b/pages/api/websites/[id]/index.ts @@ -4,7 +4,7 @@ import { allowQuery } from 'lib/auth'; import { UmamiApi } from 'lib/constants'; import { useAuth, useCors } from 'lib/middleware'; import { NextApiResponse } from 'next'; -import { methodNotAllowed, ok, serverError, unauthorized, badRequest } from 'next-basics'; +import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics'; import { deleteWebsite, getWebsite, updateWebsite } from 'queries'; export interface WebsiteRequestQuery { @@ -19,7 +19,7 @@ export interface WebsiteRequestBody { export default async ( req: NextApiRequestQueryBody, - res: NextApiResponse, + res: NextApiResponse, ) => { await useCors(req, res); await useAuth(req, res); diff --git a/queries/analytics/event/saveEvent.ts b/queries/analytics/event/saveEvent.ts index 521f6c28..c2a92d10 100644 --- a/queries/analytics/event/saveEvent.ts +++ b/queries/analytics/event/saveEvent.ts @@ -72,11 +72,12 @@ async function clickhouseQuery(data: { const { getDateFormat, sendMessage } = kafka; const website = await cache.fetchWebsite(websiteId); - const params = { + const message = { website_id: websiteId, session_id: sessionId, event_id: uuid(), url: url?.substring(0, URL_LENGTH), + event_type: UmamiApi.EventType.Event, event_name: eventName?.substring(0, EVENT_NAME_LENGTH), event_data: eventData ? JSON.stringify(eventData) : null, rev_id: website?.revId || 0, @@ -85,7 +86,7 @@ async function clickhouseQuery(data: { ...args, }; - await sendMessage(params, 'event'); + await sendMessage(message, 'event'); return data; } diff --git a/queries/analytics/pageview/savePageView.ts b/queries/analytics/pageview/savePageView.ts index fd3f7e81..b73b8c0a 100644 --- a/queries/analytics/pageview/savePageView.ts +++ b/queries/analytics/pageview/savePageView.ts @@ -50,7 +50,7 @@ async function clickhouseQuery(data) { const website = await cache.fetchWebsite(websiteId); const { getDateFormat, sendMessage } = kafka; - const msg = { + const message = { session_id: sessionId, website_id: websiteId, url: url?.substring(0, URL_LENGTH), @@ -58,10 +58,11 @@ async function clickhouseQuery(data) { rev_id: website?.revId || 0, created_at: getDateFormat(new Date()), country: country ? country : null, + event_type: UmamiApi.EventType.Pageview, ...args, }; - await sendMessage(msg, 'event'); + await sendMessage(message, 'event'); return data; }