# Mergn Web SDK

[![npm version](https://img.shields.io/npm/v/mergn-webapp-sdk)](https://www.npmjs.com/package/mergn-webapp-sdk)
[![npm downloads](https://img.shields.io/npm/dm/mergn-webapp-sdk.svg)](https://www.npmjs.com/package/mergn-webapp-sdk)

## Installation

Mergn Web SDK is available as an npm package.

#### Using a package manager

`npm install --save mergn-webapp-sdk@latest`

```javascript
import mergn from 'mergn-webapp-sdk';
```

#### Using a CDN

```html
<script src="https://cdn.jsdelivr.net/npm/mergn-webapp-sdk@latest/dist/index.global.js"></script>
```

## Initialization

#### Add your Mergn account credentials

```javascript
mergn.init('Api_Key', options?: {
  swPath?: string || '/'; // custom service worker path. Defaults to root '/'
}); // Replace with values applicable to you. Refer below
```

`Api_Key` (mandatory): This value is given by Mergn Team.

`options` (optional): Additional initialization parameters.

`mergn-sw.js` (mandatory): A service worker file given by Mergn Team to enable features like web push notifications. This file must be kept in the root of the project like `public` folder in case of React. Or a custom path given in `swPath` option.

### Event Record

Events monitor specific user actions within your app or website, such as app launch, product view, song play, photo share, purchase, or item favoring.

```javascript

mergn.recordEvent(eventName: string, eventProperties?: { eventProperty: string; value: string | number }[];
): Promise<void>

Example:

// event without properties
mergn.recordEvent('Product viewed')

// event with properties
mergn.recordEvent('Product viewed', [{ eventProperty: 'Mens Accessories', value: 59.99 }])

```

Note: In most cases, it's recommended to use recordEvent without awaiting the returned Promise. This allows your application to continue running without waiting for the event recording to complete.

### Attribute Record

Records or updates the attributes of user such as name, age or address.

```javascript
mergn.recordAttribute(attributeName: string, value: string | number): Promise<void>

mergn.recordAttribute('Age', 35);
```

Note: In most cases, it's recommended to use recordAttribute without awaiting the returned Promise. This allows your application to continue running without waiting for the attribute recording to complete.

### User Set Identity

Use this method for setting identity.

```javascript
await mergn.login('Unique_Identity'): Promise<void>;
```

`Unique_Identity` (mandatory): This value is the customer's unique identity in your database like `Id` or `Email`.

Note: it's recommended to use login with awaiting the returned Promise. This allows your application to set identity successfully which would later be used for recording events and attributes.

### User Logout

Use this method where user is logged out successfully to record logout event.

```javascript
mergn.logout();
```

### Offline Mode

`mergn-webapp-sdk` automatically queue events in case of internet disruption, and sends them once internet is restored while the user is on the website

max event queue limit is 50, after which events are ignored

### Debugging

! MUST DISABLE ON PRODUCTION

To show logs while debugging

```javascript
mergn.setDebugLevel(LOG_LEVEL);
```

`LOG_LEVEL` (mandatory): Enter corresponding value:

`NONE = 0,
  INFO = 1,
  ERROR = 2,
  ALL = 3,`

Default is 0
