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
> warning "Deprecation of Analytics Node.js Classic"
9
-
> On [date], Segment will end support for Analytics Node.js Classic, which includes versions [1.x.x] and older. Upgrade to Analytics Node.js 2.0. See the [Analytics Node.js 2.0] docs to learn more.
10
+
> On [date], Segment will end support for Analytics Node.js Classic, which includes versions [1.x.x] and older. Upgrade to the new version of Analytics Node.js. See the updated [Analytics Node.js] docs to learn more.
10
11
11
12
Segment's Node.js library lets you record analytics data from your node code. The requests hit Segment's servers, and then Segment routes your data to any destinations you have enabled.
@@ -41,18 +41,6 @@ Be sure to replace `YOUR_WRITE_KEY` with your actual **Write Key** which you can
41
41
42
42
This will create an instance of `Analytics` that you can use to send data to Segment for your project. The default initialization settings are production-ready and queue 20 messages before sending any requests. In development you might want to use [development settings](/docs/connections/sources/catalog/libraries/server/node/#development).
43
43
44
-
### Regional configuration
45
-
For Business plans with access to [Regional Segment](/docs/guides/regional-segment), you can use the `host` configuration parameter to send data to the desired region:
46
-
1. Oregon (Default) — `api.segment.io/v1`
47
-
2. Dublin — `events.eu1.segmentapis.com`
48
-
49
-
An example of setting the host to the EU endpoint using the Node library is:
50
-
```javascript
51
-
constanalytics=newAnalytics({
52
-
...
53
-
host:"https://events.eu1.segmentapis.com"
54
-
});
55
-
```
56
44
57
45
## Basic tracking methods
58
46
The basic tracking methods below serve as the building blocks of your Segment tracking. They include [Identify](#identify), [Track](#track), [Page](#page), [Group](#group), and [Alias](#alias).
@@ -286,7 +274,7 @@ The `group` call has the following fields:
286
274
</tr>
287
275
<tr>
288
276
<td>`traits` _dict, optional_</td>
289
-
<td>A dict of traits you know about the group. For a company, they might be things like `name`, `address`, or `phone`.</td>
277
+
<td>A dict of traits you know about the group. For a company, they might be things like `name`, `address`, or `phone`. [Learn more about traits](/docs/connections/spec/group/#traits).</td>
console.log(unflushedEvents) // all events that came in after closeAndFlush was called
413
401
```
414
402
403
+
## Regional configuration
404
+
For Business plans with access to [Regional Segment](/docs/guides/regional-segment), you can use the `host` configuration parameter to send data to the desired region:
405
+
1. Oregon (Default) — `api.segment.io/v1`
406
+
2. Dublin — `events.eu1.segmentapis.com`
407
+
408
+
An example of setting the host to the EU endpoint using the Node library is:
409
+
```javascript
410
+
constanalytics=newAnalytics({
411
+
...
412
+
host:"https://events.eu1.segmentapis.com"
413
+
});
414
+
```
415
+
415
416
## Error handling
416
417
417
418
To keep track of errors, subscribe and log all event delivery errors by running:
You can use this initialization during development to make the library flush every time a message is submitted, so that you can be sure your calls are working properly before pushing to production.
567
-
568
-
```javascript
569
-
var analytics = new Analytics('YOUR_WRITE_KEY', { flushAt: 1 });
570
529
```
571
530
572
-
573
531
## Selecting Destinations
574
532
575
533
The `alias`, `group`, `identify`, `page` and `track` calls can all be passed an object of `integrations` that lets you turn certain destinations on or off. By default all destinations are enabled.
You should call `client.track(...)` and know that events will be queued and eventually sent to Segment. To prevent losing messages, be sure to capture any interruption (for example, a server restart) and call flush to know of and delay the process shutdown.
656
-
657
-
```js
658
-
import { randomUUID } from 'crypto';
659
-
import Analytics from 'analytics-node'
660
-
661
-
const WRITE_KEY = '...';
662
-
663
-
const analytics = new Analytics(WRITE_KEY, { flushAt: 10 });
664
-
665
-
analytics.track({
666
-
anonymousId: randomUUID(),
667
-
event: 'Test event',
668
-
properties: {
669
-
name: 'Test event',
670
-
timestamp: new Date()
671
-
}
672
-
});
673
-
674
-
const exitGracefully = async (code) => {
675
-
console.log('Flushing events');
676
-
await analytics.flush(function(err, batch) {
677
-
console.log('Flushed, and now this program can exit!');
Short-lived functions have a predictably short and linear lifecycle, so use a queue big enough to hold all messages and then await flush to complete its work. -->
702
-
703
-
704
-
<!--```js
705
-
import { randomUUID } from 'crypto';
706
-
import Analytics from 'analytics-node'
707
-
708
-
709
-
async function lambda()
710
-
{
711
-
const WRITE_KEY = '...';
712
-
const analytics = new Analytics(WRITE_KEY, { flushAt: 20 });
713
-
analytics.flushed = true;
714
-
715
-
analytics.track({
716
-
anonymousId: randomUUID(),
717
-
event: 'Test event',
718
-
properties: {
719
-
name: 'Test event',
720
-
timestamp: new Date()
721
-
}
722
-
});
723
-
await analytics.flush(function(err, batch) {
724
-
console.log('Flushed, and now this program can exit!');
725
-
});
726
-
}
727
-
728
-
lambda();
729
-
```-->
730
-
731
-
732
611
## Multiple Clients
733
612
734
613
Different parts of your application may require different types of batching, or even sending to multiple Segment sources. In that case, you can initialize multiple instances of `Analytics` with different settings:
0 commit comments