Skip to content

Commit b1584fc

Browse files
authored
fix: sentAt set at batch upload time (#932)
`sentAt` is not set at batch upload time once per the whole batch. Individual event `sentAt` property is stripped when doing batch uploading.
1 parent 9ba0dc6 commit b1584fc

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

.changeset/odd-nails-collect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
`sentAt` is not set at batch upload time once per the whole batch. Individual event `sentAt` property is stripped when doing batch uploading.

packages/browser/src/plugins/segmentio/__tests__/batched-dispatcher.test.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ describe('Batching', () => {
4949
beforeEach(() => {
5050
jest.resetAllMocks()
5151
jest.restoreAllMocks()
52-
jest.useFakeTimers()
52+
jest.useFakeTimers({
53+
now: new Date('9 Jun 1993 00:00:00Z').getTime(),
54+
})
5355
})
5456

5557
afterEach(() => {
@@ -92,7 +94,7 @@ describe('Batching', () => {
9294
Array [
9395
"https://https://api.segment.io/b",
9496
Object {
95-
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"},{\\"event\\":\\"third\\"}]}",
97+
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"},{\\"event\\":\\"third\\"}],\\"sentAt\\":\\"1993-06-09T00:00:00.000Z\\"}",
9698
"headers": Object {
9799
"Content-Type": "text/plain",
98100
},
@@ -150,7 +152,7 @@ describe('Batching', () => {
150152
Array [
151153
"https://https://api.segment.io/b",
152154
Object {
153-
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"}]}",
155+
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"}],\\"sentAt\\":\\"1993-06-09T00:00:10.000Z\\"}",
154156
"headers": Object {
155157
"Content-Type": "text/plain",
156158
},
@@ -185,7 +187,7 @@ describe('Batching', () => {
185187
Array [
186188
"https://https://api.segment.io/b",
187189
Object {
188-
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"}]}",
190+
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"}],\\"sentAt\\":\\"1993-06-09T00:00:10.000Z\\"}",
189191
"headers": Object {
190192
"Content-Type": "text/plain",
191193
},
@@ -199,7 +201,38 @@ describe('Batching', () => {
199201
Array [
200202
"https://https://api.segment.io/b",
201203
Object {
202-
"body": "{\\"batch\\":[{\\"event\\":\\"second\\"}]}",
204+
"body": "{\\"batch\\":[{\\"event\\":\\"second\\"}],\\"sentAt\\":\\"1993-06-09T00:00:21.000Z\\"}",
205+
"headers": Object {
206+
"Content-Type": "text/plain",
207+
},
208+
"keepalive": false,
209+
"method": "post",
210+
},
211+
]
212+
`)
213+
})
214+
215+
it('removes sentAt from individual events', async () => {
216+
const { dispatch } = batch(`https://api.segment.io`, {
217+
size: 2,
218+
})
219+
220+
await dispatch(`https://api.segment.io/v1/t`, {
221+
event: 'first',
222+
sentAt: new Date('11 Jun 1993 00:01:00Z'),
223+
})
224+
225+
await dispatch(`https://api.segment.io/v1/t`, {
226+
event: 'second',
227+
sentAt: new Date('11 Jun 1993 00:02:00Z'),
228+
})
229+
230+
expect(fetch).toHaveBeenCalledTimes(1)
231+
expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
232+
Array [
233+
"https://https://api.segment.io/b",
234+
Object {
235+
"body": "{\\"batch\\":[{\\"event\\":\\"first\\"},{\\"event\\":\\"second\\"}],\\"sentAt\\":\\"1993-06-09T00:00:00.000Z\\"}",
203236
"headers": Object {
204237
"Content-Type": "text/plain",
205238
},

packages/browser/src/plugins/segmentio/batched-dispatcher.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,23 @@ export default function batch(
6060

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

63+
// Remove sentAt from every event as batching only needs a single timestamp
64+
const updatedBatch = batch.map((event) => {
65+
const { sentAt, ...newEvent } = event as SegmentEvent
66+
return newEvent
67+
})
68+
6369
return fetch(`https://${apiHost}/b`, {
6470
keepalive: pageUnloaded,
6571
headers: {
6672
'Content-Type': 'text/plain',
6773
},
6874
method: 'post',
69-
body: JSON.stringify({ batch, writeKey }),
75+
body: JSON.stringify({
76+
writeKey,
77+
batch: updatedBatch,
78+
sentAt: new Date().toISOString(),
79+
}),
7080
})
7181
}
7282

0 commit comments

Comments
 (0)