# Retein Node.js SDK

Retein is an event-based email marketing automation SDK for Node.js applications. This SDK allows you to easily integrate Retein's features into your Node.js projects.

## Installation

To install the Retein SDK, you can use npm or yarn:

```bash
# Using npm
npm install @sinaptik/retein-sdk

# Using yarn
yarn add @sinaptik/retein-sdk
```

## Usage

Here's a basic example of how to use the Retein SDK in your Node.js application:

```javascript
// Import the ReteinSDK
import { ReteinSDK } from "@sinaptik/retein-sdk";

// Function to run the example
async function runExample() {
  try {
    // Initialize the SDK
    const sdk = await ReteinSDK.create("your-public-key", { verbose: true });

    // Identify a user
    await sdk.identify("unique-distinct-id");

    // Identify a user with IP address and user_agent from user request on server.
    await sdk.identify("unique-distinct-id", {
      ip_address: "127.0.0.1",
      user_agent:
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
    });

    // Update user properties
    await sdk.updateUser({
      email: "user@example.com",
      name: "John Doe",
      age: 30,
    });

    // Track an event
    await sdk.track("custom_event", { plan_id: "Plan ID" });

    console.log("Example run successfully");
  } catch (error) {
    console.error("Error running example:", error);
  }
}

// Execute the example
runExample();
```

Here's an example of using existing session of user based on distinct id:

```javascript
// Import the ReteinSDK
import { ReteinSDK } from "@sinaptik/retein-sdk";

// Function to run the example
async function runExample() {
  try {
    // Initialize the SDK
    const sdk = await ReteinSDK.create("your-public-key", { verbose: true });

    // Use existing session of user based on distinct id
    // List last sessions returns last five sessions
    const sessions = await sdk.listLastSessions("unique-distinct-id");
    await sdk.identify("unique-distinct-id", {
      session_id: sessions[0].id, // Use the last session id
    });

    // Update user properties
    await sdk.updateUser({
      email: "user@example.com",
      name: "John Doe",
      age: 30,
    });

    // Track an event
    await sdk.track("custom_event", { plan_id: "Plan ID" });

    console.log("Example run successfully");
  } catch (error) {
    console.error("Error running example:", error);
  }
}

// Execute the example
runExample();
```

## API Reference

### `ReteinSDK.create(public_key, options)`

- **public_key**: Your Retein public API key.
- **options**: Optional configuration settings.

### `identify(distinctId)`

Identifies a user with a unique distinct ID.

### `updateUser(properties)`

Updates user properties such as email, name, and other custom attributes.

### `track(eventName, properties)`

Tracks a custom event with specified properties.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
