Skip to content

Commit 8864a6a

Browse files
committed
update docs
1 parent e1d9645 commit 8864a6a

File tree

1 file changed

+97
-14
lines changed
  • src/connections/sources/catalog/libraries/server/node

1 file changed

+97
-14
lines changed

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

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,97 @@ See the complete `AnalyticsSettings` interface [in the analytics-next repository
291291
292292
## Usage in serverless environments
293293
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"}
294+
## Runtime Support
295+
- Node.js >= 18
296+
- AWS Lambda
297+
- Cloudflare Workers
298+
- Vercel Edge Functions
299+
- Web Workers / Browser (no device mode destination support)
300+
301+
### Usage in AWS Lambda
302+
- [AWS lambda execution environment](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html) is challenging for typically non-response-blocking async activites like tracking or logging, since the runtime terminates / freezes after a response is emitted.
303+
304+
Here is an example of using analytics.js within a handler:
305+
```ts
306+
const { Analytics } = require('@segment/analytics-node');
307+
308+
// Preferable to create a new analytics instance per-invocation. Otherwise, we may get a warning about overlapping flush calls. Also, custom plugins have the potential to be stateful, so we prevent those kind of race conditions.
309+
const createAnalytics = () => new Analytics({
310+
writeKey: '<MY_WRITE_KEY>',
311+
}).on('error', console.error);
312+
313+
module.exports.handler = async (event) => {
314+
const analytics = createAnalytics()
315+
316+
analytics.identify({ ... })
317+
analytics.track({ ... })
318+
319+
// ensure analytics events get sent before program exits
320+
await analytics.flush()
321+
322+
return {
323+
statusCode: 200,
324+
};
325+
....
326+
};
327+
```
328+
329+
### Usage in Vercel Edge Functions
330+
331+
```ts
332+
import { Analytics } from '@segment/analytics-node';
333+
import { NextRequest, NextResponse } from 'next/server';
334+
335+
const createAnalytics = () => new Analytics({
336+
writeKey: '<MY_WRITE_KEY>',
337+
}).on('error', console.error)
338+
339+
export const config = {
340+
runtime: 'edge',
341+
};
342+
343+
export default async (req: NextRequest) => {
344+
const analytics = createAnalytics()
345+
346+
analytics.identify({ ... })
347+
analytics.track({ ... })
348+
349+
// ensure analytics events get sent before program exits
350+
await analytics.flush()
351+
352+
return NextResponse.json({ ... })
353+
};
354+
```
355+
356+
### Usage in Cloudflare Workers
357+
358+
```ts
359+
import { Analytics, Context } from '@segment/analytics-node';
360+
361+
362+
const createAnalytics = () => new Analytics({
363+
writeKey: '<MY_WRITE_KEY>',
364+
}).on('error', console.error);
365+
366+
export default {
367+
async fetch(
368+
request: Request,
369+
env: Env,
370+
ctx: ExecutionContext
371+
): Promise<Response> {
372+
const analytics = createAnalytics()
373+
374+
analytics.identify({ ... })
375+
analytics.track({ ... })
376+
377+
// ensure analytics events get sent before program exits
378+
await analytics.flush()
379+
380+
return new Response(...)
381+
},
382+
};
383+
384+
```
295385
296386
## Graceful shutdown
297387
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.
@@ -354,24 +444,17 @@ analytics.on('error', (err) => console.error(err))
354444
355445
356446
### Event emitter interface
357-
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.
447+
The event emitter interface allows you to pass a callback which will be invoked whenever a specific emitter event occurs in your app, such as when a certain method call is made.
358448
449+
For example:
359450
360451
```javascript
361-
analytics.on('error', (err) => console.error(err))
362-
363-
analytics.on('identify', (ctx) => console.log(ctx))
364-
365452
analytics.on('track', (ctx) => console.log(ctx))
366-
```
367-
368-
369-
Use the emitter to log all HTTP Requests.
453+
analytics.on('error', (err) => console.error(err))
370454
371-
```javascript
372-
analytics.on('http_request', (event) => console.log(event))
373455
374-
// when triggered, emits an event of the shape:
456+
// when triggered, emits an event of the shape:
457+
analytics.on('http_request', (event) => console.log(event))
375458
{
376459
url: 'https://api.segment.io/v1/batch',
377460
method: 'POST',
@@ -389,7 +472,7 @@ Use the emitter to log all HTTP Requests.
389472
390473
| Emitter Type | Description |
391474
|-------------------|-----------------------------------------------------------------------------|
392-
| `error` | Emitted when there is an error during event delivery or SDK initialization. |
475+
| `error` | Emitted when there is an error after SDK initialization. |
393476
| `identify` | Emitted when an Identify call is made.
394477
| `track` | Emitted when a Track call is made.
395478
| `page` | Emitted when a Page call is made.

0 commit comments

Comments
 (0)