Socket
Socket
Sign inDemoInstall

@segment/analytics-node

Package Overview
Dependencies
Maintainers
276
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@segment/analytics-node - npm Package Compare versions

Comparing version 1.0.0-beta.26 to 1.0.0

CHANGELOG.md

2

dist/cjs/generated/version.js

@@ -5,3 +5,3 @@ "use strict";

// This file is generated.
exports.version = '1.0.0-beta.26';
exports.version = '1.0.0';
//# sourceMappingURL=version.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b64encode = void 0;
// eslint-disable-next-line import/no-nodejs-modules
const buffer_1 = require("buffer");

@@ -5,0 +6,0 @@ /**

@@ -12,2 +12,3 @@ "use strict";

if (runtime === 'node') {
// eslint-disable-next-line no-restricted-globals
ctx.updateEvent('_metadata.nodeVersion', process.version);

@@ -14,0 +15,0 @@ }

// This file is generated.
export const version = '1.0.0-beta.26';
export const version = '1.0.0';
//# sourceMappingURL=version.js.map

@@ -0,1 +1,2 @@

// eslint-disable-next-line import/no-nodejs-modules
import { Buffer } from 'buffer';

@@ -2,0 +3,0 @@ /**

@@ -9,2 +9,3 @@ import { Publisher } from './publisher';

if (runtime === 'node') {
// eslint-disable-next-line no-restricted-globals
ctx.updateEvent('_metadata.nodeVersion', process.version);

@@ -11,0 +12,0 @@ }

@@ -1,2 +0,2 @@

export declare const version = "1.0.0-beta.26";
export declare const version = "1.0.0";
//# sourceMappingURL=version.d.ts.map
{
"name": "@segment/analytics-node",
"version": "1.0.0-beta.26",
"version": "1.0.0",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"license": "MIT",
"repository": {
"directory": "packages/node",
"type": "git",
"url": "https://github.com/segmentio/analytics-next"
},
"files": [

@@ -28,8 +34,7 @@ "dist/",

"concurrently": "yarn run -T concurrently",
"jest": "yarn run -T jest",
"publish-prerelease": "sh scripts/prerelease.sh"
"jest": "yarn run -T jest"
},
"dependencies": {
"@lukeed/uuid": "^2.0.0",
"@segment/analytics-core": "1.2.4",
"@segment/analytics-core": "1.3.0",
"buffer": "^6.0.3",

@@ -43,9 +48,3 @@ "node-fetch": "^2.6.7",

},
"packageManager": "yarn@3.4.1",
"license": "MIT",
"repository": {
"directory": "packages/node",
"type": "git",
"url": "https://github.com/segmentio/analytics-next"
}
"packageManager": "yarn@3.4.1"
}
# @segment/analytics-node
> ### Warning: This library is in [public beta](https://segment.com/legal/first-access-beta-preview) ⚠️
https://www.npmjs.com/package/@segment/analytics-node
### OFFICIAL DOCUMENTATION (FULL)
- https://segment.com/docs/connections/sources/catalog/libraries/server/node/quickstart
### LEGACY NODE SDK MIGRATION GUIDE:
- https://segment.com/docs/connections/sources/catalog/libraries/server/node/migration
## Runtime Support

@@ -10,3 +16,3 @@ - Node.js >= 14

- Cloudflare Workers
- Vercel Edge Functions (and other Edge Worker environments)
- Vercel Edge Functions
- Web Workers (experimental)

@@ -54,276 +60,13 @@

## Graceful Exit
Avoid losing events on exit!
* Call `.closeAndFlush()` to stop collecting new events and flush all existing events.
* If a callback on an event call is included, this also waits for all callbacks to be called, and any of their subsequent promises to be resolved.
```ts
await analytics.closeAndFlush()
// or
await analytics.closeAndFlush({ timeout: 5000 }) // force resolve after 5000ms
```
#### Advanced Example
```ts
const app = express()
const server = app.listen(3000)
## Settings & Configuration
See the documentation: https://segment.com/docs/connections/sources/catalog/libraries/server/node/#configuration
const onExit = async () => {
await analytics.closeAndFlush()
server.close(() => {
console.log("Gracefully closing server...")
process.exit()
})
}
['SIGINT', 'SIGTERM'].forEach((code) => process.on(code, onExit))
```
You can also see the complete list of settings in the [AnalyticsSettings interface](src/app/settings.ts).
#### Collecting unflushed events
If you absolutely need to preserve all possible events in the event of a forced timeout, even ones that came in after `analytics.closeAndFlush()` was called, you can collect those events.
```ts
const unflushedEvents = []
analytics.on('call_after_close', (event) => unflushedEvents.push(events))
await analytics.closeAndFlush()
console.log(unflushedEvents) // all events that came in after closeAndFlush was called
```
## Configuration Settings
See complete list of settings in the [AnalyticsSettings interface](src/app/settings.ts).
```ts
const analytics = new Analytics({
writeKey: '<MY_WRITE_KEY>',
host: 'https://api.segment.io',
path: '/v1/batch',
maxRetries: 3,
maxEventsInBatch: 15,
flushInterval: 10000,
// ... and more!
})
```
## Regional configuration
For Business plans with access to Regional Segment, you can use the host configuration parameter to send data to the desired region:
Oregon (Default) — api.segment.io/v1
Dublin — events.eu1.segmentapis.com
An example of setting the host to the EU endpoint using the Node library would be:
```ts
const analytics = new Analytics({
...
host: "https://events.eu1.segmentapis.com"
});
```
## Batching
Our libraries are built to support high performance environments. That means it is safe to use our Node library on a web server that’s serving thousands of requests per second.
Every method you call does not result in an HTTP request, but is queued in memory instead. Messages are then flushed in batch in the background, which allows for much faster operation.
By default, our library will flush:
- Every 15 messages (controlled by `settings.maxEventsInBatch`).
- If 10 seconds has passed since the last flush (controlled by `settings.flushInterval`)
There is a maximum of 500KB per batch request and 32KB per call.
If you don’t want to batch messages, you can turn batching off by setting the `maxEventsInBatch` setting to 1, like so:
```ts
const analytics = new Analytics({
...
maxEventsInBatch: 1
});
```
Batching means that your message might not get sent right away. But every method call takes an optional callback, which you can use to know when a particular message is flushed from the queue, like so:
```ts
analytics.track({
userId: '019mr8mf4r',
event: 'Ultimate Played',
},
(err, ctx) => {
...
}
)
```
## Error Handling
Subscribe and log all event delivery errors.
```ts
const analytics = new Analytics({ writeKey: '<MY_WRITE_KEY>' })
analytics.on('error', (err) => console.error(err))
```
## Event Emitter Interface
You can see the complete list of emitted events in [emitter.ts](src/app/emitter.ts)
```ts
analytics.on('error', (err) => console.error(err))
analytics.on('identify', (ctx) => console.log(ctx))
analytics.on('track', (ctx) => console.log(ctx))
```
#### You can use the emitter to log all HTTP Requests
```ts
analytics.on('http_request', (event) => console.log(event))
// when triggered, emits an event of the shape:
{
url: 'https://api.segment.io/v1/batch',
method: 'POST',
headers: {
'Content-Type': 'application/json',
...
},
body: '...',
}
```
## Multiple Clients
Different parts of your application may require different types of batching, or even sending to multiple Segment sources. In that case, you can initialize multiple instances of Analytics with different settings:
```ts
const marketingAnalytics = new Analytics({ writeKey: 'MARKETING_WRITE_KEY' });
const appAnalytics = new Analytics({ writeKey: 'APP_WRITE_KEY' });
```
## Troubleshooting
1. Double check that you’ve followed all the steps in the Quick Start.
2. Make sure that you’re calling a Segment API method once the library is successfully installed: identify, track, etc.
3. Log events.
```js
['initialize', 'call_after_close',
'screen', 'identify', 'group',
'track', 'ready', 'alias',
'page', 'error', 'register',
'deregister'].forEach((event) => analytics.on(event, console.log)
```
## Development: Disabling Analytics for Tests
- Set `disable: true`.
```ts
const analytics = new Analytics({
...
disable: process.env.NODE_ENV === 'test'
})
```
## Differences from legacy analytics-node / Migration Guide
- Named imports.
```ts
// old
import Analytics from 'analytics-node'
// new
import { Analytics } from '@segment/analytics-node'
```
- Instantiation now requires an _object_ as the first argument.
```ts
// old
var analytics = new Analytics('YOUR_WRITE_KEY'); // not supported
// new!
const analytics = new Analytics({ writeKey: '<MY_WRITE_KEY>' })
```
- Graceful shutdown (See Graceful Shutdown section)
```ts
// old
await analytics.flush(function(err, batch) {
console.log('Flushed, and now this program can exit!');
});
// new
await analytics.closeAndFlush()
```
Other Differences:
- The configuration option for disabling analytics has changed. `enable: false` -> `disable: true`
- the `errorHandler` configuration option has been remove -- see "Error Handling" section
- `flushAt` configuration option -> `maxEventsInBatch`.
- `callback` call signature is different
```ts
// old
(err, batch) => void
// new
(err, ctx) => void
```
- Undocumented behavior around `track` properties have been removed.
```ts
// old, undocumented behavior
analytics.track({
...
event: 'Ultimate Played',
myProp: 'abc'
})
// new
analytics.track({
...
event: 'Ultimate Played',
properties: {
myProp: 'abc'
}
})
```
## Plugin Architecture
- See segment's [documentation for plugin architecture](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugin-architecture).
```ts
import type { Plugin } from '@segment/analytics-node'
export const lowercase: Plugin = {
name: 'Lowercase events',
type: 'enrichment',
version: '1.0.0',
isLoaded: () => true,
load: () => Promise.resolve(),
track: (ctx) => {
ctx.event.event = ctx.event.event.toLowerCase()
return ctx
}
}
analytics.register(lowercase)
```
## Selecting Destinations
The alias, group, identify, page and track calls can all be passed an object of integrations that lets you turn certain destinations on or off. By default all destinations are enabled.
Here’s an example with the integrations object shown:
```ts
analytics.track({
event: 'Membership Upgraded',
userId: '97234974',
integrations: {
'All': false,
'Vero': true,
'Google Analytics': false
}
})
```
In this case, we’re specifying that we want this track to only go to Vero. All: false says that no destination should be enabled unless otherwise specified. Vero: true turns on Vero, etc.
Destination flags are case sensitive and match the [destination’s name in the docs](https://segment.com/docs/connections/destinations) (i.e. “AdLearn Open Platform”, “awe.sm”, “MailChimp”, etc.). In some cases, there may be several names for a destination; if that happens you’ll see a “Adding (destination name) to the Integrations Object” section in the destination’s doc page with a list of valid names.
Note:
- Available at the business level, filtering track calls can be done right from the Segment UI on your source schema page. We recommend using the UI if possible since it’s a much simpler way of managing your filters and can be updated with no code changes on your side.
- If you are on a grandfathered plan, events sent server-side that are filtered through the Segment dashboard will still count towards your API usage.
## Usage in AWS Lambda

@@ -334,3 +77,3 @@ - [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.

```ts
const { Analytics } = require("@segment/analytics-node");
const { Analytics } = require('@segment/analytics-node');

@@ -343,3 +86,3 @@ // since analytics has the potential to be stateful if there are any plugins added,

})
.on("error", console.error);
.on('error', console.error);

@@ -350,5 +93,6 @@ module.exports.handler = async (event) => {

await new Promise((resolve) =>
analytics().track({ event: 'My Event', anonymousId: 'foo' }, resolve)
analytics().track({ ... }, resolve)
)
...
return {

@@ -360,1 +104,52 @@ 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(...)
},
};
```
// This file is generated.
export const version = '1.0.0-beta.26'
export const version = '1.0.0'

@@ -0,1 +1,2 @@

// eslint-disable-next-line import/no-nodejs-modules
import { Buffer } from 'buffer'

@@ -2,0 +3,0 @@ /**

@@ -0,1 +1,2 @@

/* eslint-disable no-restricted-globals */
export type RuntimeEnv =

@@ -2,0 +3,0 @@ | 'node'

@@ -13,2 +13,3 @@ import { Publisher, PublisherProps } from './publisher'

if (runtime === 'node') {
// eslint-disable-next-line no-restricted-globals
ctx.updateEvent('_metadata.nodeVersion', process.version)

@@ -15,0 +16,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc