Skip to content

Commit 5249045

Browse files
authored
Merge pull request #7894 from segmentio/analytics-for-node.js
Edits to Analytics for Node.js
2 parents 7968df3 + 20e7878 commit 5249045

File tree

1 file changed

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

1 file changed

+35
-35
lines changed

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The [Segment Analytics Node.js Next library is open-source](https://github.com/s
1212

1313
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.
1414

15-
## Getting Started
15+
## Getting started
1616

1717
> warning ""
1818
> 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
2828
pnpm add @segment/analytics-node
2929
```
3030

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**:
3232

3333
```javascript
3434
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
3939
const analytics = new Analytics({ writeKey: '<YOUR_WRITE_KEY>' })
4040
```
4141
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.
4343
4444
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.
4545
@@ -52,7 +52,7 @@ These methods correspond with those used in the [Segment Spec](/docs/connections
5252
5353
### Identify
5454
55-
> info "Good to know"
55+
> info ""
5656
> 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.
5757
5858
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({
8585
}
8686
});
8787
```
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.
8989
9090
An Identify call has the following fields:
9191
9292
Field | Details
9393
----- | -------
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`.
9797
`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`.
9898
`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._
9999
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/).
101101
102102
### Track
103103
104104
Track lets you record the actions your users perform. Every action triggers what Segment calls an "event", which can also have associated properties.
105105
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**.
107107
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.
109109
110110
Example anonymous Track call:
111111
@@ -141,8 +141,8 @@ The Track call has the following fields:
141141
142142
Field | Details
143143
----- | --------
144-
`userId` _String, optional_ | The ID for this user in 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 for this user in 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._
146146
`event` _String_ | The name of the event you're tracking. We recommend human-readable names like `Song Played` or `Status Updated`.
147147
`properties` _Object, optional_ | A dictionary of properties for the event. If the event was `Product Added`, it might have properties like `price` or `product`.
148148
`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
154154
155155
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.
156156
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.
158158
159159
Example Page call:
160160
@@ -176,11 +176,11 @@ A Page call has the following fields:
176176
177177
Field | Details
178178
----- | --------
179-
`userId` _String, optional_ | The ID for this user in 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 for this user in 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._
181181
`category` _String, optional_ | The category of the page. Useful for industries, like ecommerce, where many pages often live under a larger category.
182182
`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.
184184
`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`.
185185
`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._
186186
@@ -190,7 +190,7 @@ Find details on the **Page payload** in the [Segment Spec](/docs/connections/spe
190190
191191
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.
192192
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.
194194
195195
Example Group call:
196196
@@ -209,21 +209,21 @@ The Group call has the following fields:
209209
210210
Field | Details
211211
----- | --------
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._
214214
`groupId` _string | The ID of the group.
215215
`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`.
218218
`integrations` _dict, optional_ | A dictionary of destinations to enable or disable.
219219
220220
Find more details about Group, including the **Group payload**, in the [Segment Spec](/docs/connections/spec/group/).
221221
222222
### Alias
223223
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.
225225
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`.
227227
228228
Example Alias call:
229229
@@ -464,11 +464,11 @@ analytics.on('http_request', (event) => console.log(event))
464464
}
465465
```
466466
467-
### Emitter Types
467+
### Emitter types
468468
469469
The following table documents all the emitter types available in the Analytics Node.js library:
470470
471-
| Emitter Type | Description |
471+
| Emitter type | Description |
472472
|-------------------|-----------------------------------------------------------------------------|
473473
| `error` | Emitted when there is an error after SDK initialization. |
474474
| `identify` | Emitted when an Identify call is made.
@@ -534,7 +534,7 @@ Deregister a plugin by using:
534534
await analytics.deregister("pluginNameA", "pluginNameB") // takes strings
535535
```
536536
537-
## Selecting Destinations
537+
## Selecting destinations
538538
539539
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.
540540
@@ -556,19 +556,19 @@ In this case, Segment specifies that they want this Track event to only go to Ve
556556
557557
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.
558558
559-
**Note:**
559+
Keep in mind:
560560
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.
562562
563563
- If you are on a grandfathered plan, events sent server-side that are filtered through the Segment dashboard still count towards your API usage.
564564
565-
## Historical Import
565+
## Historical import
566566
567567
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.
568568
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.
570570
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.
572572
573573
574574
## Batching
@@ -582,7 +582,7 @@ By default, Segment's library will flush:
582582
- Every 15 messages (controlled by `settings.flushAt`).
583583
- If 10 seconds has passed since the last flush (controlled by `settings.flushInterval`)
584584
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.
586586
587587
If you don't want to batch messages, you can turn batching off by setting the `flushAt` setting to `1`, like so:
588588
@@ -606,7 +606,7 @@ analytics.track({
606606
)
607607
```
608608
609-
## Multiple Clients
609+
## Multiple clients
610610
611611
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:
612612
@@ -618,7 +618,7 @@ const appAnalytics = new Analytics({ writeKey: 'APP_WRITE_KEY' });
618618
619619
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:
620620
621-
Use a custom fetch-like implementation with proxy (simple, recommended)
621+
Use a custom fetch-like implementation with proxy (Recommended)
622622
```javascript
623623
import { HTTPFetchFn } from '../lib/http-client'
624624
import axios from 'axios'

0 commit comments

Comments
 (0)