Skip to content

Commit

Permalink
Add sentAt field to node for Segment events (#946)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Jackins <[email protected]>
  • Loading branch information
danieljackins and danieljackins authored Sep 7, 2023
1 parent e1cb8f2 commit edfb8b5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-lizards-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-node': patch
---

Include sentAt field in payload
27 changes: 19 additions & 8 deletions packages/node/src/__tests__/http-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { pick } from 'lodash'
import nock from 'nock'
import { CoreContext } from '@segment/analytics-core'

const isoDateRegEx = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/

const snapshotMatchers = {
get batchEvent() {
return {
Expand All @@ -17,11 +19,14 @@ const snapshotMatchers = {
},
},
_metadata: expect.any(Object),
timestamp: expect.any(String),
timestamp: expect.stringMatching(isoDateRegEx),
}
},
get defaultReqBody() {
return { batch: [snapshotMatchers.batchEvent] }
return {
batch: [snapshotMatchers.batchEvent],
sentAt: expect.stringMatching(isoDateRegEx),
}
},
}

Expand Down Expand Up @@ -143,14 +148,15 @@ describe('Method Smoke Tests', () => {
},
"integrations": Object {},
"messageId": Any<String>,
"timestamp": Any<String>,
"timestamp": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
"traits": Object {
"foo": "bar",
},
"type": "identify",
"userId": "my_user_id",
},
],
"sentAt": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
}
`
)
Expand Down Expand Up @@ -182,11 +188,12 @@ describe('Method Smoke Tests', () => {
"properties": Object {
"hello": "world",
},
"timestamp": Any<String>,
"timestamp": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
"type": "track",
"userId": "foo",
},
],
"sentAt": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
}
`
)
Expand All @@ -213,10 +220,11 @@ describe('Method Smoke Tests', () => {
"messageId": Any<String>,
"name": "page",
"properties": Object {},
"timestamp": Any<String>,
"timestamp": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
"type": "page",
},
],
"sentAt": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
}
`
)
Expand Down Expand Up @@ -246,13 +254,14 @@ describe('Method Smoke Tests', () => {
"groupId": "myGroupId",
"integrations": Object {},
"messageId": Any<String>,
"timestamp": Any<String>,
"timestamp": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
"traits": Object {
"some_traits": 123,
},
"type": "group",
},
],
"sentAt": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
}
`
)
Expand All @@ -277,11 +286,12 @@ describe('Method Smoke Tests', () => {
"integrations": Object {},
"messageId": Any<String>,
"previousId": "previous",
"timestamp": Any<String>,
"timestamp": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
"type": "alias",
"userId": "alias",
},
],
"sentAt": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
}
`
)
Expand Down Expand Up @@ -314,10 +324,11 @@ describe('Method Smoke Tests', () => {
"properties": Object {
"title": "wip",
},
"timestamp": Any<String>,
"timestamp": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
"type": "screen",
},
],
"sentAt": StringMatching /\\^\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z\\$/,
}
`
)
Expand Down
9 changes: 9 additions & 0 deletions packages/node/src/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ describe('screen', () => {
})

describe('track', () => {
it('sends sentAt field in payload', async () => {
const analytics = createTestAnalytics({
httpClient: testClient,
})
const track = resolveCtx(analytics, 'track')
analytics.track({ event: 'hello', userId: 'foo' })
await track
expect(makeReqSpy.mock.calls[0][0].data.sentAt).toBeInstanceOf(Date)
})
it('generates track events', async () => {
const analytics = createTestAnalytics()
const eventName = 'Test Event'
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/plugins/segmentio/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class Publisher {
Authorization: `Basic ${this._auth}`,
'User-Agent': 'analytics-node-next/latest',
},
data: { batch: events },
data: { batch: events, sentAt: new Date() },
httpRequestTimeout: this._httpRequestTimeout,
}

Expand Down

0 comments on commit edfb8b5

Please sign in to comment.