Skip to content

bentonow/bento-node-sdk

Repository files navigation

Bento Node SDK

Tip

Need help? Join our Discord or email jesse@bentonow.com for personalized support.

The Bento Node.js SDK makes it quick and easy to build an excellent analytics experience in your Node.js application. We provide powerful and customizable APIs that can be used out-of-the-box to track your users' behavior and manage subscribers. We also expose low-level APIs so that you can build fully custom experiences.

Get started with our πŸ“š integration guides, or πŸ“˜ browse the SDK reference.

🐢 Battle-tested by NativShark Bento Production (a Bento customer)!

❀️ Thank you @HelloKashif from IPInfo for your contribution.

❀️ Thank you @jonsherrard from Devular for your contribution.

Tests

Table of contents

Features

  • Simple event tracking: We make it easy for you to track user events and behavior in your application.
  • Subscriber management: Easily add, update, and remove subscribers from your Bento account.
  • Custom fields: Track and update custom fields for your subscribers to store additional data.
  • Purchase tracking: Monitor customer purchases and calculate lifetime value (LTV) for your subscribers.
  • Batch operations: Perform bulk imports of subscribers and events for efficient data management.
  • TypeScript support: The SDK is written in TypeScript and provides type definitions for a better development experience.

Requirements

The Bento Node.js SDK requires Node.js version 12 or later.

Bento Account for a valid SITE_UUID, BENTO_PUBLISHABLE_KEY & BENTO_SECRET_KEY.

Getting started

Installation

Install the Bento SDK in your project folder:

# Using npm
npm install @bentonow/bento-node-sdk --save

# Using Bun
bun add @bentonow/bento-node-sdk

Using Bun (Recommended)

# Install dependencies
bun install

# Run tests
bun test

# Run tests with coverage
bun test:coverage

# Build the package
bun run build

# Lint code
bun run lint

# Format code
bun run format

Using npm

# Install dependencies
npm install

# Run tests
npm run test:npm

# Run tests with coverage
npm run test:coverage:npm

# Build the package
npm run build:npm

# Lint code
npm run lint

# Format code
npm run format

Integration

Initialize the Bento client and start tracking events:

import { Analytics } from '@bentonow/bento-node-sdk';

const bento = new Analytics({
  authentication: {
    publishableKey: 'bento-publishable-key',
    secretKey: 'bento-secret-key', 
  },
  siteUuid: 'bento-site-uuid',
});

bento.V1.track({
  email: 'user@example.com',
  type: '$formSubmitted',
  fields: {
    first_name: 'John',
    last_name: 'Doe',
  },
  details: {
    fromCustomEvent: true,
  },
}).then(result => console.log(result));

Modules

The Bento SDK provides several modules for different operations:

Analytics (Base Module)

Core functionality for tracking events and managing subscribers.

Convenience Helpers

tagSubscriber

Tags a subscriber with a specific tag.

bento.V1.tagSubscriber({
  email: 'user@example.com',
  tagName: 'New Customer',
}).then(result => console.log(result));

addSubscriber

Adds a new subscriber to your Bento account.

bento.V1.addSubscriber({
  email: 'newuser@example.com',
  fields: {
    firstName: 'John',
    lastName: 'Doe',
  },
}).then(result => console.log(result));

removeSubscriber

Removes a subscriber from your Bento account.

bento.V1.removeSubscriber({
  email: 'user@example.com',
}).then(result => console.log(result));

updateFields

Updates custom fields for a subscriber.

bento.V1.updateFields({
  email: 'user@example.com',
  fields: {
    lastPurchaseDate: new Date(),
  },
}).then(result => console.log(result));

track

Tracks a custom event for a subscriber.

bento.V1.track({
  email: 'user@example.com',
  type: '$pageView',
  details: {
    url: '/products',
  },
}).then(result => console.log(result));

trackPurchase

Tracks a purchase event for a subscriber.

bento.V1.trackPurchase({
  email: 'user@example.com',
  purchaseDetails: {
    unique: { key: 'order-123' },
    value: { amount: 9999, currency: 'USD' },
  },
}).then(result => console.log(result));

Low Level API calls

Batch

Perform bulk operations for importing subscribers and events.

importSubscribers

Imports multiple subscribers in a single operation.

bento.V1.Batch.importSubscribers({
  subscribers: [
    { email: 'user1@example.com', firstName: 'Alice' },
    { email: 'user2@example.com', firstName: 'Bob' },
  ],
}).then(result => console.log(result));

importEvents

Imports multiple events in a single operation.

bento.V1.Batch.importEvents({
  events: [
    { email: 'user@example.com', type: '$login' },
    { email: 'user@example.com', type: '$pageView', details: { url: '/home' } },
  ],
}).then(result => console.log(result));

Commands

Execute specific commands for subscriber management.

addTag

Adds a tag to a subscriber.

bento.V1.Commands.addTag({
  email: 'user@example.com',
  tagName: 'VIP',
}).then(result => console.log(result));

removeTag

Removes a tag from a subscriber.

bento.V1.Commands.removeTag({
  email: 'user@example.com',
  tagName: 'VIP',
}).then(result => console.log(result));

addField

Adds a custom field to a subscriber.

bento.V1.Commands.addField({
  email: 'user@example.com',
  field: {
    key: 'favoriteColor',
    value: 'blue',
  },
}).then(result => console.log(result));

