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

fix: sentAt set at batch upload time #932

Merged
merged 1 commit into from
Aug 18, 2023
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I like destructuring to do 'omit'. Neat

})

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