# statewize-client-sdk

> Report to STATEWIZE using this SDK

Latest version **2.7.4** (published 2022-04-20) · GPL-3.0-only license · 0 weekly downloads

## Install

```sh
npm install statewize-client-sdk
pnpm add statewize-client-sdk
yarn add statewize-client-sdk
bun add statewize-client-sdk
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.7.4 |
| Published | 2022-04-20 |
| First published | 2020-09-19 |
| Weekly downloads | 0 |
| License | GPL-3.0-only |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 48.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | justlev |

## Links

- npm: https://www.npmjs.com/package/statewize-client-sdk
- npm.io page: https://npm.io/package/statewize-client-sdk

## Dependencies (8)

- [url](https://npm.io/package/url.md) ^0.11.0
- [path](https://npm.io/package/path.md) ^0.12.7
- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [dotenv](https://npm.io/package/dotenv.md) ^9.0.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [form-data](https://npm.io/package/form-data.md) ^4.0.0
- [type-fest](https://npm.io/package/type-fest.md) ^1.1.3
- [isomorphic-fetch](https://npm.io/package/isomorphic-fetch.md) ^3.0.0

## Recent versions

- 2.7.4 (latest) — 2022-04-20
- 2.7.3 — 2022-04-19
- 2.7.2 — 2022-04-19
- 2.7.1 — 2022-04-10
- 2.7.0 — 2022-04-10
- 2.6.0 — 2022-02-24
- 2.5.7 — 2022-02-02
- 2.5.6 — 2022-01-31
- 2.5.5 — 2022-01-27
- 2.5.4 — 2022-01-27
- 2.5.2 — 2022-01-27
- 2.5.1 — 2022-01-27
- 2.5.0 — 2022-01-03
- 2.0.48 — 2021-12-25
- 2.0.46 — 2021-11-21
- … 46 more at https://npm.io/package/statewize-client-sdk/versions

## README

# STATEWIZE Client SDK 🔍 ✅
Used to report events to [STATEWIZE](https://statewize.com).

Official docs: https://docs.statewize.com

## USAGE
1. Add package to your project (NodeJS / React / Anything JS) 
```
yarn add statewize-client-sdk
```
OR
```
npm install statewize-client-sdk
```

2. Import and initialize (using the API key from your account)
```javascript
import {StatewizeClient} from 'statewize-client-sdk';

const sdk = new StatewizeClient(procee.env.YOUR_API_KEY);
```
You can fetch your API key [here](https://app.statewize.com/settings/user).

3. Download your journey integration map from the journey planner canvas

![](https://app.statewize.com/images/onboarding/integrate.gif)

4. Report events as and when required
```javascript
const {eventTrackingId} = await sdk.reportEvent({
  yourId: "Your Tracking ID",
  type: "started",
  slug: journeyMap.YOUR_STATE_NAME, // from step 3
  yourUserId: 'your-user-1',
});
```

... some time passes ...

```javascript
await sdk.reportEvent({
  eventTrackingId,
  type: "finisned",
  slug: journeyMap.YOUR_STATE_NAME, // from step 3
  yourUserId: 'your-user-1'
});
```

### Attachments
You can include attachments with state events. Simply provide the attachments in the attachments array.

Attachment types:
1. Text
2. JSON objects
3. Files
4. URLs

**example:**
```javascript
  await sdk.reportEvent({
    yourId: `Your ID for this whole journey`,
    stateId: journeyMap.START_HERE, // from step 3 
    type: 'finished',

    // optional
  eventTrackingId: `Event Tracking ID received from reportEvent`,

    // optional:
    attachments: [
        {
            type: 'text',
            content: {content: `LOG: info: ${new Date().toISOString()} Selfie verification completed`}
        },
        {
            type: 'url',
            content: {url: 'https://images.unsplash.com/photo-1566241440091-ec10de8db2e1?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2076&q=80'}
        },
        {
            type: 'file',
            content: {buffer: Buffer.from("test-buffer"), fileName: 'test-text.txt'}
        },
    ]
});
```



### Enriching user data

You can provide additional user data for users that you track. We don't know who your users are, and we don't use their
information for any purposes. But for your own logging purposes, if you're interested to attach some rich data about the
users you track - you can do so using `setUserData` method.

For example:

```js
await sdk.setUserData({yourUserId: 'user-1234', data: {something: 'important'}});
```


### Switching the execution id
Let's say you've started tracking a journey using one id, but then you have another ID you'd like to use for this specific execution. 
For example, you may have started tracking an e-commerce order using an auto-generated ID on the marketing site, but now you'd like to 
use your own `orderId` - something you didn't have before - for the same execution.

You can do that by calling:
```js
await sdk.switchId({oldId: 'your-old-id', newId: 'your-new-id'});
```

Please be aware that execution IDs are unique, so the switch may fail if the ID is already used by you in another execution.

For any questions and support, feel free to reach out to [support@statewize.com](mailto:support@statewize.com)


## Feature Flags
You can use the STATEWIZE SDK in order to fetch available feature-flags.
The feature flags will be returned for the environmend that you've initially passed
to the SDK's constructor. If no environment is passed, the **production** environment
will be used by default.

```js
const featureFlags = await sdk.getFeatureFlags();
// Will return something like:
// {
//    'your-feature-flag-key': 'someValue',
//    'another-one': false,
//    'now-a-json-one': {
//        "zim": "zoom"
//    },
// }
```

We've also prepared a wrapper for you, which would execute its callback
only if a feature flag has a truthy value.

For example:

```js
await sdk.withFeatureFlag('your-feature-flag-key', async (featureFlagValue) => {
  // This code will be executed ONLY if the value of `your-feature-flag-key` is truthy.
  // The parameter received by the callback - `featureFlagValue` - will contain the
  // value of the feature flag.
  
  console.log("Hello!");
});
```

---
_Source: https://npm.io/package/statewize-client-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
