Skip to content

Commit 82bc27b

Browse files
authored
Update node README (#1205)
1 parent 1c83289 commit 82bc27b

File tree

1 file changed

+1
-114
lines changed

1 file changed

+1
-114
lines changed

packages/node/README.md

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -54,123 +54,10 @@ app.post('/cart', (req, res) => {
5454
res.sendStatus(201)
5555
});
5656
```
57+
See our [official documentation](https://segment.com/docs/connections/sources/catalog/libraries/server/node) for more examples and information.
5758

5859

5960
## Settings & Configuration
6061
See the documentation: https://segment.com/docs/connections/sources/catalog/libraries/server/node/#configuration
6162

6263
You can also see the complete list of settings in the [AnalyticsSettings interface](src/app/settings.ts).
63-
64-
65-
## Plugin Architecture
66-
- See segment's [documentation for plugin architecture](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugin-architecture).
67-
68-
69-
70-
## Usage in non-node runtimes
71-
### Usage in AWS Lambda
72-
- [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.
73-
74-
Here is an example of using analytics.js within a handler:
75-
```ts
76-
const { Analytics } = require('@segment/analytics-node');
77-
78-
// since analytics has the potential to be stateful if there are any plugins added,
79-
// to be on the safe side, we should instantiate a new instance of analytics on every request (the cost of instantiation is low).
80-
const analytics = () => new Analytics({
81-
flushAt: 1,
82-
writeKey: '<MY_WRITE_KEY>',
83-
})
84-
.on('error', console.error);
85-
86-
module.exports.handler = async (event) => {
87-
...
88-
// we need to await before returning, otherwise the lambda will exit before sending the request.
89-
await new Promise((resolve) =>
90-
analytics().track({ ... }, resolve)
91-
)
92-
93-
...
94-
return {
95-
statusCode: 200,
96-
};
97-
....
98-
};
99-
```
100-
101-
### Usage in Vercel Edge Functions
102-
```ts
103-
import { Analytics } from '@segment/analytics-node';
104-
import { NextRequest, NextResponse } from 'next/server';
105-
106-
export const analytics = new Analytics({
107-
writeKey: '<MY_WRITE_KEY>',
108-
flushAt: 1,
109-
})
110-
.on('error', console.error)
111-
112-
export const config = {
113-
runtime: 'edge',
114-
};
115-
116-
export default async (req: NextRequest) => {
117-
await new Promise((resolve) =>
118-
analytics.track({ ... }, resolve)
119-
);
120-
return NextResponse.json({ ... })
121-
};
122-
```
123-
124-
### Usage in Cloudflare Workers
125-
```ts
126-
import { Analytics, Context } from '@segment/analytics-node';
127-
128-
export default {
129-
async fetch(
130-
request: Request,
131-
env: Env,
132-
ctx: ExecutionContext
133-
): Promise<Response> {
134-
const analytics = new Analytics({
135-
flushAt: 1,
136-
writeKey: '<MY_WRITE_KEY>',
137-
}).on('error', console.error);
138-
139-
await new Promise((resolve, reject) =>
140-
analytics.track({ ... }, resolve)
141-
);
142-
143-
...
144-
return new Response(...)
145-
},
146-
};
147-
148-
```
149-
150-
### OAuth 2
151-
In order to guarantee authorized communication between your server environment and Segment's Tracking API, you can [enable OAuth 2 in your Segment workspace](https://segment.com/docs/partners/enable-with-segment/). To support the non-interactive server environment, the OAuth workflow used is a signed client assertion JWT. You will need a public and private key pair where the public key is uploaded to the segment dashboard and the private key is kept in your server environment to be used by this SDK. Your server will verify its identity by signing a token request and will receive a token that is used to to authorize all communication with the Segment Tracking API.
152-
153-
You will also need to provide the OAuth Application ID and the public key's ID, both of which are provided in the Segment dashboard. You should ensure that you are implementing handling for Analytics SDK errors. Good logging will help distinguish any configuration issues.
154-
155-
```ts
156-
import { Analytics, OAuthSettings } from '@segment/analytics-node';
157-
import { readFileSync } from 'fs'
158-
159-
const privateKey = readFileSync('private.pem', 'utf8')
160-
161-
const settings: OAuthSettings = {
162-
clientId: '<CLIENT_ID_FROM_DASHBOARD>',
163-
clientKey: privateKey,
164-
keyId: '<PUB_KEY_ID_FROM_DASHBOARD>',
165-
}
166-
167-
const analytics = new Analytics({
168-
writeKey: '<MY_WRITE_KEY>',
169-
oauthSettings: settings,
170-
})
171-
172-
analytics.on('error', (err) => { console.error(err) })
173-
174-
analytics.track({ userId: 'foo', event: 'bar' })
175-
176-
```

0 commit comments

Comments
 (0)