Initialize the React Native SDK
Rokt partners can use the Rokt SDK for React Native applications to display overlay or embedded placements while Rokt advertisers can use it to record conversions for their campaigns. All use cases of the SDK follow the same initial steps to integrate and initialize the SDK in your application. At a high level, these steps are:
- Integrating the SDK into your React Native application
- Configuring for Android applications
- Configuring for iOS applications
- Initializing the SDK
Following these steps, the execute
method of the SDK can then be used in various ways to suit the required use case.
Integrate the SDK into your React Native application
To integrate the SDK into your React Native application, you need to:
Install the Rokt SDK by running the command:
$ npm install @rokt/react-native-sdk --save
Install all packages and dependencies by running the command:
$ npm install
Configure for Android
To configure the Rokt React Native SDK for Android applications, you need to:
- Add the Rokt SDK into repositories block of build.gradle:
allprojects {
repositories {
...
maven {
url "https://apps.rokt.com/msdk"
}
}
}
- In your
ReactApplication
class, add theRoktEmbeddedViewPackage
togetPackages
:
// import the class
import com.rokt.reactnativesdk.RoktEmbeddedViewPackage;
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
//Add the RoktEmbeddedViewPackage
packages.add(new RoktEmbeddedViewPackage());
return packages;
}
SDK versions below major version 4 uses a different package name com.reactlibrary.RoktEmbeddedViewPackage;
- Check that
multiDexEnabled
is set to true, and you are targeting Mind SDK version 18 or higher:
android {
...
defaultConfig {
...
multiDexEnabled true,
minSdkVersion 21
}
}
If the build fails due to Kotlin version conflict, check the link for possible solutions: https://github.com/facebook/react-native/issues/35979
Configure for iOS
To configure the Rokt React Native SDK for iOS applications, go to the iOS folder and run the command:
cd ios && pod install
Mac M1 architecture is not directly compatible with Cocoapods. If you encounter issues when installing pods, you can solve it by running:
sudo arch -x86_64 gem install ffi
arch -x86_64 pod install
These commands install the ffi
package, to load dynamically-linked libraries and let you run the pod install
properly, and runs pod install
with the proper architecture.
Configure using Expo
The Rokt React Native SDK cannot be used with Expo Go as it requires custom native code. The SDK can be integrated with Expo via both the bare workflow and managed workflow.
Expo Bare Workflow
If using the bare workflow, follow the steps in Configure for Android and Configure for iOS.
Expo Managed Workflow
The suggested workflow is to use a custom development client. If starting a new app, you can run npx create-react-native-app -t with-dev-client
to have this set up automatically. It will also allow you to use the Expo Application Services (EAS Build) for your Android and iOS builds.
To configure the Rokt React Native SDK with the Expo managed workflow, add the @rokt/react-native-sdk
plugin to the plugins array of your app.json
or app.config.js
file:
{
"expo": {
"plugins": ["@rokt/react-native-sdk"]
}
}
If you are not using EAS build then you must use the expo prebuild --clean
command as described in the Adding custom native code guide to rebuild your app with the plugin changes.
Initialize the SDK
The Rokt module provides two methods:
Major Version 3
initialize(roktTagId: string, appVersion: string): void
execute(viewName: string, attributes: Record<string, string>, placeholders: Record<string, number | null>, callback: () => void): void
Major Version 4
initialize(roktTagId: string, appVersion: string, fontFilesMap?: Record<string, string>): void
execute(viewName: string, attributes: Record<string, string>, placeholders: Record<string, number | null>): void
The initialize
method fetches API results that are required by the execute
method. Therefore it’s best to not put both calls immediately next to each other. It is recommended to call the initialize
method within the first few views of the app. It is also recommended to call initialze
within componentDidMount
or useEffect
to prevent multiple unwanted calls.
To initialize the SDK, you need to:
- Import the Rokt SDK into your JavaScript/TypeScript file:
import { Rokt, RoktEmbeddedView } from "@rokt/react-native-sdk";
- Initialize the Rokt SDK:
// The following will reveal a demo integration. To view your integration:
// 1) Replace the integration test tag ID (222) with your unique Rokt Account ID
// 2) Replace Y.Y.Y with the application version
Rokt.initialize("222", "Y.Y.Y");
Receiving initialization status
From version 4.6.0
, the Rokt SDK emits initialization status through 'RoktEvents' channel.
eventManagerEmitter.addListener('RoktEvents', (data) => {
if (data.event == 'InitComplete') {
console.log(`Rokt init completed with status ${data.status}`);
}
});
Debugging
Use the Rokt.setLoggingEnabled(true);
API to enable debug logs from the Rokt SDK.
Next steps
Your next steps depend on the use case of your integration. Check out these resources for more information: