Skip to content

Commit e1a910f

Browse files
committed
update node README
1 parent c3dbd52 commit e1a910f

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

packages/node/README.md

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,20 @@ Here is an example of using analytics.js within a handler:
7575
```ts
7676
const { Analytics } = require('@segment/analytics-node');
7777

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,
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({
8280
writeKey: '<MY_WRITE_KEY>',
83-
})
84-
.on('error', console.error);
81+
}).on('error', console.error);
8582

8683
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-
)
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()
9291

93-
...
9492
return {
9593
statusCode: 200,
9694
};
@@ -99,48 +97,56 @@ module.exports.handler = async (event) => {
9997
```
10098

10199
### Usage in Vercel Edge Functions
100+
102101
```ts
103102
import { Analytics } from '@segment/analytics-node';
104103
import { NextRequest, NextResponse } from 'next/server';
105104

106-
export const analytics = new Analytics({
105+
const createAnalytics = () => new Analytics({
107106
writeKey: '<MY_WRITE_KEY>',
108-
flushAt: 1,
109-
})
110-
.on('error', console.error)
107+
}).on('error', console.error)
111108

112109
export const config = {
113110
runtime: 'edge',
114111
};
115112

116113
export default async (req: NextRequest) => {
117-
await new Promise((resolve) =>
118-
analytics.track({ ... }, resolve)
119-
);
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+
120122
return NextResponse.json({ ... })
121123
};
122124
```
123125

124126
### Usage in Cloudflare Workers
127+
125128
```ts
126129
import { Analytics, Context } from '@segment/analytics-node';
127130

131+
132+
const createAnalytics = () => new Analytics({
133+
writeKey: '<MY_WRITE_KEY>',
134+
}).on('error', console.error);
135+
128136
export default {
129137
async fetch(
130138
request: Request,
131139
env: Env,
132140
ctx: ExecutionContext
133141
): Promise<Response> {
134-
const analytics = new Analytics({
135-
flushAt: 1,
136-
writeKey: '<MY_WRITE_KEY>',
137-
}).on('error', console.error);
142+
const analytics = createAnalytics()
138143

139-
await new Promise((resolve, reject) =>
140-
analytics.track({ ... }, resolve)
141-
);
144+
analytics.identify({ ... })
145+
analytics.track({ ... })
146+
147+
// ensure analytics events get sent before program exits
148+
await analytics.flush()
142149

143-
...
144150
return new Response(...)
145151
},
146152
};
@@ -167,10 +173,7 @@ const settings: OAuthSettings = {
167173
const analytics = new Analytics({
168174
writeKey: '<MY_WRITE_KEY>',
169175
oauthSettings: settings,
170-
})
171-
172-
analytics.on('error', (err) => { console.error(err) })
176+
}).on('error', console.error)
173177

174178
analytics.track({ userId: 'foo', event: 'bar' })
175-
176179
```

0 commit comments

Comments
 (0)