Skip to content

Commit

Permalink
feat(capture): Add timestamp override option (#391)
Browse files Browse the repository at this point in the history
* Add timestamp override option in event capture

* More accurate comments and simplifying business logic

* Remove double spacing

* Update unit test

Co-authored-by: Michael Matloka <[email protected]>
  • Loading branch information
germain-gg and Twixes authored Aug 1, 2022
1 parent 3f3c4a3 commit 14ad8c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ describe('capture()', () => {
'{"navigation":[["duration"],[[1234]]],"paint":[],"resource":[["c"],[["d"]]]}'
)
})

it('supports timestamp override', () => {
const date = new Date(1234)
given('options', () => ({
timestamp: date,
}))

const captured_event = given.subject()
expect(captured_event.timestamp).toBe(date)
})
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ export class PostHog {
* @param {Object} [properties] A set of properties to include with the event you're sending. These describe the user who did the event or details about the event itself.
* @param {Object} [options] Optional configuration for this capture request.
* @param {String} [options.transport] Transport method for network request ('XHR' or 'sendBeacon').
* @param {Date} [options.timestamp] Timestamp is a Date object. If not set, it'll automatically be set to the current time.
*/
capture(
event_name: string,
Expand Down Expand Up @@ -703,6 +704,8 @@ export class PostHog {
data,
options._noTruncate ? null : this.get_config('properties_string_max_length')
)
data.timestamp = options.timestamp || new Date()

if (this.get_config('debug')) {
logger.log('PostHog.js send', data)
}
Expand All @@ -717,7 +720,6 @@ export class PostHog {
(!has_unique_traits || options._batchKey) &&
!options.send_instantly
) {
data['timestamp'] = new Date()
this._requestQueue.enqueue(url, data, options)
} else {
this.__compress_and_send_json_request(url, jsonData, options)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface CaptureOptions extends XHROptions {
_noTruncate?: boolean /** if set, overrides and disables config.properties_string_max_length */
endpoint?: string /** defaults to '/e/' */
send_instantly?: boolean /** if set skips the batched queue */
timestamp?: Date
}

export interface RetryQueueElement {
Expand Down

0 comments on commit 14ad8c2

Please sign in to comment.