> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gleap.io/llms.txt
> Use this file to discover all available pages before exploring further.

# NodeJS

This package allows you to track server-side customer events.

## Installation

```bash theme={null}
npm install gleap-admin --save
```

## Usage

Import the GleapAdmin package.

```js theme={null}
import GleapAdmin from "gleap-admin";
```

### Initialize the SDK

It is required to initialize the GleapAdmin SDK before sending events or other requests.

```js theme={null}
GleapAdmin.initialize("secret-api-token");
```

The secret api token can be found within your project settings -> API.

### Track an event

```js theme={null}
GleapAdmin.trackEvent("user-id", "event-name", {
  someEventData: "yeah!",
});
```

The userId should match the userId you are using to identify your users.

The event data (last param) is optional.

### Identify an user

```js theme={null}
GleapAdmin.identify("user-id", {
  name: "XOXO",
  email: "asdf@asf.de",
  value: 1,
  phone: "+4395959595",
  customData: {
    plan: "Growth plan",
  },
  // Optional: associate the user with a company.
  company: {
    id: "acme-inc",
    name: "ACME inc.",
  },
});
```

The userId should match the userId you are using to identify your users.

All key-value pairs in the user properties part are optional.

The optional `company` object associates the user with a company. Only
`company.id` is required — it is your own identifier for the company. The
optional `company.name` is a fallback label and never overwrites a name you set
authoritatively via `updateCompany` (see below).

### Companies

Create, update, read and delete companies from your backend. Use these to set
authoritative company attributes (plan, value, SLA, address and custom data)
that are shown in the dashboard and used for company-level SLAs. Attributes set
here are never overwritten by the fallback data sent from your client apps.

#### Create or update a company

```js theme={null}
const company = await GleapAdmin.updateCompany("acme-inc", {
  name: "ACME inc.",
  plan: "Growth plan",
  value: 4990,
  sla: 3600, // Response-time SLA in seconds.
  domain: "acme.com",
  address: {
    line1: "1 Infinite Loop",
    city: "Cupertino",
    state: "CA",
    postalCode: "95014",
    country: "US",
  },
  customData: {
    tier: "gold",
  },
});
```

The first argument is your own company id. It is immutable and cannot be changed
afterwards. `updateCompany` creates the company on first use and updates it on
subsequent calls. It returns the saved company, or `null` if the request failed.

#### Get a company

```js theme={null}
const company = await GleapAdmin.getCompany("acme-inc");
```

Returns the company, or `null` if it does not exist.

#### Delete a company

```js theme={null}
const success = await GleapAdmin.deleteCompany("acme-inc");
```

Permanently deletes the company. Its members (contacts) and their conversations
are not deleted. Returns `true` on success.

## Rate limit

Please note that the identify and track APIs enforce a rate limit of 1500 requests / 60 seconds per API token. If you exceed the limit, requests are rejected with `429 Too Many Requests` and the token is blocked for a short period before requests are accepted again.
