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/sources/catalog/libraries/server/node/index.md
+35-31Lines changed: 35 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,23 +291,15 @@ See the complete `AnalyticsSettings` interface [in the analytics-next repository
291
291
292
292
## Usage in serverless environments
293
293
294
-
When calling Track within functions in serverless runtime environments, wrap the call in a `Promise` and `await` it to avoid having the runtime exit or freeze:
295
-
296
-
```js
297
-
await new Promise((resolve) =>
298
-
analytics().track({ ... }, resolve)
299
-
)
300
-
```
301
-
302
294
See the complete documentation on [Usage in AWS Lambda](https://github.com/segmentio/analytics-next/blob/master/packages/node/README.md#usage-in-aws-lambda){:target="_blank"}, [Usage in Vercel Edge Functions](https://github.com/segmentio/analytics-next/blob/master/packages/node/README.md#usage-in-vercel-edge-functions){:target="_blank"}, and [Usage in Cloudflare Workers](https://github.com/segmentio/analytics-next/blob/master/packages/node/README.md#usage-in-cloudflare-workers){:target="_blank"}
303
295
304
296
## Graceful shutdown
305
-
Avoid losing events after shutting down your console. Call `.closeAndFlush()` to stop collecting new events and flush all existing events. If a callback on an event call is included, this also waits for all callbacks to be called, and any of their subsequent promises to be resolved.
297
+
Avoid losing events after shutting down your console. Call `.flush({ close: true })` to stop collecting new events and flush all existing events. If a callback on an event call is included, this also waits for all callbacks to be called, and any of their subsequent promises to be resolved.
306
298
307
299
```javascript
308
-
await analytics.closeAndFlush()
300
+
await analytics.flush({ close: true })
309
301
// or
310
-
await analytics.closeAndFlush({ timeout: 5000 }) // force resolve after 5000ms
302
+
await analytics.flush({ close: true, timeout: 5000 }) // force resolve after 5000ms
311
303
```
312
304
313
305
Here's an example of how to use graceful shutdown:
If you need to preserve all of your events in the instance of a forced timeout, even ones that came in after analytics.closeAndFlush() was called, you can still collect those events by using:
321
+
If you need to preserve all of your events in the instance of a forced timeout, even ones that came in after analytics.flush({ close: true }) was called, you can still collect those events by using:
The event emitter interface allows you to track events, like Track and Identify calls, and it calls the functionyou provided with some arguments upon successful delivery. `error` emits on delivery error.
|`error`| Emitted when there is an error during event delivery or SDK initialization. |
393
+
|`identify`| Emitted when an Identify call is made.
394
+
|`track`| Emitted when a Track call is made.
395
+
|`page`| Emitted when a Page call is made.
396
+
|`group`| Emitted when a Group call is made.
397
+
|`alias`| Emitted when an Alias call is made.
398
+
|`flush`| Emitted after a batch is flushed.
399
+
|`http_request`| Emitted when an HTTP request is made. |
400
+
|`call_after_close`| Emitted when an event is received after the flush with `{ close: true }`. |
401
+
402
+
These emitters allow you to hook into various stages of the event lifecycle and handle them accordingly.
391
403
392
404
393
405
## Plugin architecture
@@ -396,22 +408,14 @@ When you develop in [Analytics.js 2.0](/docs/connections/sources/catalog/librari
396
408
Though middlewares functionthe same as plugins, it's best to use plugins as they are easier to implement and are more testable.
397
409
398
410
### Plugin categories
399
-
Plugins are bound by Analytics.js 2.0 which handles operations such as observability, retries, and error handling. There are two different categories of plugins:
400
-
* **Critical Plugins**: Analytics.js expects this plugin to be loaded before starting event delivery. Failure to load a critical plugin halts event delivery. Use this category sparingly, and only for plugins that are critical to your tracking.
401
-
* **Non-critical Plugins**: Analytics.js can start event delivery before this plugin finishes loading. This means your plugin can fail to load independently from all other plugins. For example, every Analytics.js destination is a non-critical plugin. This makes it possible for Analytics.js to continue working if a partner destination fails to load, or if users have ad blockers turned on that are targeting specific destinations.
402
-
403
-
> info ""
404
-
> Non-critical plugins are only non-critical from a loading standpoint. For example, if the `before` plugin crashes, this can still halt the event delivery pipeline.
405
-
406
-
Non-critical plugins run through a timeline that executes in order of insertion based on the entry type. Segment has these five entry types of non-critical plugins:
407
-
408
-
| Type | Details
409
-
------ | --------
410
-
| `before` | Executes before event processing begins. These are plugins that run before any other plugins run. <br><br>For example, validating events before passing them along to other plugins. A failure here could halt the event pipeline.
411
-
| `enrichment` | Executes as the first level of event processing. These plugins modify an event.
412
-
| `destination` | Executes as events begin to pass off to destinations. <br><br> This doesn't modify the event outside of the specific destination, and failure doesn't halt the execution.
413
-
| `after` | Executes after all event processing completes. You can use this to perform cleanup operations. <br><br>An example of this is the [Segment.io Plugin](https://github.com/segmentio/analytics-next/blob/master/packages/browser/src/plugins/segmentio/index.ts){:target="_blank"} which waits for destinations to succeed or fail so it can send it observability metrics.
414
-
| `utility` | Executes once during the bootstrap, to give you an outlet to make any modifications as to how Analytics.js works internally. This allows you to augment Analytics.js functionality.
411
+
412
+
| Type | Details
413
+
| ------------- | ------------- |
414
+
| `before` | Executes before event processing begins. These are plugins that run before any other plugins run. Thrown errors here can block the event pipeline. Source middleware added via `addSourceMiddleware` is treated as a `before` plugin. |
415
+
| `enrichment` | Executes as the first level of event processing. These plugins modify an event. Thrown errors here can block the event pipeline. |
416
+
| `destination` | Executes as events begin to pass off to destinations. Segment.io is implemented as a destination plugin. Thrown errors here will _not_ block the event pipeline. |
417
+
| `after` | Executes after all event processing completes. You can use this to perform cleanup operations. |
418
+
| `utility` | Executes _only once_ during the bootstrap. Gives you access to the analytics instance via the plugin's `load()` method. This doesn't allow you to modify events. |
415
419
416
420
### Example plugins
417
421
Here's an example of a plugin that converts all track event names to lowercase before the event goes through the rest of the pipeline:
0 commit comments