Skip to content

Commit 1017a03

Browse files
committed
feat: clean up loading
1 parent 76bb33f commit 1017a03

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

apps/basket/src/routes/observability.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async function insertObservabilityEvent(
9797
}
9898

9999
const app = new Elysia()
100-
.post('/', async ({ body }: { body: ObservabilityEvent }) => {
100+
.post('/track', async ({ body }: { body: ObservabilityEvent }) => {
101101
const parseResult = observabilityEventSchema.safeParse(body);
102102
if (!parseResult.success) {
103103
logger.error('Invalid observability event schema', {
@@ -125,7 +125,7 @@ const app = new Elysia()
125125
};
126126
}
127127
})
128-
.post('/batch', async ({ body }: { body: ObservabilityEvent[] }) => {
128+
.post('/track/batch', async ({ body }: { body: ObservabilityEvent[] }) => {
129129
if (!Array.isArray(body)) {
130130
return {
131131
status: 'error',

apps/basket/src/utils/validation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const VALIDATION_LIMITS = {
3333
TIMEZONE_MAX_LENGTH: 64,
3434
PATH_MAX_LENGTH: 2048,
3535
TEXT_MAX_LENGTH: 2048,
36+
EVENT_ID_MAX_LENGTH: 512,
3637
} as const;
3738

3839
export const SAFE_HEADERS = new Set([

apps/dashboard/public/databuddy.js

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,52 @@
483483
finalProperties = { value: properties };
484484
}
485485

486+
const customEvent = {
487+
type: 'custom',
488+
eventId: generateUUIDv4(),
489+
name: eventName,
490+
anonymousId: this.anonymousId,
491+
sessionId: this.sessionId,
492+
timestamp: Date.now(),
493+
properties: finalProperties,
494+
};
495+
496+
if (this.options.enableBatching) {
497+
return this.send(customEvent);
498+
}
499+
500+
try {
501+
const beaconResult = await this.sendBeacon(customEvent);
502+
if (beaconResult) {
503+
return beaconResult;
504+
}
505+
} catch (_e) {}
506+
507+
return this.send(customEvent);
508+
}
509+
510+
async _trackSystemEvent(eventName, properties) {
511+
if (this.options.disabled || this.isLikelyBot) {
512+
return;
513+
}
514+
515+
if (this.options.samplingRate < 1.0) {
516+
const samplingValue = Math.random();
517+
518+
if (samplingValue > this.options.samplingRate) {
519+
return { sampled: false };
520+
}
521+
}
522+
523+
let finalProperties;
524+
if (properties === undefined || properties === null) {
525+
finalProperties = {};
526+
} else if (typeof properties === 'object') {
527+
finalProperties = properties;
528+
} else {
529+
finalProperties = { value: properties };
530+
}
531+
486532
// Collect base context data
487533
const baseContext = this.getBaseContext();
488534

@@ -970,44 +1016,6 @@
9701016
};
9711017
}
9721018

973-
trackCustomEvent(eventName, properties = {}) {
974-
if (this.isServer()) {
975-
return;
976-
}
977-
978-
let finalProperties;
979-
if (properties === undefined || properties === null) {
980-
finalProperties = {};
981-
} else if (typeof properties === 'object') {
982-
finalProperties = properties;
983-
} else {
984-
finalProperties = { value: properties };
985-
}
986-
987-
const customEvent = {
988-
type: 'custom',
989-
eventId: generateUUIDv4(),
990-
name: eventName,
991-
anonymousId: this.anonymousId,
992-
sessionId: this.sessionId,
993-
timestamp: Date.now(),
994-
properties: finalProperties,
995-
};
996-
997-
if (this.options.enableBatching) {
998-
return this.send(customEvent);
999-
}
1000-
1001-
try {
1002-
const beaconResult = this.sendBeacon(customEvent);
1003-
if (beaconResult) {
1004-
return beaconResult;
1005-
}
1006-
} catch (_e) {}
1007-
1008-
return this.send(customEvent);
1009-
}
1010-
10111019
async trackOutgoingLink(linkData) {
10121020
if (this.isServer()) {
10131021
return;
@@ -1373,7 +1381,7 @@
13731381
...(n ?? {}),
13741382
};
13751383

1376-
this.track('screen_view', pageData);
1384+
this._trackSystemEvent('screen_view', pageData);
13771385
}
13781386
}
13791387
};
@@ -1398,7 +1406,7 @@
13981406
clear: () => {},
13991407
flush: () => {},
14001408
setGlobalProperties: () => {},
1401-
trackCustomEvent: () => {},
1409+
14021410
trackOutgoingLink: () => {},
14031411
options: { disabled: true },
14041412
};
@@ -1409,7 +1417,7 @@
14091417
clear: () => {},
14101418
flush: () => {},
14111419
setGlobalProperties: () => {},
1412-
trackCustomEvent: () => {},
1420+
14131421
trackOutgoingLink: () => {},
14141422
};
14151423

@@ -1562,14 +1570,13 @@
15621570
});
15631571

15641572
window.db = {
1565-
track: (...args) => window.databuddy?.trackCustomEvent(...args),
1573+
track: (...args) => window.databuddy?.track(...args),
15661574
screenView: (...args) => window.databuddy?.screenView(...args),
15671575
clear: () => window.databuddy?.clear(),
15681576
flush: () => window.databuddy?.flush(),
15691577
setGlobalProperties: (...args) =>
15701578
window.databuddy?.setGlobalProperties(...args),
1571-
trackCustomEvent: (...args) =>
1572-
window.databuddy?.trackCustomEvent(...args),
1579+
15731580
trackOutgoingLink: (...args) =>
15741581
window.databuddy?.trackOutgoingLink(...args),
15751582
};
@@ -1608,7 +1615,6 @@
16081615
const noop = () => {};
16091616
window.databuddy.track = noop;
16101617
window.databuddy.screenView = noop;
1611-
window.databuddy.trackCustomEvent = noop;
16121618
window.databuddy.trackOutgoingLink = noop;
16131619
window.databuddy.clear = noop;
16141620
window.databuddy.flush = noop;
@@ -1619,7 +1625,6 @@
16191625
const noop = () => {};
16201626
window.db.track = noop;
16211627
window.db.screenView = noop;
1622-
window.db.trackCustomEvent = noop;
16231628
window.db.trackOutgoingLink = noop;
16241629
window.db.clear = noop;
16251630
window.db.flush = noop;

0 commit comments

Comments
 (0)