Search…
4. Initialization
Initializing the Sentiance SDK creates the native components necessary to do proper detections. The first SDK initialization additionally creates a unique Sentiance user on the device, identified by a Sentiance user ID. During the initialization process, this Sentiance user can be linked to your app-specific user on the Sentiance platform, making sure that you can properly track your users across both platforms. Additionally, linking allows your users to migrate to new devices, or reinstall your app without losing any historical data. Check our comprehensive user linking guide to find out more about how this works.
To make user linking possible, we provide a React Native method to initialize the SDK in your javascript code. However, since the Sentiance SDK can run in the background without any user interaction, we also require that you initialize the SDK natively during app startup.

Native Initialization

In order for SDK detections to work in the background, you must initialize the Sentiance SDK natively during app startup. Doing so ensures that Sentiance can properly detect the user's activity even when the user does not have your app open.
We initialize the SDK natively only after a previous React Native initialization was successfully complete. This makes sure that our first SDK initialization happens in React Native, allowing us to perform user linking.

iOS

The correct way to natively initialize on iOS is to do it inside the didFinishLaunchingWithOptions method of the AppDelegate class.
1
...
2
#import <RNSentiance.h> // Import Sentiance React Native bridge module
3
4
@implementation AppDelegate
5
6
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
7
{
8
9
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
10
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
11
moduleName:@"ReactNativeApp"
12
initialProperties:nil];
13
14
// Read SENTIANCE_APP_ID and SENTIANCE_APP_SECRET from any safe source
15
NSString * SENTIANCE_APP_ID = @"";
16
NSString * SENTIANCE_APP_SECRET = @"";
17
18
// Check if native initialization is enabled. In your JS code, there's an equivalent
19
// setter that you can use to enable it, after successfully initializing the SDK there.
20
BOOL isNativeInitializationEnabled = [[bridge moduleForName:@"RNSentiance"] isNativeInitializationEnabled];
21
22
if (isNativeInitializationEnabled) {
23
// initialize the SDK
24
[[bridge moduleForName:@"RNSentiance"] initSDK:SENTIANCE_APP_ID secret:SENTIANCE_APP_SECRET baseURL:nil shouldStart:YES resolver:nil rejecter:nil];
25
}
26
27
...
28
29
return YES;
30
}
31
...
Copied!

Android

The correct way to natively initialize on Android is to do it inside the onCreate() method of the Application class.
1
...
2
import com.sentiance.react.bridge.RNSentianceHelper;
3
4
public class MainApplication extends Application implements ReactApplication {
5
6
@Override
7
public void onCreate() {
8
super.onCreate();
9
...
10
11
String SENTIANCE_APP_ID = "";
12
String SENTIANCE_APP_SECRET = "";
13
14
RNSentianceHelper rnSentianceHelper = RNSentianceHelper.getInstance(getApplicationContext());
15
16
// Check if native initialization is enabled. In your JS code, there's an equivalent
17
// setter that you can use to enable it, after successfully initializing the SDK there.
18
boolean isNativeInitializationEnabled = rnSentianceHelper.isNativeInitializationEnabled();
19
20
if (isNativeInitializationEnabled) {
21
// initialize the SDK
22
rnSentianceHelper.initializeSentianceSDK(
23
SENTIANCE_APP_ID,
24
SENTIANCE_APP_SECRET,
25
true, // auto start
26
null, // init callback
27
null // start callback
28
);
29
}
30
}
31
32
...
33
}
Copied!

React Native Initialization

Having implemented the native SDK initialization, we're now ready to implement the React Native SDK initialization.
Initializing the SDK in React Native should normally happen only once, when the Sentiance SDK will be initialized for the first time. This first initialization creates a Sentiance user on the device, and allows you to link your app-specific user to this Sentiance user.
1
async initSDK() {
2
const sdkNotInitialized = await RNSentiance.getInitState() === "NOT_INITIALIZED";
3
4
if (sdkNotInitialized) {
5
const appId = "";
6
const appSecret = "";
7
8
// Before initializing, subscribe to receive the user linking event.
9
subscribeSDKEvents();
10
11
// Initialize the SDK.
12
await RNSentiance.initWithUserLinkingEnabled(
13
appId,
14
appSecret,
15
null,
16
true
17
);
18
19
// Now that initialization has succeeded, enable native initialization so
20
// that the SDK is initialized from the native code upon next app startup.
21
await RNSentiance.enableNativeInitialization();
22
}
23
...
24
}
Copied!
Notice that we enable the native SDK initialization after we successfully initialize the SDK, by calling RNSentiance.enableNativeInitialization(). This will result in the isNativeInitializationEnabled check we added in the native code to succeed next time.
When you reset the Sentiance SDK, native initalization will be automatically disabled, so you don't need to manually call RNSentiance.disableNativeInitialization().
In the above snippet, while initWithUserLinkingEnabled executes, the Sentiance SDK will publish an SDKUserLink event allowing you to perform user linking.
1
const rnSentianceEmitter = new NativeEventEmitter(RNSentiance);
2
3
subscribeSDKEvents() {
4
this.userLinkListener = rnSentianceEmitter.addListener(
5
"SDKUserLink",
6
id => {
7
const { installId } = id;
8
this.linkMetaUser(installId);
9
}
10
);
11
}
12
13
async linkMetaUser(installId) {
14
// Supply the 'installId' to your server, which should then initiate
15
// a user linking request with the Sentiance backend.
16
...
17
18
// Inform the Sentiance SDK about the user linking result.
19
RNSentiance.userLinkCallback(success);
20
}
Copied!
The RNSentiance.userLinkCallback(boolean) call is important. Without it, the Sentiance SDK will not complete the initialization. If linking succeeds, this SDKUserLink event does not get emitted during subsequent SDK initializations, unless the SDK gets reset.