# Incognia Web SDK

The Incognia SDK for the web.

Documentation can be found at <https://developer.incognia.com/docs>

## Installation

### npm

```sh
npm install @incognia/web-sdk
```

### pnpm

```sh
pnpm install @incognia/web-sdk
```

### yarn

```sh
yarn add @incognia/web-sdk
```

### CDN

Add the following script to your HTML:

```sh
<script src="https://repo.incognia.com/web/latest/incognia-web-sdk.js"></script>
```

###

## Getting started

Import the package:

```js
// ES6
import IncogniaWebSdk from '@incognia/web-sdk'

// CommonJS (Outdated)
const IncogniaWebSdk = require('@incognia/web-sdk')

// CDN Script
const IncogniaWebSdk = window.IncogniaWebSdk
```

## Library API methods

### Init

Initialize the Web SDK with your Web Application ID. This step is _required_, and further methods will not work if the SDK is not initialized.

```js
IncogniaWebSdk.init('<your-web-app-id>')
```

### Identify

This method receives user identifiers (i.e., the `accountId`) and stores locally. The user identifier is then added to the `sessionToken` data.

```js
IncogniaWebSdk.identify({ accountId: '@accountId' })
```

### Get session token

This method generates a session token and returns it.

```js
const sessionToken = await IncogniaWebSdk.getSessionToken()
```

### Send custom event

This method sends a custom event with the client custom data.

```js
IncogniaWebSdk.sendCustomEvent({
  tag: 'test-event',
  externalId: 'external-id',
  accountId: 'account-id',
  address: {
    street: 'Main Street',
    number: '100',
    city: 'New York',
    state: 'NY',
    countryName: 'US',
    postalCode: '10001',
    addressLine: 'Main Street 100, New York, NY, US, 10001',
    latitude: 40.7486,
    longitude: -73.9864
  },
  properties: {
    string: 'string',
    number: 123,
    boolean: false
  }
})
```

## Geolocation API

By default, the Incognia Web SDK does not ask the user for geolocation, because:

- Not all use cases requires geolocation
- The website should have total control when to ask for geolocation
- The lib should run silently.

However, if the geolocation is available (It was authorized by the user before), the Web SDK will include the geolocation information into the token.

### Allowing Incognia to request geolocation permissions

When getting the `sessionToken`, the option parameter `askForGeolocation` can be used to allow Incognia to automatically request the user location permissions.

```js
const sessionToken = await IncogniaWebSdk.getSessionToken({
  askForGeolocation: true
})
```

### How and when to ask the user for geolocation?

The ideal flow to manually ask the user for geolocation is:

1. Initialize the SDK:

```js
IncogniaWebSdk.init(...)
```

2. Ask the user for Geolocation and then proceed to get the token:

```js
navigator.geolocation.getCurrentPosition(getSessionToken, getSessionToken)

function getSessionToken() {
  const sessionToken = await IncogniaWebSdk.getSessionToken()
  //TODO: Send the sessionToken to your backend.
}
```

## Browser Compatibility

Except for IE, this lib is compatible with every modern browser.

However, for Geolocation API, each browser handles it differently. For example, Firefox v104 does not provide the Geolocation API by default. So the user has to enable it.
Besides, for some OSs like MacOS, you have to allow the browser to access the geolocation. If the geolocation is not available, the token will be generated without that information.
