open-index/open-npm huggingface dataset:
npm package: @struct-app/client
Description: ## Installation
Latest version: 0.1.10

# Struct JavaScript Client

## Installation

```bash
 npm install --save @struct-app/client
```

## Usage (Browser)

###  Create a Client

```javascript
import { Struct } from "@struct-app/client";

const struct = new Struct({
  apiKey: "YOUR_API_KEY",
});
```

### Identify Users and Organisations

You can identify users using the `identify` method. This method will create or retrieve the user's active Struct session so that you can associate events with the user, their organisation, and their session. Once called, subsequent calls to `identify` will update the user's details.

```javascript
struct.identify({
  userId: "YOUR_USER_ID",
  name: "John Doe",
  email: "john@example.com",

  organisationId: "YOUR_ORGANISATION_ID",
  organisationName: "Acme Inc.",

  traits: { foo: "bar", type: "premium", age: 30 }, // Optional user traits
});
```

To end a user's session, call identify with `null` as the only argument.

```javascript
struct.identify(null); // End the user's session
```

### Track Events

```javascript
struct.track("my_event", { foo: "bar" });
struct.track("my_conversion_event", { value: 100 }); // Track a conversion event by providing a value
```

### Track Page Views

```javascript
struct.page();
```

## Usage (Node.js)

```javascript
import { Struct } from "@struct-app/client";

const struct = new Struct({
  apiKey: "YOUR_API_KEY",

  // Optionally, you can pass a user id to associate events created with this client with a user.
  userId: "YOUR_USER_ID",
});
```

### Track Events on your Backend

```javascript
struct.track("processing_finished", { time: "100ms" });
struct.track("checkout", { value: 100 }); // Track a conversion event
```

### Track Page Views on your Backend

```javascript
struct.page({
  url: "https://example.com",
  title: "Example Page",
});
```

###  Identify Users and Organisations on your Backend

```javascript
struct.identify({
 userId: 'YOUR_USER_ID',
 name: 'John Doe',
 email: 'john@example.com'

 organisationId: 'YOUR_ORGANISATION_ID',
 organisationName: 'Acme Inc.',
})
```

## TypeScript Integration

You can make use of our TypesScript types to get type safety for your events and traits.

```typescript
import { Struct } from "@struct-app/client";
import type { event, traits } from "@struct-app/client";

type Events = {
  "processing finished": event<{ time: string }>;
  "add to cart": event<{ productId: string; productName: string }>;
  checkout: event<{ value: number; currency: string }>;
};

type UserTraits = traits<{
  type: "premium" | "basic";
  age: number;
}>;

const struct = new Struct<UserTraits, Events>();

// Get type safety for your events!
struct.track("processing finished", { time: "100ms" });
struct.track("add to cart", {
  productId: "123",
  productName: "Example Product",
});

// And for your traits!
struct.identify({
  userId: "123",
  name: "John Doe",
  email: "john@struct.app",
  traits: {
    type: "premium",
    age: 22,
  },
});
```