diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index f879a180a..e3bb3a920 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -14,6 +14,8 @@ jest.mock('../gdpr-utils', () => ({ })) jest.mock('../decide') +jest.useFakeTimers().setSystemTime(new Date('2020-01-01')) + given('lib', () => { const posthog = new PostHog() posthog._init('testtoken', given.config, 'testhog') @@ -74,6 +76,22 @@ describe('posthog core', () => { expect(captureData).toHaveProperty('uuid') }) + it('adds system time to events', () => { + const captureData = given.subject() + expect(captureData).toHaveProperty('timestamp') + // timer is fixed at 2020-01-01 + expect(captureData.timestamp).toEqual(new Date(2020, 0, 1)) + }) + + it('captures when time is overriden by caller', () => { + given.options = { timestamp: new Date(2020, 0, 2, 12, 34) } + const captureData = given.subject() + expect(captureData).toHaveProperty('timestamp') + expect(captureData.timestamp).toEqual(new Date(2020, 0, 2, 12, 34)) + expect(captureData.properties['$event_time_override_provided']).toEqual(true) + expect(captureData.properties['$event_time_override_system_time']).toEqual(new Date(2020, 0, 1)) + }) + it('handles recursive objects', () => { given('eventProperties', () => { const props = {} diff --git a/src/posthog-core.ts b/src/posthog-core.ts index eba796d3b..28cb74f04 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -921,6 +921,10 @@ export class PostHog { data = _copyAndTruncateStrings(data, options._noTruncate ? null : this.config.properties_string_max_length) data.timestamp = options.timestamp || new Date() + if (!_isUndefined(options.timestamp)) { + data.properties['$event_time_override_provided'] = true + data.properties['$event_time_override_system_time'] = new Date() + } // Top-level $set overriding values from the one from properties is taken from the plugin-server normalizeEvent // This doesn't handle $set_once, because posthog-people doesn't either