Skip to content

Commit

Permalink
fix: sentAt set at batch upload time (#932)
Browse files Browse the repository at this point in the history
`sentAt` is not set at batch upload time once per the whole batch. Individual event `sentAt` property is stripped when doing batch uploading.
  • Loading branch information
oscb authored Aug 18, 2023
1 parent 9ba0dc6 commit b1584fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-nails-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

`sentAt` is not set at batch upload time once per the whole batch. Individual event `sentAt` property is stripped when doing batch uploading.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ describe('Batching', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.restoreAllMocks()
jest.useFakeTimers()
jest.useFakeTimers({
now: new Date('9 Jun 1993 00:00:00Z').getTime(),
})
})

afterEach(() => {
Expand Down Expand Up @@ -92,7 +94,7 @@ describe('Batching', () => {
Array [
"https://https://api.segment.io/b",
Object {
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"},{\\"event\\":\\"third\\"}]}",
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"},{\\"event\\":\\"third\\"}],\\"sentAt\\":\\"1993-06-09T00:00:00.000Z\\"}",
"headers": Object {
"Content-Type": "text/plain",
},
Expand Down Expand Up @@ -150,7 +152,7 @@ describe('Batching', () => {
Array [
"https://https://api.segment.io/b",
Object {
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"}]}",
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"}],\\"sentAt\\":\\"1993-06-09T00:00:10.000Z\\"}",
"headers": Object {
"Content-Type": "text/plain",
},
Expand Down Expand Up @@ -185,7 +187,7 @@ describe('Batching', () => {
Array [
"https://https://api.segment.io/b",
Object {
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"}]}",
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"}],\\"sentAt\\":\\"1993-06-09T00:00:10.000Z\\"}",
"headers": Object {
"Content-Type": "text/plain",
},
Expand All @@ -199,7 +201,38 @@ describe('Batching', () => {
Array [
"https://https://api.segment.io/b",
Object {
"body": "{\\"batch\\":[{\\"event\\":\\"second\\"}]}",
"body": "{\\"batch\\":[{\\"event\\":\\"second\\"}],\\"sentAt\\":\\"1993-06-09T00:00:21.000Z\\"}",
"headers": Object {
"Content-Type": "text/plain",
},
"keepalive": false,
"method": "post",
},
]
`)
})

it('removes sentAt from individual events', async () => {
const { dispatch } = batch(`https://api.segment.io`, {
size: 2,
})

await dispatch(`https://api.segment.io/v1/t`, {
event: 'first',
sentAt: new Date('11 Jun 1993 00:01:00Z'),
})

await dispatch(`https://api.segment.io/v1/t`, {
event: 'second',
sentAt: new Date('11 Jun 1993 00:02:00Z'),
})

expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"https://https://api.segment.io/b",
Object {
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"}],\\"sentAt\\":\\"1993-06-09T00:00:00.000Z\\"}",
"headers": Object {
"Content-Type": "text/plain",
},
Expand Down
12 changes: 11 additions & 1 deletion packages/browser/src/plugins/segmentio/batched-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,23 @@ export default function batch(

const writeKey = (batch[0] as SegmentEvent)?.writeKey

// Remove sentAt from every event as batching only needs a single timestamp
const updatedBatch = batch.map((event) => {
const { sentAt, ...newEvent } = event as SegmentEvent
return newEvent
})

return fetch(`https://${apiHost}/b`, {
keepalive: pageUnloaded,
headers: {
'Content-Type': 'text/plain',
},
method: 'post',
body: JSON.stringify({ batch, writeKey }),
body: JSON.stringify({
writeKey,
batch: updatedBatch,
sentAt: new Date().toISOString(),
}),
})
}

Expand Down

0 comments on commit b1584fc

Please sign in to comment.