Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix: better support for custom timestamps

Carl-Gerhard Lindesvärd10726bf3732c
diff --git a/apps/api/src/controllers/track.controller.ts b/apps/api/src/controllers/track.controller.ts
index 7c1fb4416..ae1fdc06e 100644
--- a/apps/api/src/controllers/track.controller.ts
+++ b/apps/api/src/controllers/track.controller.ts
@@ -68,16 +68,25 @@ export function getTimestamp(
const clientTimestamp = new Date(userDefinedTimestamp);
const clientTimestampNumber = clientTimestamp.getTime();
+ // Constants for time validation
+ const ONE_MINUTE_MS = 60 * 1000;
+ const FIFTEEN_MINUTES_MS = 15 * ONE_MINUTE_MS;
+
+ // Use safeTimestamp if invalid or more than 1 minute in the future
if (
Number.isNaN(clientTimestampNumber) ||
- clientTimestampNumber > safeTimestamp
+ clientTimestampNumber > safeTimestamp + ONE_MINUTE_MS
) {
return { timestamp: safeTimestamp, isTimestampFromThePast: false };
}
+ // isTimestampFromThePast is true only if timestamp is older than 1 hour
+ const isTimestampFromThePast =
+ clientTimestampNumber < safeTimestamp - FIFTEEN_MINUTES_MS;
+
return {
timestamp: clientTimestampNumber,
- isTimestampFromThePast: true,
+ isTimestampFromThePast,
};
}
diff --git a/apps/worker/src/jobs/events.incoming-event.ts b/apps/worker/src/jobs/events.incoming-event.ts
index 9d20f81dc..9799ebc6d 100644
--- a/apps/worker/src/jobs/events.incoming-event.ts
+++ b/apps/worker/src/jobs/events.incoming-event.ts
@@ -21,7 +21,7 @@ import type { EventsQueuePayloadIncomingEvent } from '@openpanel/queue';
import * as R from 'ramda';
import { v4 as uuid } from 'uuid';
-const GLOBAL_PROPERTIES = ['__path', '__referrer'];
+const GLOBAL_PROPERTIES = ['__path', '__referrer', '__timestamp'];
// This function will merge two objects.
// First it will strip '' and undefined/null from B