Add event type to event.

This commit is contained in:
Brian Cao 2022-11-22 15:06:52 -08:00
parent c90194dad4
commit d85b2be5f8
4 changed files with 20 additions and 10 deletions

View File

@ -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(/\/$/, '');
}

View File

@ -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<WebsiteRequestQuery, WebsiteRequestBody>,
res: NextApiResponse<Website | any>,
res: NextApiResponse<Website>,
) => {
await useCors(req, res);
await useAuth(req, res);

View File

@ -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;
}

View File

@ -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;
}