You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -8,19 +8,19 @@ Use Destination Insert Functions to compute, transform, and enrich your data bef
8
8
9
9
**Implement advanced data computation**: Write custom computation, operations, and business logic on streaming data that you send to downstream destinations.
10
10
11
-
**Enrich your data**: Use insert functions with Segment's Profile API or third party sources to add additional context to your data and create personalized customer experiences.
11
+
**Enrich your data**: Use destination insert functions with Segment's Profile API or third party sources to add additional context to your data and create personalized customer experiences.
12
12
13
-
**Support data compliance**: Use insert functions to support data masking, encryption, decryption, improved PII data handling, and tokenization.
13
+
**Support data compliance**: Use destination insert functions to support data masking, encryption, decryption, improved PII data handling, and tokenization.
14
14
15
15
**Customize filtration for your destinations**: Create custom logic with nested if-else statements, regex, custom business rules, and more to filter event data.
16
16
17
-
> info "Destination Insert Functions is in Private Beta"
17
+
> info "Private Beta"
18
18
> Destination Insert Functions is in Private Beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available. [Contact Segment](https://segment.com/help/contact/){:target="_blank"} with any feedback or questions.
19
19
20
20
21
-
## Create and manage destination insert functions
21
+
## Create destination insert functions
22
22
23
-
There are two ways you can access Destination Insert Functions from your Segment space:
23
+
There are two ways you can access destination insert functions from your Segment space:
24
24
- From the Connections [catalog](#using-the-catalog).
25
25
- From the [Destinations](#using-the-destinations-tab) tab.
26
26
@@ -44,11 +44,11 @@ For data to flow to your downstream destinations, you'll need to connect your in
44
44
45
45
To access insert functions through the Destinations tab, navigate to **Connections > Destinations > Functions** and select your insert function. Use this page to edit and manage insert functions in your workspace.
46
46
47
-
You can also use this page to [enable insert functions](#enable-the-insert-function) in your workspace.
47
+
You can also use this page to [enable destination insert functions](#enable-the-insert-function) in your workspace.
48
48
49
-
## Code the insert function
49
+
## Code the destination insert function
50
50
51
-
Segment invokes a separate part of the function (called a "handler") for each event type that you send to your insert function.
51
+
Segment invokes a separate part of the function (called a "handler") for each event type that you send to your destination insert function.
52
52
53
53
> info ""
54
54
> Your function isn't invoked for an event if you've configured a [destination filter](/docs/connections/destinations/destination-filters/), and the event doesn't pass the filter.
@@ -94,7 +94,81 @@ To change which event type the handler listens to, you can rename it to the name
94
94
> info ""
95
95
> Functions' runtime includes a `fetch()` polyfill using a `node-fetch` package. Check out the [node-fetch documentation](https://www.npmjs.com/package/node-fetch){:target="_blank"} for usage examples.
96
96
97
-
## Testing the insert function
97
+
### Errors and error handling
98
+
99
+
Segment considers a function's execution successful if it finishes without error. You can also `throw` an error to create a failure on purpose. Use these errors to validate event data before processing it, to ensure the function works as expected.
100
+
101
+
You can `throw` the following pre-defined error types to indicate that the function ran as expected, but that data was deliverable:
102
+
103
+
-`EventNotSupported`
104
+
-`InvalidEventPayload`
105
+
-`ValidationError`
106
+
-`RetryError`
107
+
108
+
The examples show basic uses of these error types.
109
+
110
+
```js
111
+
asyncfunctiononGroup(event) {
112
+
if (!event.traits.company) {
113
+
thrownewInvalidEventPayload('Company name is required')
114
+
}
115
+
}
116
+
117
+
asyncfunctiononPage(event) {
118
+
if (!event.properties.pageName) {
119
+
thrownewValidationError('Page name is required')
120
+
}
121
+
}
122
+
123
+
asyncfunctiononAlias(event) {
124
+
thrownewEventNotSupported('Alias event is not supported')
125
+
}
126
+
127
+
asyncfunctiononTrack(event) {
128
+
let res
129
+
try {
130
+
res =awaitfetch('http://example-service.com/api', {
131
+
method:'POST',
132
+
headers: {
133
+
'Content-Type':'application/json'
134
+
},
135
+
body:JSON.stringify({ event })
136
+
})
137
+
} catch (err) {
138
+
// Retry on connection error
139
+
thrownewRetryError(err.message)
140
+
}
141
+
if (res.status>=500||res.status===429) {
142
+
// Retry on 5xx and 429s (ratelimits)
143
+
thrownewRetryError(`HTTP Status ${res.status}`)
144
+
}
145
+
}
146
+
147
+
```
148
+
If you don't supply a function for an event type, Segment throws an `EventNotSupported` error by default.
149
+
150
+
You can read more about [error handling](#destination-functions-logs-and-errors) below.
151
+
152
+
## Create settings and secrets
153
+
154
+
{% include content/functions/settings.md %}
155
+
156
+
Next, fill out this setting's value in the **Test** tab, so you can run the function and verify that the correct setting value is passed. (This value is only for testing your function.)
157
+
158
+
Now that you've configured a setting and entered a test value, you can add code to read its value and run the function, as in the example below:
159
+
160
+
```js
161
+
asyncfunctiononTrack(request, settings) {
162
+
constapiKey=settings.apiKey
163
+
//=> "super_secret_string"
164
+
}
165
+
```
166
+
167
+
When you deploy your destination insert function in your workspace, you fill out the settings on the destination configuration page, similar to how you would configure a normal destination.
168
+
169
+
170
+
## Test the destination insert function
171
+
98
172
{% comment %}
99
173
You can test your code directly from the editor in two ways:
100
174
@@ -103,13 +177,14 @@ Use a sample event:
103
177
2. Select a destination or source to use events from. Then, select a sample event to use.
104
178
2. Click **Use event**, and select **Run** from the **Test** tab.
105
179
{% endcomment %}
180
+
106
181
You can manually test your code from the functions editor:
107
182
1. From the **Test** tab, click **customize the event yourself** and manually input your own JSON payload.
108
183
2. If your test fails, you can check the error details and logs in the Output section.
109
184
- Error messages display errors surfaced from your function.
110
185
- Logs display any messages to console.log() from the function.
111
186
112
-
## Save and deploy the insert function
187
+
## Save and deploy the destination insert function
113
188
114
189
Once you finish building your insert function, click **Next: Configure and deploy** to name it, then click **Create function** to save it.
115
190
@@ -122,7 +197,7 @@ You can also choose to **Save & Deploy** to save the changes, then choose which
122
197
> info ""
123
198
> You may need additional permissions to update existing functions.
124
199
125
-
## Enable the insert function
200
+
## Enable the destination insert function
126
201
127
202
You need to enable your insert function for it to process your data.
128
203
@@ -151,8 +226,71 @@ Note the following limitations for batching with insert functions:
151
226
152
227
## Destination Insert Functions logs and errors
153
228
229
+
A function can throw errors, or Segment might encounter errors while invoking your function. You can view these errors in the [Event Delivery](/docs/connections/event-delivery/) tab for your Destination as in the example below.
230
+
231
+

232
+
233
+
### Destination functions error types
234
+
235
+
-**Bad Request** - Any error thrown by the function code that is not covered by the other errors.
236
+
-**Invalid Settings** - A configuration error prevented Segment from executing your code. If this error persists for more than an hour, [contact Segment Support](https://segment.com/help/contact/){:target="_blank"}.
237
+
-**Message Rejected** - Your code threw `InvalidEventPayload` or `ValidationError` due to invalid input.
238
+
-**Unsupported Event Type** - Your code doesn't implement a specific event type (for example, `onTrack()`) or threw an `EventNotSupported` error.
239
+
-**Retry** - Your code threw `RetryError` indicating that the function should be retried.
240
+
241
+
Segment only attempts to send the event to your destination function again if a **Retry** error occurs.
242
+
243
+
You can view Segment's list of [Integration Error Codes](/docs/connections/integration_error_codes/) for more information about what might cause an error.
244
+
245
+
### Destination functions logs
246
+
247
+
If your function throws an error, execution halts immediately. Segment captures the event, any outgoing requests/responses, any logs the function might have printed, as well as the error itself.
248
+
249
+
Segment then displays the captured error information in the [Event Delivery](/docs/connections/event-delivery/) page for your destination insert function. You can use this information to find and fix unexpected errors.
250
+
251
+
You can throw [an error or a custom error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error){:target="_blank"} and you can also add helpful context in logs using the [`console` API](https://developer.mozilla.org/en-US/docs/Web/API/console){:target="_blank"}. For example:
252
+
253
+
```js
254
+
asyncfunctiononTrack(event, settings) {
255
+
constuserId=event.userId
256
+
257
+
console.log('User ID is', userId)
258
+
259
+
if (typeof userId !=='string'||userId.length<8) {
260
+
thrownewValidationError('User ID is invalid')
261
+
}
262
+
263
+
console.log('User ID is valid')
264
+
}
265
+
```
266
+
267
+
> warning ""
268
+
> Don't log sensitive data, such as personally-identifying information (PII), authentication tokens, or other secrets. Avoid logging entire request/response payloads. The **Function Logs** tab may be visible to other workspace members if they have the necessary permissions.
269
+
154
270
## Caching in Destination Insert Functions
155
271
272
+
Functions execute only in response to incoming data, but the environments that functions run in are generally long-running. Because of this, you can use global variables to cache small amounts of information between invocations. For example, you can reduce the number of access tokens you generate by caching a token, and regenerating it only after it expires. Segment cannot make any guarantees about the longevity of environments, but by using this strategy, you can improve the performance and reliability of your Functions by reducing the need for redundant API requests.
273
+
274
+
This example code fetches an access token from an external API and refreshes it every hour:
@@ -166,6 +304,29 @@ If you are a **Workspace Owner** or **Functions Admin**, you can manage your fun
166
304
167
305
## Destination Insert Functions FAQs
168
306
307
+
##### Can I see who made changes to a function?
308
+
309
+
Yes, Functions access is logged in the [Audit Trail](/docs/segment-app/iam/audit-trail/), so user activity related to functions appears in the logs.
310
+
311
+
##### Does Segment retry failed function invocations?
312
+
313
+
Yes, Segment retries invocations that throw RetryError or Timeout errors (temporary errors only). Segment's internal system retries failed functions API calls for four hours with a randomized exponential backoff after each attempt. This substantially improves delivery rates.
314
+
315
+
[Retries](/docs/connections/destinations/#retries-between-segment-and-destinations) work the same for both functions and cloud-mode destinations in Segment.
316
+
317
+
##### Are events guaranteed to send data in order?
318
+
319
+
No, Segment can't guarantee the order in which the events are delivered to an endpoint.
320
+
321
+
##### Can I create a device-mode destination?
322
+
323
+
No, destination insert functions are currently available as cloud-mode destinations only. Segment is in the early phases of exploration and discovery for supporting customer "web plugins" for custom device-mode destinations and other use cases, but this is unsupported today.
324
+
325
+
##### How do I publish a destination to the public Segment catalog?
326
+
327
+
If you are a partner, looking to publish your destination and distribute your app through Segment catalog, visit the [Developer Center](https://segment.com/partners/developer-center/){:target="_blank"} and check out the Segment [partner docs](/docs/partners/).
0 commit comments