Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow populating event's createdAt on the send endpoint #3157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/pages/api/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface CollectRequestBody {
tag?: string;
title?: string;
url: string;
createdAt?: string;
};
type: CollectionType;
}
Expand Down Expand Up @@ -72,6 +73,7 @@ const schema = {
website: yup.string().uuid().required(),
name: yup.string().max(50),
tag: yup.string().max(50).nullable(),
createdAt: yup.date(),
})
.required(),
type: yup
Expand All @@ -83,7 +85,6 @@ const schema = {

export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
await useCors(req, res);

if (req.method === 'POST') {
if (!process.env.DISABLE_BOT_CHECK && isbot(req.headers['user-agent'])) {
return ok(res, { beep: 'boop' });
Expand All @@ -97,6 +98,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {

const { type, payload } = req.body;
const { url, referrer, name: eventName, data, title, tag } = payload;
const createdAt = payload.createdAt ? new Date(payload.createdAt) : undefined;
const pageTitle = safeDecodeURI(title);

await useSession(req, res);
Expand All @@ -107,7 +109,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
return;
}

const iat = Math.floor(new Date().getTime() / 1000);
const iat = Math.floor((createdAt || new Date()).getTime() / 1000);

// expire visitId after 30 minutes
if (session.iat && iat - session.iat > 1800) {
Expand Down Expand Up @@ -147,6 +149,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
eventName,
eventData: data,
...session,
createdAt,
sessionId: session.id,
tag,
});
Expand Down
12 changes: 9 additions & 3 deletions src/queries/analytics/events/saveEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function saveEvent(args: {
subdivision2?: string;
city?: string;
tag?: string;
createdAt?: string;
}) {
return runQuery({
[PRISMA]: () => relationalQuery(args),
Expand All @@ -49,6 +50,7 @@ async function relationalQuery(data: {
eventName?: string;
eventData?: any;
tag?: string;
createdAt?: Date;
}) {
const {
websiteId,
Expand All @@ -63,6 +65,7 @@ async function relationalQuery(data: {
eventData,
pageTitle,
tag,
createdAt,
} = data;
const websiteEventId = uuid();

Expand All @@ -80,6 +83,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,
createdAt,
tag,
},
});
Expand Down Expand Up @@ -121,6 +125,7 @@ async function clickhouseQuery(data: {
subdivision2?: string;
city?: string;
tag?: string;
createdAt?: Date;
}) {
const {
websiteId,
Expand All @@ -139,12 +144,13 @@ async function clickhouseQuery(data: {
subdivision2,
city,
tag,
createdAt,
...args
} = data;
const { insert, getUTCString } = clickhouse;
const { sendMessage } = kafka;
const eventId = uuid();
const createdAt = getUTCString();
const createdAtUTC = getUTCString(createdAt);

const message = {
...args,
Expand All @@ -170,7 +176,7 @@ async function clickhouseQuery(data: {
event_type: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
tag: tag,
created_at: createdAt,
created_at: createdAtUTC,
};

if (kafka.enabled) {
Expand All @@ -187,7 +193,7 @@ async function clickhouseQuery(data: {
urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,
createdAt,
createdAt: createdAtUTC,
});
}

Expand Down