removeField

Removes a custom field from a subscriber.

bento.V1.Commands.removeField({
  email: 'user@example.com',
  fieldName: 'favoriteColor',
}).then(result => console.log(result));

Experimental

Access experimental features (use with caution).

validateEmail

Attempts to validate an email address.

bento.V1.Experimental.validateEmail({
  email: 'user@example.com',
}).then(result => console.log(result));

guessGender

Attempts to guess the gender based on a given name.

bento.V1.Experimental.guessGender({
  name: 'Alex',
}).then(result => console.log(result));

Fields

Manage custom fields for your subscribers.

getFields

Retrieves all custom fields defined in your Bento account.

bento.V1.Fields.getFields().then(fields => console.log(fields));

createField

Creates a new custom field in your Bento account.

bento.V1.Fields.createField({
  key: 'loyaltyPoints',
}).then(result => console.log(result));

Forms

Retrieve form responses.

getResponses

Retrieves responses for a specific form.

bento.V1.Forms.getResponses('form-id-123').then(responses => console.log(responses));

Subscribers

Manage individual subscribers.

getSubscribers

Retrieves subscriber information.

bento.V1.Subscribers.getSubscribers({
  email: 'user@example.com',
}).then(subscriber => console.log(subscriber));

createSubscriber

Creates a new subscriber in your Bento account.

bento.V1.Subscribers.createSubscriber({
  email: 'newuser@example.com',
}).then(result => console.log(result));

Tags

Create and manage tags for subscriber segmentation.

getTags

Retrieves all tags defined in your Bento account.

bento.V1.Tags.getTags().then(tags => console.log(tags));

createTag

Creates a new tag in your Bento account.

bento.V1.Tags.createTag({
  name: 'Premium Customer',
}).then(result => console.log(result));

For detailed information on each module, refer to the SDK Documentation.

Types Reference

This section provides a detailed reference for the types used in the Bento Node.js SDK. AddFieldParameters <S>

Parameters for adding a field to a subscriber.

Property Type Required Description
email string βœ”οΈ Subscriber's email address
field { key: keyof S; value: any } βœ”οΈ Field to add

AddSubscriberParameters <S>

Parameters for adding a new subscriber.

Property Type Required Description
date Date ❌ Date of subscription
email string βœ”οΈ Subscriber's email address
fields Partial <S> ❌ Additional fields for the subscriber

AddTagParameters

Parameters for adding a tag to a subscriber.

Property Type Required Description
email string βœ”οΈ Subscriber's email address
tagName string βœ”οΈ Name of the tag to add

BatchImportEventsParameter <S>, <E>

Parameters for batch importing events.

Property Type Required Description
events BentoEvent <S>, <E> βœ”οΈ Array of events to import

BatchImportSubscribersParameter <S>

Parameters for batch importing subscribers.

Property Type Required Description
subscribers ({ email: string } & Partial <S>)[] βœ”οΈ Array of subscribers to import

BentoEvent <S>, <E>

Represents different types of events in Bento. It's a union of the following event types:

  • BaseEvent <E>
  • PurchaseEvent
  • SubscribeEvent <S>
  • TagEvent
  • UnsubscribeEvent
  • UpdateFieldsEvent <S>

PurchaseDetails

Details of a purchase event.

Property Type Required Description
unique { key: string | number } βœ”οΈ Unique identifier for the purchase
value { currency: string; amount: number } βœ”οΈ Value of the purchase
cart PurchaseCart ❌ Additional cart details

ChangeEmailParameters

Parameters for changing a subscriber's email.

Property Type Required Description
oldEmail string βœ”οΈ Current email address
newEmail string βœ”οΈ New email address

CreateFieldParameters

Parameters for creating a new field.

Property Type Required Description
key string βœ”οΈ Key of the new field

CreateTagParameters

Parameters for creating a new tag.

Property Type Required Description
name string βœ”οΈ Name of the new tag

Subscriber <S>

Represents a subscriber in Bento.

Property Type Required Description
attributes SubscriberAttributes <S> βœ”οΈ Attributes of the subscriber
id string βœ”οΈ Unique identifier
type EntityType.VISITOR βœ”οΈ Type of the entity

TrackParameters <S>, <E>

Parameters for tracking an event.

Property Type Required Description
email string βœ”οΈ Subscriber's email address
type string βœ”οΈ Type of the event
details { [key: string]: any } ❌ Additional details of the event
fields Partial <S> ❌ Fields to update for the subscriber

ValidateEmailParameters

Parameters for validating an email address.

Property Type Required Description
email string βœ”οΈ Email address to validate
ip string ❌ IP address of the user
name string ❌ Name associated with the email
userAgent string ❌ User agent string

Note: The S and E generic types are used for TypeScript support. S represents the type of your subscriber's custom fields, and E represents the prefix used for custom events. For more details, refer to the TypeScript section of the documentation.

Things to know

  • All events must be identified with an email address.
  • Most events are indexed within seconds in your Bento account.
  • The SDK supports TypeScript with generics for custom fields and events.
  • Batch operations are available for importing subscribers and events efficiently.
  • The SDK doesn't currently support anonymous events (coming soon).

Contributing

We welcome contributions! Please see our contributing guidelines for details on how to submit pull requests, report issues, and suggest improvements.

License

The Bento SDK for Node.js is available as open source under the terms of the MIT License.