Allow populating event's createdAt on the send endpoint

This commit is contained in:
David Ventura 2025-01-06 13:46:38 +01:00
parent 9a87442870
commit 30b28793cf
2 changed files with 16 additions and 7 deletions

View file

@ -26,6 +26,7 @@ const schema = z.object({
tag: z.string().max(50).optional(), tag: z.string().max(50).optional(),
ip: z.string().ip().optional(), ip: z.string().ip().optional(),
userAgent: z.string().optional(), userAgent: z.string().optional(),
createdAt: yup.number().optional(),
}), }),
}); });
@ -55,6 +56,7 @@ export async function POST(request: Request) {
data, data,
title, title,
tag, tag,
reqCreatedAt,
} = payload; } = payload;
// Cache check // Cache check
@ -119,14 +121,14 @@ export async function POST(request: Request) {
} }
// Visit info // Visit info
const now = Math.floor(new Date().getTime() / 1000); const createdAt = Math.floor((reqCreatedAt || new Date()).getTime() / 1000);
let visitId = cache?.visitId || uuid(sessionId, visitSalt()); let visitId = cache?.visitId || uuid(sessionId, visitSalt());
let iat = cache?.iat || now; let iat = cache?.iat || createdAt;
// Expire visit after 30 minutes // Expire visit after 30 minutes
if (now - iat > 1800) { if (createdAt - iat > 1800) {
visitId = uuid(sessionId, visitSalt()); visitId = uuid(sessionId, visitSalt());
iat = now; iat = createdAt;
} }
if (type === COLLECTION_TYPE.event) { if (type === COLLECTION_TYPE.event) {
@ -179,6 +181,7 @@ export async function POST(request: Request) {
subdivision2, subdivision2,
city, city,
tag, tag,
createdAt,
}); });
} }

View file

@ -29,6 +29,7 @@ export async function saveEvent(args: {
subdivision2?: string; subdivision2?: string;
city?: string; city?: string;
tag?: string; tag?: string;
createdAt?: Date;
}) { }) {
return runQuery({ return runQuery({
[PRISMA]: () => relationalQuery(args), [PRISMA]: () => relationalQuery(args),
@ -49,6 +50,7 @@ async function relationalQuery(data: {
eventName?: string; eventName?: string;
eventData?: any; eventData?: any;
tag?: string; tag?: string;
createdAt?: Date;
}) { }) {
const { const {
websiteId, websiteId,
@ -63,6 +65,7 @@ async function relationalQuery(data: {
eventData, eventData,
pageTitle, pageTitle,
tag, tag,
createdAt,
} = data; } = data;
const websiteEventId = uuid(); const websiteEventId = uuid();
@ -80,6 +83,7 @@ async function relationalQuery(data: {
pageTitle: pageTitle?.substring(0, PAGE_TITLE_LENGTH), pageTitle: pageTitle?.substring(0, PAGE_TITLE_LENGTH),
eventType: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView, eventType: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
eventName: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null, eventName: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
createdAt,
tag, tag,
}, },
}); });
@ -121,6 +125,7 @@ async function clickhouseQuery(data: {
subdivision2?: string; subdivision2?: string;
city?: string; city?: string;
tag?: string; tag?: string;
createdAt?: Date;
}) { }) {
const { const {
websiteId, websiteId,
@ -139,12 +144,13 @@ async function clickhouseQuery(data: {
subdivision2, subdivision2,
city, city,
tag, tag,
createdAt,
...args ...args
} = data; } = data;
const { insert, getUTCString } = clickhouse; const { insert, getUTCString } = clickhouse;
const { sendMessage } = kafka; const { sendMessage } = kafka;
const eventId = uuid(); const eventId = uuid();
const createdAt = getUTCString(); const createdAtUTC = getUTCString(createdAt);
const message = { const message = {
...args, ...args,
@ -170,7 +176,7 @@ async function clickhouseQuery(data: {
event_type: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView, event_type: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null, event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
tag: tag, tag: tag,
created_at: createdAt, created_at: createdAtUTC,
}; };
if (kafka.enabled) { if (kafka.enabled) {
@ -187,7 +193,7 @@ async function clickhouseQuery(data: {
urlPath: urlPath?.substring(0, URL_LENGTH), urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH), eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData, eventData,
createdAt, createdAt: createdAtUTC,
}); });
} }