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

feat: reecord when timestamp is overriden by caller #1033

Merged
merged 2 commits into from
Feb 28, 2024
Merged
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
18 changes: 18 additions & 0 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 = {}
Expand Down
4 changes: 4 additions & 0 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading