Skip to content

Commit ccf0ed7

Browse files
committed
fix: custom events schema
1 parent d76e7af commit ccf0ed7

File tree

4 files changed

+516
-6
lines changed

4 files changed

+516
-6
lines changed

apps/basket/src/routes/basket.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ async function insertCustomEvent(
321321
customData.eventId,
322322
VALIDATION_LIMITS.SHORT_STRING_MAX_LENGTH
323323
);
324-
q;
324+
325325
if (!eventId) {
326326
eventId = randomUUID();
327327
}
@@ -841,8 +841,12 @@ const app = new Elysia()
841841
errors: parseResult.error.issues,
842842
};
843843
}
844-
insertCustomEvent(body, clientId, userAgent, ip);
845-
return { status: 'success', type: 'custom' };
844+
845+
const eventId = body.eventId || randomUUID();
846+
const customEventWithId = { ...body, eventId };
847+
848+
await insertCustomEvent(customEventWithId, clientId, userAgent, ip);
849+
return { status: 'success', type: 'custom', eventId };
846850
}
847851

848852
if (eventType === 'outgoing_link') {
@@ -1132,11 +1136,15 @@ const app = new Elysia()
11321136
};
11331137
}
11341138
try {
1135-
await insertCustomEvent(event, clientId, userAgent, ip);
1139+
// Generate eventId if not provided
1140+
const eventId = event.eventId || randomUUID();
1141+
const customEventWithId = { ...event, eventId };
1142+
1143+
await insertCustomEvent(customEventWithId, clientId, userAgent, ip);
11361144
return {
11371145
status: 'success',
11381146
type: 'custom',
1139-
eventId: event.eventId,
1147+
eventId,
11401148
};
11411149
} catch (error) {
11421150
return {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use server';
2+
3+
interface CustomEventData {
4+
clientId: string;
5+
name: string;
6+
anonymousId?: string;
7+
sessionId?: string;
8+
timestamp?: number;
9+
properties?: Record<string, any>;
10+
}
11+
12+
export async function sendCustomEvent(data: CustomEventData) {
13+
try {
14+
const payload = {
15+
type: 'custom',
16+
name: data.name,
17+
...(data.anonymousId && { anonymousId: data.anonymousId }),
18+
...(data.sessionId && { sessionId: data.sessionId }),
19+
...(data.timestamp && { timestamp: data.timestamp }),
20+
...(data.properties && { properties: data.properties }),
21+
};
22+
23+
const response = await fetch(
24+
`https://basket.databuddy.cc/?client_id=${data.clientId}`,
25+
{
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
},
30+
body: JSON.stringify(payload),
31+
}
32+
);
33+
34+
const result = await response.json();
35+
36+
if (!response.ok) {
37+
throw new Error(
38+
result.message || `HTTP ${response.status}: ${response.statusText}`
39+
);
40+
}
41+
42+
return result;
43+
} catch (error) {
44+
console.error('Failed to send custom event:', error);
45+
throw error;
46+
}
47+
}
48+
49+
export async function sendBatchCustomEvents(
50+
clientId: string,
51+
events: CustomEventData[]
52+
) {
53+
try {
54+
const payload = events.map((event) => ({
55+
type: 'custom',
56+
name: event.name,
57+
...(event.anonymousId && { anonymousId: event.anonymousId }),
58+
...(event.sessionId && { sessionId: event.sessionId }),
59+
...(event.timestamp && { timestamp: event.timestamp }),
60+
...(event.properties && { properties: event.properties }),
61+
}));
62+
63+
const response = await fetch(
64+
`https://basket.databuddy.cc/batch?client_id=${clientId}`,
65+
{
66+
method: 'POST',
67+
headers: {
68+
'Content-Type': 'application/json',
69+
},
70+
body: JSON.stringify(payload),
71+
}
72+
);
73+
74+
const result = await response.json();
75+
76+
if (!response.ok) {
77+
throw new Error(
78+
result.message || `HTTP ${response.status}: ${response.statusText}`
79+
);
80+
}
81+
82+
return result;
83+
} catch (error) {
84+
console.error('Failed to send batch custom events:', error);
85+
throw error;
86+
}
87+
}

0 commit comments

Comments
 (0)