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
Copy file name to clipboardExpand all lines: src/connections/functions/destination-functions.md
+14-16Lines changed: 14 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ integration_type: feature
8
8
---
9
9
Destination functions allow you to transform and annotate your Segment events and send them to any external tool or API without worrying about setting up or maintaining any infrastructure.
10
10
11
-
All functions are scoped to your workspace, so members of other workspaces won't be able to view or use them.
11
+
All functions are scoped to your workspace, so members of other workspaces can't view or use them.
12
12
13
13
> info ""
14
14
> Functions is available to all customer plan types with a free allotment of usage hours. Read more about [Functions usage limits](/docs/connections/functions/usage/), or see [your workspace’s Functions usage stats](https://app.segment.com/goto-my-workspace/settings/usage?metric=functions).
@@ -17,7 +17,7 @@ All functions are scoped to your workspace, so members of other workspaces won't
17
17
18
18
19
19
> note ""
20
-
> Destination functions can't currently accept data from [Object Cloud sources](/docs/connections/sources/#object-cloud-sources).
20
+
> Destination functions does not accept data from [Object Cloud sources](/docs/connections/sources/#object-cloud-sources).
21
21
22
22
## Create a destination function
23
23
@@ -37,9 +37,9 @@ When you click **Build**, a code editor appears. Use the editor to write the cod
37
37
Segment invokes a separate part of the function (called a "handler") for each event type that you send to your destination function.
38
38
39
39
> info ""
40
-
> Your function is not invoked for an event if a [destination filter](/docs/connections/destinations/destination-filters/) is set up, and the event doesn't pass the filter.
40
+
> Your function is not invoked for an event if you've configured a [destination filter](/docs/connections/destinations/destination-filters/), and the event doesn't pass the filter.
41
41
42
-
The default source code template includes handlers for all event types. However, you do not need to implement all of them - just use the ones you need, and skip the ones you don't.
42
+
The default source code template includes handlers for all event types. You do not need to implement all of them - just use the ones you need, and skip the ones you don't.
43
43
44
44
Destination functions can define handlers for each message type in the [Segment spec](/docs/connections/spec/):
45
45
@@ -54,11 +54,9 @@ Destination functions can define handlers for each message type in the [Segment
54
54
55
55
Each of the functions above accepts two arguments:
56
56
57
-
-**event** - Segment event object, where fields and values depend on the type of event. For example, for "Identify" events, the object is formatted to match the [Identify spec](/docs/connections/spec/identify/).
57
+
-**event** - Segment event object, where fields and values depend on the event type. For example, in "Identify" events, Segment formats the object to match the [Identify spec](/docs/connections/spec/identify/).
58
58
-**settings** - Set of [settings](#create-settings-and-secrets) for this function.
59
59
60
-
We'll learn more about settings later, let's see how we can process Segment events with destination function first.
61
-
62
60
The example below shows a destination function that listens for "Track" events, and sends some details about them to an external service.
63
61
64
62
```js
@@ -77,16 +75,16 @@ async function onTrack(event) {
77
75
}
78
76
```
79
77
80
-
To change which type of event the handler listens to, you can rename it to the name of the message type. For example, if you rename this function `onIdentify`, it listens for "Identify" events instead.
78
+
To change which event type the handler listens to, you can rename it to the name of the message type. For example, if you rename this function `onIdentify`, it listens for "Identify" events instead.
81
79
82
80
> info ""
83
81
> Functions' runtime includes a `fetch()` polyfill using a `node-fetch` package. Check out the [node-fetch documentation](https://www.npmjs.com/package/node-fetch) for usage examples.
84
82
85
83
### Errors and error handling
86
84
87
-
A function's execution is considered successful if it finishes without any errors. You can also `throw` an error to indicate a failure on purpose. You can use these errors to validate event data before processing it, to ensure your function works as expected.
85
+
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.
88
86
89
-
You can `throw` the following pre-defined error types to indicate that the function ran as expected, but that data could not be delivered:
87
+
You can `throw` the following pre-defined error types to indicate that the function ran as expected, but that data was deliverable:
90
88
91
89
-`EventNotSupported`
92
90
-`InvalidEventPayload`
@@ -192,9 +190,9 @@ If your function fails, you can check the error details and logs in the **Output
192
190
## Batching the destination function (Beta)
193
191
194
192
> warning ""
195
-
> Batch handling for Functions is currently available as an early access beta release. By enabling batch handlers for your function, you acknowledge that your use of batch handlers is subject to [Segment’s Beta Terms and Conditions](https://segment.com/legal/first-access-beta-preview), or the applicable terms governing Beta Releases found in your subscription agreement with Segment.
193
+
> Batch handling for Functions is available as an early access beta release. By enabling batch handlers for your function, you acknowledge that your use of batch handlers is subject to [Segment’s Beta Terms and Conditions](https://segment.com/legal/first-access-beta-preview), or the applicable terms governing Beta Releases found in your subscription agreement with Segment.
196
194
>
197
-
> If you notice any bugs or have any general feedback on this new feature, contact [[email protected]](mailto:beta@segment.com).
195
+
> If you notice any bugs or have any general feedback on this new feature, please fill out [this form](https://airtable.com/shr9TU4huO0PK0DSU).
198
196
199
197
Batch handlers are an extension of destination 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 destination function can accept and handle batches of events.
200
198
@@ -285,7 +283,7 @@ async function onIdentifyBatch(events, settings) {
285
283
286
284
### Configure your batch parameters
287
285
288
-
You cannot yet configure batch parameters (either in the code or UI) in this version of the beta. If you would like to change your batch parameters, contact [[email protected]](mailto:beta@segment.com) with information about your specific use case, the reason you need to adjust parameters, and the URL to your destination function.
286
+
You cannot configure batch parameters (either in the code or UI) in this version of the beta. Functions waits up to 10 seconds to form a batch of 20 events. If you would like to change your batch parameters, please fill out [this form](https://airtable.com/shr9TU4huO0PK0DSU) and Support will contact you once this is made available.
289
287
290
288
### Avoid writing batch and single event handlers
291
289
@@ -384,11 +382,11 @@ async function onTrack(event, settings) {
384
382
```
385
383
386
384
> warning ""
387
-
> **Warning:** Do not log sensitive data, such as personally-identifying information (PII), authentication tokens, or other secrets. You should especially avoid logging entire request/response payloads. The **Function Logs** tab may be visible to other workspace members if they have the necessary permissions.
385
+
> **Warning:** Do not 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.
388
386
389
387
## Caching in destination functions
390
388
391
-
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 significantly improve the performance and reliability of your Functions by reducing the need for redundant API requests.
389
+
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.
392
390
393
391
This example code fetches an access token from an external API and refreshes it every hour:
394
392
@@ -430,7 +428,7 @@ If any of your deployed function instances are failing consistently, they will a
430
428
431
429
### Data control
432
430
433
-
You can use [Destination Filters](/docs/connections/destinations/destination-filters/) or [Privacy Portal](https://segment.com/docs/privacy/portal/) to manage what events and, of those events, which event properties are sent to your destination function.
431
+
You can use [Destination Filters](/docs/connections/destinations/destination-filters/) or [Privacy Portal](/docs/privacy/portal/) to manage what events and, of those events, which event properties are sent to your destination function.
0 commit comments