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-35Lines changed: 35 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ The [Segment Analytics Node.js Next library is open-source](https://github.com/s
12
12
13
13
All of Segment's server-side libraries are built for high-performance, so you can use them in your web server controller code. This library uses an internal queue to make Identify and Track calls non-blocking and fast. It also batches messages and flushes asynchronously to Segment's servers.
14
14
15
-
## Getting Started
15
+
## Getting started
16
16
17
17
> warning ""
18
18
> Make sure you're using a version of Node that's 18 or higher.
@@ -28,7 +28,7 @@ All of Segment's server-side libraries are built for high-performance, so you ca
28
28
pnpm add @segment/analytics-node
29
29
```
30
30
31
-
2. Initialize the `Analytics` constructor the module exposes with your Segment source**Write Key**, like so:
31
+
2. Initialize the `Analytics` constructor the module exposes with your Segment source**Write Key**:
32
32
33
33
```javascript
34
34
import { Analytics } from '@segment/analytics-node'
@@ -39,7 +39,7 @@ All of Segment's server-side libraries are built for high-performance, so you ca
39
39
const analytics = new Analytics({ writeKey: '<YOUR_WRITE_KEY>' })
40
40
```
41
41
42
-
Be sure to replace`YOUR_WRITE_KEY` with your actual **Write Key** which you can find in Segment by navigating to: **Connections > Sources** and selecting your source and going to the **Settings** tab.
42
+
Replace`YOUR_WRITE_KEY` with your actual **Write Key** which you can find in Segment by navigating to: **Connections > Sources** and selecting your source and going to the **Settings** tab.
43
43
44
44
This creates 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.
45
45
@@ -52,7 +52,7 @@ These methods correspond with those used in the [Segment Spec](/docs/connections
52
52
53
53
### Identify
54
54
55
-
> info "Good to know"
55
+
> info ""
56
56
> For any of the different methods described on this page, you can replace the properties and traits in the code samples with variables that represent the data collected.
57
57
58
58
Identify lets you tie a user to their actions and record traits about them. It includes a unique User ID and/or anonymous ID, and any optional traits you know about them.
@@ -85,27 +85,27 @@ analytics.identify({
85
85
}
86
86
});
87
87
```
88
-
The call above identifies Michael by his unique User ID (the one you know him by in your database), and labels him with the `name`, `email`, `plan` and `friends` traits.
88
+
The call above identifies Michael by his unique User ID (the one you know him by in your database), and labels him with the `name`, `email`, `plan`, and `friends` traits.
89
89
90
90
An Identify call has the following fields:
91
91
92
92
Field | Details
93
93
----- | -------
94
-
`userId` _String, optional_ | The ID for this user in your database. _Note: at least one of `userId` or `anonymousId` must be included in any identify call._
95
-
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example, [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id)). _Note: You must include at least one of `userId` or `anonymousId` in all identify calls._
96
-
`traits` _Object, optional_ | A dictionary of [traits](/docs/connections/spec/identify#traits) you know about the user. Things like: `email`, `name` or `friends`.
94
+
`userId` _String, optional_ | The ID for this user in your database. _Note: at least one of `userId` or `anonymousId` must be included in any Identify call._
95
+
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example, [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id)). _Note: You must include at least one of `userId` or `anonymousId` in all Identify calls._
96
+
`traits` _Object, optional_ | A dictionary of [traits](/docs/connections/spec/identify#traits) you know about the user. Things like: `email`, `name`, or `friends`.
97
97
`timestamp` _Date, optional_ | A JavaScript date object representing when the identify took place. If the identify just happened, leave it out as Segment uses the server's time. If you're importing data from the past make sure to send a `timestamp`.
98
98
`context` _Object, optional_ | A dictionary of extra [context](/docs/connections/spec/common/#context) to attach to the call. _Note: `context` differs from `traits` because it is not attributes of the user itself._
99
99
100
-
Find details on the **identify method payload**in Segment's [Spec](/docs/connections/spec/identify/).
100
+
Find details on the **Identify method payload**in Segment's [Spec](/docs/connections/spec/identify/).
101
101
102
102
### Track
103
103
104
104
Track lets you record the actions your users perform. Every action triggers what Segment calls an "event", which can also have associated properties.
105
105
106
-
You'll want to track events that are indicators of success for your site, like **Signed Up**, **Item Purchased** or **Article Bookmarked**.
106
+
You'll want to track events that are indicators of success for your site, like **Signed Up**, **Item Purchased**, or **Article Bookmarked**.
107
107
108
-
To get started, Segment recommends tracking just a few important events. You can always add more later.
108
+
To get started, we recommend tracking just a few important events. You can always add more later.
109
109
110
110
Example anonymous Track call:
111
111
@@ -141,8 +141,8 @@ The Track call has the following fields:
141
141
142
142
Field | Details
143
143
----- | --------
144
-
`userId` _String, optional_ | The ID forthis userin your database. _Note: at least one of `userId` or `anonymousId` must be included in any track call.
145
-
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example, [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id)). _Note: You must include at least one of `userId` or `anonymousId` in all track calls._
144
+
`userId` _String, optional_ | The ID forthis userin your database. _Note: at least one of `userId` or `anonymousId` must be included in any Track call.
145
+
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example, [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id)). _Note: You must include at least one of `userId` or `anonymousId` in all Track calls._
146
146
`event` _String_ | The name of the event you're tracking. We recommend human-readable names like `Song Played` or `Status Updated`.
147
147
`properties` _Object, optional_ | A dictionary of properties for the event. If the event was `Product Added`, it might have properties like `price` or `product`.
148
148
`timestamp` _Date, optional_ | A JavaScript date object representing when the track took place. If the track just happened, leave it out and we'll use the server's time. If you're importing data from the past make sure you to send a `timestamp`.
@@ -154,7 +154,7 @@ Find details on **best practices in event naming** as well as the **Track method
154
154
155
155
The [Page](/docs/connections/spec/page/) method lets you record page views on your website, along with optional extra information about the page being viewed.
156
156
157
-
If you're using Segment's client-side set up in combination with the Node.js library, page calls are **already tracked for you** by default. However, if you want to record your own page views manually and aren't using the client-side library, read on.
157
+
If you're using Segment's client-side set up in combination with the Node.js library, Page calls are **already tracked for you** by default. However, if you want to record your own page views manually and aren't using the client-side library, read on.
158
158
159
159
Example Page call:
160
160
@@ -176,11 +176,11 @@ A Page call has the following fields:
176
176
177
177
Field | Details
178
178
----- | --------
179
-
`userId` _String, optional_ | The ID forthis userin your database. _Note: at least one of `userId` or `anonymousId` must be included in any page call.
180
-
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example, [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id)). _Note: at least one of `userId` or `anonymousId` must be included in any page call._
179
+
`userId` _String, optional_ | The ID forthis userin your database. _Note: at least one of `userId` or `anonymousId` must be included in any Page call.
180
+
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example, [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id)). _Note: at least one of `userId` or `anonymousId` must be included in any Page call._
181
181
`category` _String, optional_ | The category of the page. Useful for industries, like ecommerce, where many pages often live under a larger category.
182
182
`name` _String, optional_ | The name of the page, for example **Signup** or **Home**.
183
-
`properties` _Object, optional_ | A dictionary of properties of the page. A few properties specially recognized and automatically translated: `url`, `title`, `referrer` and `path`, but you can add your own too.
183
+
`properties` _Object, optional_ | A dictionary of properties of the page. A few properties specially recognized and automatically translated: `url`, `title`, `referrer`, and `path`, but you can add your own too.
184
184
`timestamp` _Date, optional_ | A JavaScript date object representing when the Page took place. If the Page just happened, leave it out and Segment will use the server's time. If you're importing data from the past make sure you to send a `timestamp`.
185
185
`context` _Object, optional_ | A dictionary of extra [context](docs/connections/spec/common/#context) to attach to the call. _Note: `context` differs from `traits` because it is not attributes of the user itself._
186
186
@@ -190,7 +190,7 @@ Find details on the **Page payload** in the [Segment Spec](/docs/connections/spe
190
190
191
191
Group lets you associate an [identified user](/docs/connections/sources/catalog/libraries/server/node/#identify) with a group. A group could be a company, organization, account, project or team. It also lets you record custom traits about the group, like industry or number of employees.
192
192
193
-
This is useful for tools like [Intercom](/docs/connections/destinations/catalog/intercom/), [Preact](/docs/connections/destinations/catalog/preact/) and [Totango](/docs/connections/destinations/catalog/totango/), as it ties the user to a **group** of other users.
193
+
This is useful for tools like [Intercom](/docs/connections/destinations/catalog/intercom/), [Preact](/docs/connections/destinations/catalog/preact/), and [Totango](/docs/connections/destinations/catalog/totango/), as it ties the user to a **group** of other users.
194
194
195
195
Example Group call:
196
196
@@ -209,21 +209,21 @@ The Group call has the following fields:
209
209
210
210
Field | Details
211
211
----- | --------
212
-
`userId` _String, optional_ | The ID for this user in your database. _Note: at least one of `userId` or `anonymousId` must be included in any group call.
213
-
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are (for example), [the anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id). _Note: at least one of `userId` or `anonymousId` must be included in any group call._
212
+
`userId` _String, optional_ | The ID for this user in your database. _Note: at least one of `userId` or `anonymousId` must be included in any Group call.
213
+
`anonymousId` _String, optional_ | An ID associated with the user when you don't know who they are. For example, the [anonymousId generated by `analytics.js`](/docs/connections/sources/catalog/libraries/website/javascript/#anonymous-id). _Note: at least one of `userId` or `anonymousId` must be included in any Group call._
214
214
`groupId` _string | The ID of the group.
215
215
`traits` _dict, optional_ | 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).
216
-
`context` _dict, optional_ | A dict containing any context about the request. To see the full reference of supported keys, check them out in the [context reference](/docs/connections/spec/common/#context)
217
-
`timestamp` _datetime, optional_ | A `datetime` object representing when the Group took place. If the Group just happened, leave it out and Segment will use the server's time. If you're importing data from the past make sure you send `timestamp`.
216
+
`context` _dict, optional_ | A dict containing any context about the request. To see the full reference of supported keys, check them out in the [context reference](/docs/connections/spec/common/#context).
217
+
`timestamp` _datetime, optional_ | A `datetime` object representing when the Group call took place. If the Group call just happened, leave it out and Segment will use the server's time. If you're importing data from the past make sure you send `timestamp`.
218
218
`integrations` _dict, optional_ | A dictionary of destinations to enable or disable.
219
219
220
220
Find more details about Group, including the **Group payload**, in the [Segment Spec](/docs/connections/spec/group/).
221
221
222
222
### Alias
223
223
224
-
The Alias call allows you to associate one identity with another. This is an advanced method and should not be widely used, but is required to manage user identities in _some_ destinations. Other destinations do not support the alias call.
224
+
The Alias call allows you to associate one identity with another. This is an advanced method and should not be widely used, but is required to manage user identities in _some_ destinations. Other destinations do not support the Alias call.
225
225
226
-
In [Mixpanel](/docs/connections/destinations/catalog/mixpanel/#alias) it's used to associate an anonymous user with an identified user once they sign up. For [Kissmetrics](/docs/connections/destinations/catalog/kissmetrics/#alias), if your user switches IDs, you can use 'alias' to rename the 'userId'.
226
+
In [Mixpanel](/docs/connections/destinations/catalog/mixpanel/#alias) it's used to associate an anonymous user with an identified user once they sign up. For [Kissmetrics](/docs/connections/destinations/catalog/kissmetrics/#alias), if your user switches IDs, you can use the Alias call to rename the `userId`.
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.
540
540
@@ -556,19 +556,19 @@ In this case, Segment specifies that they want this Track event to only go to Ve
556
556
557
557
Destination flags are **case sensitive** and match [the destination's name in the docs](/docs/connections/destinations/) (for example, "AdLearn Open Platform", "awe.sm", or "MailChimp"). In some cases, there may be several names for a destination; if that happens you'll see a "Adding (destination name) to the Integrations Object" section in the destination's doc page with a list of valid names.
558
558
559
-
**Note:**
559
+
Keep in mind:
560
560
561
-
- Business Tier users can filter Track calls right from the Segment UI on your source schema page. Segment recommends using the UI if possible since it's a much simpler way of managing your filters and can be updated with no code changes on your side.
561
+
- Business Tier users can filter Track calls right from the Segment UI on your source schema page. We recommend using the UI if possible since it's a much simpler way of managing your filters and can be updated with no code changes on your side.
562
562
563
563
- If you are on a grandfathered plan, events sent server-side that are filtered through the Segment dashboard still count towards your API usage.
564
564
565
-
## Historical Import
565
+
## Historical import
566
566
567
567
You can import historical data by adding the `timestamp` argument to any of your method calls. This can be helpful if you've just switched to Segment.
568
568
569
-
Historical imports can only be done into destinations that can accept historical timestamped data. Most analytics tools like Mixpanel, Amplitude, Kissmetrics, etc. can handle that type of data just fine. One common destination that does not accept historical data is Google Analytics since their API cannot accept historical data.
569
+
Historical imports can only be done into destinations that can accept historical timestamped data. Most analytics tools like Mixpanel, Amplitude, and Kissmetrics can handle that type of data just fine. One common destination that does not accept historical data is Google Analytics since their API cannot accept historical data.
570
570
571
-
**Note:** If you're tracking things that are happening right now, leave out the `timestamp` and Segment's servers will timestamp the requests for you.
571
+
**Note**: If you're tracking things that are happening right now, leave out the `timestamp` and Segment's servers will timestamp the requests for you.
572
572
573
573
574
574
## Batching
@@ -582,7 +582,7 @@ By default, Segment's library will flush:
582
582
- Every 15 messages (controlled by `settings.flushAt`).
583
583
- If 10 seconds has passed since the last flush (controlled by `settings.flushInterval`)
584
584
585
-
There is a maximum of `500KB` per batch request and `32KB` per call.
585
+
There is a maximum of `500 KB` per batch request and `32 KB` per call.
586
586
587
587
If you don't want to batch messages, you can turn batching off by setting the `flushAt` setting to `1`, like so:
588
588
@@ -606,7 +606,7 @@ analytics.track({
606
606
)
607
607
```
608
608
609
-
## Multiple Clients
609
+
## Multiple clients
610
610
611
611
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:
Segment attempts to use the global `fetch` implementation if available in order to support several diverse environments. Some special cases (for example, http proxy) may require a different implementation for http communication. You can provide a customized wrapper in the Analytics configuration to support this. Here are a few approaches:
620
620
621
-
Use a custom fetch-like implementation with proxy (simple, recommended)
621
+
Use a custom fetch-like implementation with proxy (Recommended)
0 commit comments