Skip to content

Commit adc40cf

Browse files
Merge pull request #7944 from thumbsupep/patch-1
Update insert-functions.md to correct onBatch example
2 parents 6dfff5c + a4b3969 commit adc40cf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/connections/functions/insert-functions.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ To prevent your insert function from processing data, toggle Enable Function off
267267

268268
## Batching the destination insert function
269269

270-
Batch handlers are an extension of insert functions. When you define an `onBatch` handler alongside the handler functions for single events (for example, `onTrack` or `onIdentity`), you're telling Segment that the insert function can accept and handle batches of events.
270+
Batch handlers are an extension of insert functions. When you define an `onBatch` handler alongside the handler functions for single events (for example, `onTrack` or `onIdentity`), you're telling Segment that the insert function can accept and handle batches of events.
271271

272272
> info ""
273273
> Batching is available for destination and destination insert functions only.
@@ -289,6 +289,8 @@ Segment collects the events over a short period of time and combines them into a
289289

290290
To create a batch handler, define an `onBatch` function within your destination insert function. You can also use the "Default Batch" template found in the Functions editor to get started quickly.
291291

292+
You must preserve the original order in the `onBatch` implementation, as Segment's function invoker service relies on positional consistency in the `onBatch` handler between the input and output arrays. When a function returns a transformed batch, Segment pairs each output event with its corresponding input using array index positions, not event IDs or timestamps.
293+
292294
```js
293295
async function onBatch(events, settings){
294296
// handle the batch of events
@@ -306,6 +308,7 @@ The handler function receives an array of events. The events can be of any suppo
306308
{
307309
"type": "identify",
308310
"userId": "019mr8mf4r",
311+
"messageId": "ajs-next-1757955997356-c029ce21-9034-45a6-ac62-b8b23b655df5",
309312
"traits": {
310313
"email": "jake@yahoo.com",
311314
"name": "Jake Peterson",
@@ -315,6 +318,7 @@ The handler function receives an array of events. The events can be of any suppo
315318
{
316319
"type": "track",
317320
"userId": "019mr8mf4r",
321+
"messageId": "ajs-next-1757955997356-bde2ad8e-2af9-45f5-837b-993671f6b1b0",
318322
"event": "Song Played",
319323
"properties": {
320324
"name": "Fallin for You",
@@ -324,6 +328,7 @@ The handler function receives an array of events. The events can be of any suppo
324328
{
325329
"type": "track",
326330
"userId": "971mj8mk7p",
331+
"messageId": "ajs-next-1757955997356-9e2ba07c-0085-4a5b-8928-e4450a417f74",
327332
"event": "Song Played",
328333
"properties": {
329334
"name": "Get Right",
@@ -339,6 +344,9 @@ Segment batches together any event _of any type_ that it sees over a short perio
339344

340345
```js
341346
async function onBatch(events, settings) {
347+
// store original order
348+
const originalOrder = events.map(event => event.messageId);
349+
342350
// group events by type
343351
const eventsByType = {}
344352
for (const event of events) {
@@ -361,7 +369,10 @@ async function onBatch(events, settings) {
361369
try {
362370
const results = await Promise.all(promises);
363371
const batchResult = [].concat(...results); // Combine arrays into a single array
364-
return batchResult;
372+
373+
// restore original order
374+
const resultMap = new Map(batchResult.map(e => [e.messageId, e]));
375+
return originalOrder.map(id => resultMap.get(id));
365376
} catch (error) {
366377
throw new RetryError(error.message);
367378
}

0 commit comments

Comments
 (0)