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
+97-14Lines changed: 97 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -291,7 +291,97 @@ See the complete `AnalyticsSettings` interface [in the analytics-next repository
291
291
292
292
## Usage in serverless environments
293
293
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:
// 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
+
```
295
385
296
386
## Graceful shutdown
297
387
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.
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.
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.
0 commit comments