|
1 | 1 | ---
|
2 | 2 | title: 'Quickstart: Node.js'
|
3 | 3 | redirect_from: '/connections/sources/catalog/libraries/server/node-js/quickstart/'
|
| 4 | +strat: node-js |
4 | 5 | ---
|
5 | 6 |
|
6 |
| -This tutorial will help you start sending data from your Node servers to Segment and any of our destinations, using our Node library. As soon as you're set up you'll be able to turn on any new destinations with the flip of a switch! |
7 |
| - |
8 |
| -If you want to dive deeper at any point, check out the [Node library reference](/docs/connections/sources/catalog/libraries/server/node). |
9 |
| - |
10 |
| -## Step 1: Create a Source in the Segment app |
11 |
| - |
12 |
| -Before you begin, you need a Workspace (which is a container that holds all of the sources and destinations which are billed together for an organization). If you already created one, great! If not, you can sign up for a free Segment account and create one. |
13 |
| - |
14 |
| -Next, create a Node.js source from your Workspace: |
15 |
| - |
16 |
| -1. Click **Add Source**. |
17 |
| -2. From the source catalog page, click **Node.js**. |
18 |
| -3. Click **Add Source** again from the informational panel that appears to the right. |
19 |
| -4. Give the source a display name, and enter the URL the source will collect data from. |
20 |
| - |
21 |
| -When you create a Source in the Segment web app, it tells the Segment servers that you'll be sending data from a specific source type. When you create (or change!) a Source in the Segment app, Segment generates a new Write Key for that source. You use the write key in your code to tell the Segment servers where the data is coming from, so Segment can route it to your destinations and other tools. |
22 |
| - |
23 |
| -## Step 2: Install the Module |
24 |
| - |
25 |
| -Installing Segment is easy, just run the following npm command: |
26 |
| - |
27 |
| -``` |
28 |
| -npm install --save analytics-node |
29 |
| -``` |
30 |
| - |
31 |
| -That will add our Node library module to your `package.json`. The module exposes an `Analytics` constructor, which you need to initialize with your Segment project's **Write Key**, like so: |
32 |
| - |
33 |
| -```javascript |
34 |
| -var Analytics = require('analytics-node'); |
35 |
| -var analytics = new Analytics('YOUR_WRITE_KEY'); |
36 |
| -``` |
37 |
| - |
38 |
| -That 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). |
39 |
| - |
40 |
| -Once you've got that, you're ready to... |
41 |
| - |
42 |
| - |
43 |
| -## Step 3: Identify Users |
44 |
| - |
45 |
| -> note "" |
46 |
| -> **Good to know**: For any of the different methods described in this quickstart, you can replace the properties and traits in the code samples with variables that represent the data collected. |
47 |
| -
|
48 |
| -The `identify` method is how you tell Segment who the current user is. It includes a unique User ID and any optional traits you know about them. You can read more about it in the [identify reference](/docs/connections/sources/catalog/libraries/server/node#identify). |
49 |
| - |
50 |
| -Here's what a basic call to `identify` might look like: |
51 |
| - |
52 |
| -```js |
53 |
| -analytics.identify({ |
54 |
| - userId:'f4ca124298', |
55 |
| - traits: { |
56 |
| - name: 'Michael Bolton', |
57 |
| - |
58 |
| - createdAt: new Date('2014-06-14T02:00:19.467Z') |
59 |
| - } |
60 |
| -}); |
61 |
| -``` |
62 |
| - |
63 |
| -That's identifying Michael by his unique User ID (the one you know him by in your database) and labeling him with `name` and `email` traits. |
64 |
| - |
65 |
| -When you're using our Node library, you don't need to identify a user on every request they make to your servers. Instead, we recommend calling `identify` a single time when the user's account is first created, and only identifying again later when their traits are change. |
66 |
| - |
67 |
| -Once you've added an identify call, you can move on to... |
68 |
| - |
69 |
| - |
70 |
| -## Step 4: Track Actions |
71 |
| - |
72 |
| -The `track` method is how you tell Segment about which actions your users are performing. Every action triggers what we call an "event", which can also have associated properties. You can read more about `track` in the [track reference](/docs/connections/sources/catalog/libraries/server/node#track). |
73 |
| - |
74 |
| -Here's what a call to `track` might look like when a user signs up: |
75 |
| - |
76 |
| -```js |
77 |
| -analytics.track({ |
78 |
| - userId:'f4ca124298', |
79 |
| - event: 'Signed Up', |
80 |
| - properties: { |
81 |
| - plan: 'Enterprise' |
82 |
| - } |
83 |
| -}); |
84 |
| -``` |
85 |
| - |
86 |
| -That's just telling us that your user just triggered the **Signed Up** event and chose your hypothetical `'Enterprise'` plan. Properties can be anything you want to record, for example: |
87 |
| - |
88 |
| -```js |
89 |
| -analytics.track({ |
90 |
| - userId:'f4ca124298', |
91 |
| - event: 'Bookmarked Article', |
92 |
| - properties: { |
93 |
| - title: 'Snow Fall', |
94 |
| - subtitle: 'The Avalanche at Tunnel Creek', |
95 |
| - author: 'John Branch' |
96 |
| - } |
97 |
| -}); |
98 |
| -``` |
99 |
| - |
100 |
| -You'll want to track events that are indicators of success for your site, like **Signed Up**, **Item Purchased** or **Article Bookmarked**. |
101 |
| - |
102 |
| -To get started, we recommend tracking just a few important events. You can always add more later! |
103 |
| - |
104 |
| -Once you've added a few `track` calls, **you're done!** You successfully installed analytics tracking on your servers. Now you're ready to turn on any destination you fancy from our interface, margarita in hand. |
105 |
| - |
| 7 | +This tutorial will help you start sending data from your Node servers to Segment and any destination, using Segment's Node library. Check out the full documentation for [Analytics Node.js](/docs/connections/sources/catalog/libraries/server/node) to learn more. |
| 8 | + |
| 9 | +To get started with Analytics Node.js: |
| 10 | +1. Create a Node.js source in the Segment app. |
| 11 | + 1. Navigate to **Connections > Sources > Add Source**. |
| 12 | + 2. Search for **Node.js** from the source catalog and click **Node.js**. |
| 13 | + 3. Click **Add Source** again from the informational panel that appears to the right. |
| 14 | + 4. Give the source a display name, and enter the URL the source will collect data from. |
| 15 | + * When you create a Source in the Segment web app, it tells the Segment servers that you'll be sending data from a specific source type. When you create or change a Source in the Segment app, Segment generates a new Write Key for that source. You use the write key in your code to tell the Segment servers where the data is coming from, so Segment can route it to your destinations and other tools. |
| 16 | +2. Install the module. |
| 17 | + 1. Run the following npm command to install Segment: |
| 18 | + ``` |
| 19 | + npm install --save analytics-node |
| 20 | + ``` |
| 21 | +
|
| 22 | + This will add the Node library module to your `package.json`. The module exposes an `Analytics` constructor, which you need to initialize with your Segment project's **Write Key**, like so: |
| 23 | +
|
| 24 | + ```javascript |
| 25 | + var Analytics = require('analytics-node'); |
| 26 | + var analytics = new Analytics('YOUR_WRITE_KEY'); |
| 27 | + ``` |
| 28 | +
|
| 29 | + 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). |
| 30 | +3. Identify Users. |
| 31 | +
|
| 32 | + * **Note:** For any of the different methods described in this quickstart, you can replace the properties and traits in the code samples with variables that represent the data collected. |
| 33 | +
|
| 34 | +
|
| 35 | + The `identify` method is how you tell Segment who the current user is. It includes a unique User ID and any optional traits you know about them. You can read more about it in the [identify reference](/docs/connections/sources/catalog/libraries/server/node#identify). Here's what a basic call to `identify` might look like: |
| 36 | +
|
| 37 | + ```js |
| 38 | + analytics.identify({ |
| 39 | + userId:'f4ca124298', |
| 40 | + traits: { |
| 41 | + name: 'Michael Bolton', |
| 42 | + |
| 43 | + createdAt: new Date('2014-06-14T02:00:19.467Z') |
| 44 | + } |
| 45 | + }); |
| 46 | + ``` |
| 47 | +
|
| 48 | + This identifies Michael by his unique User ID (the one you know him by in your database) and labeling him with `name` and `email` traits. When you're using the Node library, you don't need to identify a user on every request they make to your servers. Instead, Segment recommends calling `identify` a single time when the user's account is first created, and only identifying again later when their traits change. |
| 49 | +4. Track Actions. |
| 50 | +
|
| 51 | + Segment recommends tracking just a few important events. You can always add more later. You should track events that are indicators of success for your site, like **Signed Up**, **Item Purchased** or **Article Bookmarked**. |
| 52 | +
|
| 53 | + <br> The `track` method is how you tell Segment about which actions your users are performing. Every action triggers what Segment calls an "event", which can also have associated properties. You can read more about `track` in the [track reference](/docs/connections/sources/catalog/libraries/server/node#track). |
| 54 | +
|
| 55 | + Here's what a call to `track` might look like when a user signs up: |
| 56 | +
|
| 57 | + ```js |
| 58 | + analytics.track({ |
| 59 | + userId:'f4ca124298', |
| 60 | + event: 'Signed Up', |
| 61 | + properties: { |
| 62 | + plan: 'Enterprise' |
| 63 | + } |
| 64 | + }); |
| 65 | + ``` |
| 66 | +
|
| 67 | + This tells Segment that your user just triggered the **Signed Up** event and chose your hypothetical `'Enterprise'` plan. Properties can be anything you want to record, for example: |
| 68 | +
|
| 69 | + ```js |
| 70 | + analytics.track({ |
| 71 | + userId:'f4ca124298', |
| 72 | + event: 'Bookmarked Article', |
| 73 | + properties: { |
| 74 | + title: 'Snow Fall', |
| 75 | + subtitle: 'The Avalanche at Tunnel Creek', |
| 76 | + author: 'John Branch' |
| 77 | + } |
| 78 | + }); |
| 79 | + ``` |
| 80 | +
|
| 81 | +After you've added a few `track` calls, you've successfully installed analytics tracking on your servers. Now you're ready to turn on any destination from the Segment app. |
106 | 82 |
|
107 | 83 | ---
|
108 | 84 |
|
109 |
| - |
110 | 85 | ## What's Next?
|
111 | 86 |
|
112 |
| -We just walked through the quickest way to get started with Segment using node. You might also want to check out our full [Node library reference](/docs/connections/sources/catalog/libraries/server/node) to see what else is possible, or read about the [Tracking API methods](/docs/connections/sources/catalog/libraries/server/http/) to get a sense for the bigger picture. |
| 87 | +You can check out the full docs for [Analytics Node.js](/docs/connections/sources/catalog/libraries/server/node) to see what else is possible, or read about the [Tracking API methods](/docs/connections/sources/catalog/libraries/server/http/) to get a sense for the bigger picture. |
113 | 88 |
|
114 |
| -You might also want to consider installing [Analytics.js](/docs/connections/sources/catalog/libraries/website/javascript/quickstart/) so that you can use destinations that require being loaded in the browser, like live chat tools or user feedback systems. |
| 89 | +You can also consider installing [Analytics.js](/docs/connections/sources/catalog/libraries/website/javascript/quickstart/) so that you can use destinations that require being loaded in the browser, like live chat tools or user feedback systems. |
0 commit comments