SDK React Native

Last updated March 30, 2021

Introduction

The purpose of this manual is to provide all of the information necessary to install and use the tools in the apps developed for the React Native platform.

This Plugin gathers device data (information and location) and sends it to ClearSale. The data gathered will be related solely to the device and is unrelated to the integrated app.

Geolocation data depends on permission granted by the device user. This means the app must request access to user location data (the SDK does not ask for permission). If the app does not request access or if the user denies permission the data will not be gathered.

SDK respects Apple's and Google's privacy policies in terms of gathering device data and the level of permission granted by the device user.

Checksum

Package Digest
behavior-analytics-sdk/react-native-clear-sale-module sha1-6VAoJWwE2O4R28Z30FtagJOu/jY=

To consult the artifact's checksum, check the package.json.lock, after running the npm install command in your project.

React Native

Dependencies

- Node >= 10.19.0
- React Native >= 0.63.2

React-Navigation (navigation between screens)

- react-navigation/native: 5.7.3
- react-navigation/stack: 5.9.0

Installation

The plugin is available in a private repository. Use requires following the example below:


Adding private repository configuration

1. Add an .npmrc file to the same directory holding package.json in the project.

2. Add the following to the .npmrc:

@behavior-analytics-sdk:registry=https://pkgs.dev.azure.com/PublicPackagesCS/Behavior/_packaging/BehaviorAnalytics.SDK/npm/registry

always-auth=true

 

3. Add ClearSale's SDK React Native to your installation, as per the following example:

"dependencies": {
    "@behavior-analytics-sdk/react-native-clear-sale-module": "1.0.0-rc.2",
    ...
},

4. Run the npm install command.


Adding the plugin to the project

Npm:

npm install @behavior-analytics-sdk/react-native-clear-sale-module@1.0.0-rc.2

Configuration

Android

Requires adding the SDK repository to the project's build.gradle file.

maven {
    url 'https://pkgs.dev.azure.com/PublicPackagesCS/Behavior/_packaging/BehaviorAnalytics.SDK/maven/v1'
    name 'BehaviorAnalytics.SDK.Android'
}

Add the code to enable multidex in the app's build.gradle file.

android {
    defaultConfig {
        ...
        multiDexEnabled true
    }
    ...
}
dependencies {
    implementation 'com.android.support:multidex:1.0.3'
}

Add the method to enable multidex in Android versions older than 5.0 to the app's MainActivity.java class.

First multidex import:

import android.content.Context;
import androidx.multidex.MultiDex;

Then add the method within the class:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

In the MainApplication.javaclass, import:

import androidx.multidex.MultiDexApplication;

Then change the Application legacy to MultiDexApplication.
For further information please go to:

 


IOS

Configuration

Add Clearsale lib native iOS to your project's pod.

source	'https://dev.azure.com/PublicPackagesCS/Behavior/_git/BehaviorAnalytics.SDK.IOS.Specs'
target 'PROJECT_NAME' in the
pod 'CSBehavior', '3.0.1'
end

Still in your project pod, add the following option to pod_install:

post_install in |installer|
...
installer.pods_project.targets.each do |t|
    t.build_configurations.each do |config|
    config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
    end
end
end

Within the project file go to the ios file and run the pod install command:

pod install

If there is an error Error: EMFILE: too many open files - React Native CLI install o watchman.

Run the following commands in the terminal:

brew update
$ brew install watchman

Add the following option to your plist and then in xcode Menu Build -> Clean and Build Folder.

<dict>
        <key>NSAllowsArbitraryLoads</key>
        <true />
        <key>NSExceptionDomains</key>
        <dict>
            <key>clearsale.com.br</key>
            <dict>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.0</string>
            </dict>
        </dict>
    </dict>

Interface Behavior

Provides all methods that communicate SDK. All methods return a Promise.


Methods

 

Method name Platform Description
Promise<String> generateSessionId(appKey) Android / iOS Generates and returns a session identifier. This method should be used only if the app does not generate unique identifiers for each data gathering.

Promise<void> collectDeviceInformation(appKey, sessionId) Android / iOS Gathers data on the device linked to the Session value. SessionId, the session value must be provided as a parameter.
Note:
  • If that SessionID has no unique value use. generateSessionId().
  • The maximum SessionID is 128 characters.
  • The minimum SessionID is 6 characters.
Promise<void> start(appKey, success, failure) Android Initiates the collection of data on the device and screen. Exception: CaptureWasStartedException - launched when a prior capture was not stopped.
Promise<void> stop(appKey, sessionId, success, failure) Android Ends the data capture process.
     

 

Examples

Example of using a plugin


IImporting the pllugin
import ClearSaleModule from 'react-native-clear-sale-module'

Using the start and stop methods during the component life cycle (React Hooks):

useEffect(() => {
    ...
    ClearSaleModule.start(appKey)

    return () => {
        ...
        ClearSaleModule.stop(appKey)
    }
}, [])

Android

The start method must be executed before using the generateSessionId and collectDeviceInformationfunctions.

If a start method is executed before the stop method, a CaptureWasStartedException type of exception will be launched.


Data gathering approach

generateSessionId reutnrs the sessionID.

const loadPage = async () => {
    ...

    await verifyLocationPermissions()

    const sessionId = await ClearSaleModule.generateSessionId(state.token)
    setState({
        ...state,
        sessionId: sessionId
    })

    ...
}

The collectDeviceInformationmethod starts to gather data with an existing sessionId.

useFocusEffect(
    React.useCallback(() => {
        loadPage()

    return () => {
        unloadPage()
        };
    }, [])
);

const loadPage = async () => {
    ClearSaleModule.collectDeviceInformation(state.token, state.sessionId).then(() => {
        console.log('id with success', state.sessionId)
        setLoading(false)
        setSend(true)
    }).catch(() => {
        setLoading(false)
        setSend(false)
    })
}

Capturing the Promise response.
const getSessionId = () => {
    ClearSaleModule.generateSessionId(appKey).then(sessionId => {
        ...
        setSessionId(sessionId)
    }).catch(err => {
        ...
    })
}

Remove

Npm:
npm uninstall @behavior-analytics-sdk/react-native-clear-sale-module

User License

By downloading and using our SDK you are agreeing to the following license.

Copyright © 2021 ClearSale

All rights reserved. This license grants permission to use the software "as is". The software may not be modified or copied for any purpose. The Software is licensed as configured and with absolutely no expressed or implicit guarantee, including but not limited to marketing guarantees, suitability for specific purposes and non-violation of patented rights. Under no circumstances may the owners of the Copyright be held liable for damages, losses, basis for lawsuits due to contract or illegal acts or any other illegal means arising from the use of the Software or other actions related to this Software without the prior written authorization of the copyright holders.