Skip to content

Commit e1d9645

Browse files
committed
wip
1 parent 7cdc6d4 commit e1d9645

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

src/connections/sources/catalog/libraries/server/node/index.md

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,15 @@ See the complete `AnalyticsSettings` interface [in the analytics-next repository
291291
292292
## Usage in serverless environments
293293
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-
302294
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"}
303295
304296
## 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.
306298
307299
```javascript
308-
await analytics.closeAndFlush()
300+
await analytics.flush({ close: true })
309301
// or
310-
await analytics.closeAndFlush({ timeout: 5000 }) // force resolve after 5000ms
302+
await analytics.flush({ close: true, timeout: 5000 }) // force resolve after 5000ms
311303
```
312304
313305
Here's an example of how to use graceful shutdown:
@@ -316,7 +308,7 @@ const app = express()
316308
const server = app.listen(3000)
317309
318310
const onExit = async () => {
319-
await analytics.closeAndFlush()
311+
await analytics.flush({ close: true })
320312
server.close(() => {
321313
console.log("Gracefully closing server...")
322314
process.exit()
@@ -326,15 +318,15 @@ const onExit = async () => {
326318
```
327319
328320
### Collect unflushed events
329-
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:
330322
331323
```javascript
332324
const unflushedEvents = []
333325
334326
analytics.on('call_after_close', (event) => unflushedEvents.push(events))
335-
await analytics.closeAndFlush()
327+
await analytics.flush({ close: true })
336328
337-
console.log(unflushedEvents) // all events that came in after closeAndFlush was called
329+
console.log(unflushedEvents) // all events that came in after flush was called
338330
```
339331
340332
## Regional configuration
@@ -364,6 +356,7 @@ analytics.on('error', (err) => console.error(err))
364356
### Event emitter interface
365357
The event emitter interface allows you to track events, like Track and Identify calls, and it calls the function you provided with some arguments upon successful delivery. `error` emits on delivery error.
366358
359+
367360
```javascript
368361
analytics.on('error', (err) => console.error(err))
369362
@@ -372,6 +365,7 @@ analytics.on('identify', (ctx) => console.log(ctx))
372365
analytics.on('track', (ctx) => console.log(ctx))
373366
```
374367
368+
375369
Use the emitter to log all HTTP Requests.
376370
377371
```javascript
@@ -388,6 +382,24 @@ Use the emitter to log all HTTP Requests.
388382
body: '...',
389383
}
390384
```
385+
386+
### Emitter Types
387+
388+
The following table documents all the emitter types available in the Analytics Node.js library:
389+
390+
| Emitter Type | Description |
391+
|-------------------|-----------------------------------------------------------------------------|
392+
| `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.
391403
392404
393405
## Plugin architecture
@@ -396,22 +408,14 @@ When you develop in [Analytics.js 2.0](/docs/connections/sources/catalog/librari
396408
Though middlewares function the same as plugins, it's best to use plugins as they are easier to implement and are more testable.
397409
398410
### 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. |
415419
416420
### Example plugins
417421
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:

src/connections/sources/catalog/libraries/server/node/migration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ If you're using the [classic version of Analytics Node.js](/docs/connections/sou
3232

3333
<br> Before:
3434
```javascript
35-
await analytics.flush(function(err, batch) {
35+
await analytics.flush((err, batch) => {
3636
console.log('Flushed, and now this program can exit!');
3737
});
3838
```
3939

4040
After:
4141
```javascript
42-
await analytics.closeAndFlush()
42+
await analytics.flush({ close: true })
4343
```
4444

4545
### Key differences between the classic and updated version

0 commit comments

Comments
 (0)