# @jeanp413/analytics-node-umd

> https://www.npmjs.com/package/@segment/analytics-node

Latest version **1.1.0** (published 2023-08-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jeanp413/analytics-node-umd
pnpm add @jeanp413/analytics-node-umd
yarn add @jeanp413/analytics-node-umd
bun add @jeanp413/analytics-node-umd
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-08-27 |
| First published | 2023-08-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 5 |
| Unpacked size | 480.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 481 |
| Maintainers | jeanp413 |

## Links

- npm: https://www.npmjs.com/package/@jeanp413/analytics-node-umd
- Repository: https://github.com/segmentio/analytics-next
- Homepage: https://github.com/segmentio/analytics-next#readme
- Issues: https://github.com/segmentio/analytics-next/issues
- npm.io page: https://npm.io/package/@jeanp413/analytics-node-umd

## Dependencies (5)

- [tslib](https://npm.io/package/tslib.md) ^2.4.1
- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.7
- [@lukeed/uuid](https://npm.io/package/@lukeed/uuid.md) ^2.0.0
- [@segment/analytics-core](https://npm.io/package/@segment/analytics-core.md) 1.3.0

## Recent versions

- 1.1.0 (latest) — 2023-08-27
- 0.0.1 — 2023-08-26

## README

# @segment/analytics-node

https://www.npmjs.com/package/@segment/analytics-node

### OFFICIAL DOCUMENTATION (FULL)
- https://segment.com/docs/connections/sources/catalog/libraries/server/node

### LEGACY NODE SDK MIGRATION GUIDE:
- https://segment.com/docs/connections/sources/catalog/libraries/server/node/migration


## Runtime Support
- Node.js >= 14
- AWS Lambda
- Cloudflare Workers
- Vercel Edge Functions
- Web Workers (experimental)

## Quick Start
### Install library
```bash
# npm
npm install @segment/analytics-node
# yarn
yarn add @segment/analytics-node
# pnpm
pnpm install @segment/analytics-node
```

### Usage
Assuming some express-like web framework.
```ts
import { Analytics } from '@segment/analytics-node'
// or, if you use require:
const { Analytics } = require('@segment/analytics-node')

// instantiation
const analytics = new Analytics({ writeKey: '<MY_WRITE_KEY>' })

app.post('/login', (req, res) => {
   analytics.identify({
      userId: req.body.userId,
      previousId: req.body.previousId
  })
  res.sendStatus(200)
})

app.post('/cart', (req, res) => {
  analytics.track({
    userId: req.body.userId,
    event: 'Add to cart',
    properties: { productId: '123456' }
  })
   res.sendStatus(201)
});
```


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

You can also see the complete list of settings in the [AnalyticsSettings interface](src/app/settings.ts).


## Plugin Architecture
- See segment's [documentation for plugin architecture](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugin-architecture).



## Usage in AWS Lambda
- [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.

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({
      maxEventsInBatch: 1,
      writeKey: '<MY_WRITE_KEY>',
    })
    .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)
   )

  ...
  return {
    statusCode: 200,
  };
  ....
};
```

## Usage in Vercel Edge Functions
```ts
import { Analytics } from '@segment/analytics-node';
import { NextRequest, NextResponse } from 'next/server';

export const analytics = new Analytics({
  writeKey: 'DjTUVRhleGaZX31JQpj6XIAaprCIb25W',
  maxEventsInBatch: 1,
})
  .on('error', console.error)

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

export default async (req: NextRequest) => {
  await new Promise((resolve) =>
    analytics.track({ ... }, resolve)
  );
  return NextResponse.json({ ... })
};
```

## Usage in Cloudflare Workers
```ts
import { Analytics, Context } from '@segment/analytics-node';

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

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

```

---
_Source: https://npm.io/package/@jeanp413/analytics-node-umd · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
