# React Native Survicate Plugin

[Survicate](https://survicate.com/) is an all-in-one customer feedback platform that helps you collect and act on feedback from your customers. It helps you understand your customers and improve their experience with your product or service.

## Installation

Install the `@survicate/analytics-react-native-survicate` and `@survicate/react-native-survicate` packages.

Using npm:

```bash
npm install --save @survicate/analytics-react-native-survicate @survicate/react-native-survicate
```

Using yarn:

```bash
yarn add @survicate/analytics-react-native-survicate @survicate/react-native-survicate
```

### Configure iOS

1. Add your Survicate workspace key to `Info.plist`.

```text
	<key>Survicate</key>
	<dict>
		<key>WorkspaceKey</key>
		<string>*WORKSPACE_KEY*</string>
	</dict>
```

2. Run `pod install` in the `ios` directory.

### Configure Survicate bindings for Android

1. Add the Maven repository to your project `build.gradle` located under `android` directory.

```text
allprojects {
    repositories {
        // ...
        maven { url 'https://repo.survicate.com' }
    }
}
```

2. Add your Survicate workspace key to `AndroidManifest.xml`.

```java
<application
    android:name=".MyApp"
>
    <!-- ... -->
    <meta-data android:name="com.survicate.surveys.workspaceKey" android:value="*WORKSPACE_KEY*"/>
</application>
```

## Usage

Follow the [instructions for adding plugins](https://github.com/segmentio/analytics-react-native#adding-plugins) on the main Analytics client:

In the file where you initialize the Analytics client, call `.add(plugin)` with a `SurvicatePlugin` instance:

```ts
import { createClient } from '@segment/analytics-react-native';

import { SurvicatePlugin } from '@segment/analytics-react-native-plugin-survicate';

const segmentClient = createClient({
  writeKey: 'SEGMENT_KEY'
});

segmentClient.add({ plugin: new SurvicatePlugin() });
```

### Use the Survicate plugin

#### Identify

The `SurvicateDestination` plugin maps Segment [Identify](/docs/segment/connections/spec/identify) events to Survicate's `setUserTrait` method. Inside the plugin's Identify function, the code extracts the `traits` object and `userId` from the incoming Identify payload and forwards them to Survicate. For each key–value pair in `traits`, the function calls `setUserTrait(key,value)`. It also sends the `userId` as a trait by calling `setUserTrait('userId',userId)`.

#### Track

The `SurvicateDestination` plugin maps Segment [Track](/docs/segment/connections/spec/track) events to the Survicate `invokeEvent` method. This means when you track an event in Segment, it will be invoked in Survicate.

#### Screen

The `SurvicateDestination` plugin maps the Segment [Screen](/docs/segment/connections/spec/screen) method to the Survicate `enterScreen` method. This means when you track a screen in Segment, it will be entered in Survicate.

#### Reset

The `SurvicateDestination` plugin maps the Segment Reset method to the Survicate `reset` method. This means when you reset the Segment client, the Survicate client will also be reset.
