Skip to content

Commit a963b99

Browse files
committed
wip
1 parent e1a910f commit a963b99

File tree

1 file changed

+1
-117
lines changed

1 file changed

+1
-117
lines changed

packages/node/README.md

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -54,126 +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-
// Preferable to create a new analytics instance per-invocation. Otherwise, we may get a warning about overlapping flush calls. Also, custom plugins have the potential to be stateful, so we prevent those kind of race conditions.
79-
const createAnalytics = () => new Analytics({
80-
writeKey: '<MY_WRITE_KEY>',
81-
}).on('error', console.error);
82-
83-
module.exports.handler = async (event) => {
84-
const analytics = createAnalytics()
85-
86-
analytics.identify({ ... })
87-
analytics.track({ ... })
88-
89-
// ensure analytics events get sent before program exits
90-
await analytics.flush()
91-
92-
return {
93-
statusCode: 200,
94-
};
95-
....
96-
};
97-
```
98-
99-
### Usage in Vercel Edge Functions
100-
101-
```ts
102-
import { Analytics } from '@segment/analytics-node';
103-
import { NextRequest, NextResponse } from 'next/server';
104-
105-
const createAnalytics = () => new Analytics({
106-
writeKey: '<MY_WRITE_KEY>',
107-
}).on('error', console.error)
108-
109-
export const config = {
110-
runtime: 'edge',
111-
};
112-
113-
export default async (req: NextRequest) => {
114-
const analytics = createAnalytics()
115-
116-
analytics.identify({ ... })
117-
analytics.track({ ... })
118-
119-
// ensure analytics events get sent before program exits
120-
await analytics.flush()
121-
122-
return NextResponse.json({ ... })
123-
};
124-
```
125-
126-
### Usage in Cloudflare Workers
127-
128-
```ts
129-
import { Analytics, Context } from '@segment/analytics-node';
130-
131-
132-
const createAnalytics = () => new Analytics({
133-
writeKey: '<MY_WRITE_KEY>',
134-
}).on('error', console.error);
135-
136-
export default {
137-
async fetch(
138-
request: Request,
139-
env: Env,
140-
ctx: ExecutionContext
141-
): Promise<Response> {
142-
const analytics = createAnalytics()
143-
144-
analytics.identify({ ... })
145-
analytics.track({ ... })
146-
147-
// ensure analytics events get sent before program exits
148-
await analytics.flush()
149-
150-
return new Response(...)
151-
},
152-
};
153-
154-
```
155-
156-
### OAuth 2
157-
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.
158-
159-
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.
160-
161-
```ts
162-
import { Analytics, OAuthSettings } from '@segment/analytics-node';
163-
import { readFileSync } from 'fs'
164-
165-
const privateKey = readFileSync('private.pem', 'utf8')
166-
167-
const settings: OAuthSettings = {
168-
clientId: '<CLIENT_ID_FROM_DASHBOARD>',
169-
clientKey: privateKey,
170-
keyId: '<PUB_KEY_ID_FROM_DASHBOARD>',
171-
}
172-
173-
const analytics = new Analytics({
174-
writeKey: '<MY_WRITE_KEY>',
175-
oauthSettings: settings,
176-
}).on('error', console.error)
177-
178-
analytics.track({ userId: 'foo', event: 'bar' })
179-
```

0 commit comments

Comments
 (0)