Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update node README #1205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions packages/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,20 @@ Here is an example of using analytics.js within a handler:
```ts
const { Analytics } = require('@segment/analytics-node');

// since analytics has the potential to be stateful if there are any plugins added,
// to be on the safe side, we should instantiate a new instance of analytics on every request (the cost of instantiation is low).
const analytics = () => new Analytics({
flushAt: 1,
// 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.
const createAnalytics = () => new Analytics({
writeKey: '<MY_WRITE_KEY>',
})
.on('error', console.error);
}).on('error', console.error);

module.exports.handler = async (event) => {
...
// we need to await before returning, otherwise the lambda will exit before sending the request.
await new Promise((resolve) =>
analytics().track({ ... }, resolve)
)
const analytics = createAnalytics()

analytics.identify({ ... })
analytics.track({ ... })

// ensure analytics events get sent before program exits
await analytics.flush()

...
return {
statusCode: 200,
};
Expand All @@ -99,48 +97,56 @@ module.exports.handler = async (event) => {
```

### Usage in Vercel Edge Functions

```ts
import { Analytics } from '@segment/analytics-node';
import { NextRequest, NextResponse } from 'next/server';

export const analytics = new Analytics({
const createAnalytics = () => new Analytics({
writeKey: '<MY_WRITE_KEY>',
flushAt: 1,
})
.on('error', console.error)
}).on('error', console.error)

export const config = {
runtime: 'edge',
};

export default async (req: NextRequest) => {
await new Promise((resolve) =>
analytics.track({ ... }, resolve)
);
const analytics = createAnalytics()

analytics.identify({ ... })
analytics.track({ ... })

// ensure analytics events get sent before program exits
await analytics.flush()

return NextResponse.json({ ... })
};
```

### Usage in Cloudflare Workers

```ts
import { Analytics, Context } from '@segment/analytics-node';


const createAnalytics = () => new Analytics({
writeKey: '<MY_WRITE_KEY>',
}).on('error', console.error);

export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const analytics = new Analytics({
flushAt: 1,
writeKey: '<MY_WRITE_KEY>',
}).on('error', console.error);
const analytics = createAnalytics()

await new Promise((resolve, reject) =>
analytics.track({ ... }, resolve)
);
analytics.identify({ ... })
analytics.track({ ... })

// ensure analytics events get sent before program exits
await analytics.flush()

...
return new Response(...)
},
};
Expand All @@ -167,10 +173,7 @@ const settings: OAuthSettings = {
const analytics = new Analytics({
writeKey: '<MY_WRITE_KEY>',
oauthSettings: settings,
})

analytics.on('error', (err) => { console.error(err) })
}).on('error', console.error)

analytics.track({ userId: 'foo', event: 'bar' })

```
Loading