This repository contains the React Native SDK Rokt.Widget and a Sample Application RoktSampleApp.
The React Native SDK enables you to integrate Rokt into your React Native mobile apps to drive more value from—and for—your customers. The SDK is built to be lightweight, secure, and simple to integrate and maintain, resulting in minimal lift for your engineering team. The RoktSampleApp includes bare-minimum UI to demonstrate the usage of React Native SDK for partners.
For detailed information about each component, please refer to:
- Rokt.Widget README - Documentation for the SDK implementation
- RoktSampleApp README - Guide for the sample application
The React Native SDK uses a bridge architecture to connect your JavaScript/TypeScript application code to the native Rokt Widget SDK implementations:
- API Interface: The SDK provides a TypeScript interface in
Rokt.tsxthat exports methods likeinitialize(),execute(), andexecute2Step() - Module Definition: The TypeScript layer defines a
RNRoktWidgetinterface that maps to the native modules
- Swift Bridge Classes:
RNRoktWidget: Main bridge class implementingRCTBridgeModulethat exports methods to React NativeRoktEventManager: Handles events and callbacks between the Swift implementation and JavaScript- The bridge translates React Native method calls to the corresponding native Rokt iOS SDK methods
- Kotlin Bridge Classes:
RNRoktWidgetModule: Main Kotlin class that extendsReactContextBaseJavaModuleto implement the bridgeRoktEmbeddedViewManager: Manages embedded view components and UI rendering- The module takes React Native parameters and converts them to the format needed by the native Rokt Android SDK
When you call methods from the TypeScript layer, the following happens:
- Your JavaScript/TypeScript code invokes methods on the
Roktobject - The React Native bridge serializes parameters and routes the call to the appropriate native module (
RNRoktWidgeton iOS orRNRoktWidgetModuleon Android) - The native module translates the call to the corresponding native Rokt Widget SDK method (e.g.,
Rokt.initialize(),Rokt.execute()) - Results and callbacks are serialized and passed back through the bridge to your JavaScript code
- Initialization: Call
Rokt.initialize()with your ROKT_TAG_ID to prepare the SDK - Placement Execution: Call
Rokt.execute()with template and user attributes to fetch and display Rokt content - Callback Handling: Process the onLoad callback once content is loaded
Behind the scenes, the SDK handles:
- API communication with Rokt's backend services
- Content rendering in the appropriate placement locations
- Event tracking and user interaction
The sample application demonstrates both placement types with a minimal implementation that you can use as a reference.
- Install ReactNative development environment by following the React Native environment setup instructions.
- This project requires a minimum
Rubyversion2.7.6. Check by runningruby -vin your terminal. - Clone this repository to your local machine.
- Run
npm installto install all dependencies.
Make sure you have Android Studio installed and properly configured with the required SDK versions.
For iOS development, ensure you have:
- Xcode installed
- CocoaPods installed (
sudo gem install cocoapods)
- Make your code changes in the appropriate files
- Test changes using the RoktSampleApp:
- Android:
npx react-native run-android - iOS:
npx react-native run-ios
- Android:
This SDK is published to a NPM package repository. Publishing is automated through GitHub Actions workflows.
The SDK is automatically published to NPM when the VERSION file is updated and pushed to the main branch. The GitHub Actions workflow (release-from-main.yml) handles:
- Building and testing the package
- Running iOS and Android tests
- Publishing to NPM with the appropriate dist tag
- Creating a GitHub release
To create a new release:
- Use the "Create draft release" workflow to bump the version (major, minor, or patch)
- Review and merge the generated pull request
- Once merged to
main, the release workflow will automatically publish to NPM
The appropriate dist tag will be applied automatically when publishing if one is set (e.g. 1.2.3-alpha.1 will set the dist tag as alpha). If not, the default latest tag will be used.
You can also release the SDK manually by following the steps in the above section.
To add this SDK to your application:
-
Go to the root of your project in your terminal and run:
npm install @rokt/react-native-sdk --save npm install
The Rokt SDK is available from Maven Central and will be resolved automatically via React Native autolinking. No manual repository or package configuration is required.
Ensure your app meets the minimum requirements:
android {
defaultConfig {
minSdkVersion 21
}
}Navigate to the iOS folder and run:
pod installThis package cannot be used with the "Expo Go" app because it requires custom native code. Integration with Expo is supported in both bare workflow and managed workflow.
No additional configuration is required. React Native autolinking will automatically set up the SDK.
Since Expo Go does not support custom native code, you need to use a custom development client.
-
Install the required packages:
npm install @rokt/react-native-sdk expo-dev-client
-
(Optional) Add the config plugin to your
app.jsonorapp.config.js:{ "expo": { "plugins": ["@rokt/react-native-sdk"] } }Note: The config plugin is optional as React Native autolinking handles the native setup automatically.
-
Generate the native projects and build:
npx expo prebuild --clean npx expo run:ios # or npx expo run:android
For production builds, you can use EAS Build which will handle the native compilation automatically.
The Rokt Module provides two methods:
initialize(string ROKT_TAG_ID, string AppVersion)execute(string TemplateVersion, object UserAttributes, object placements, function onLoad)
The Initialize Method will fetch API results that Execute Method would need, so best not to place both calls next to each other.
import { Rokt, RoktEmbeddedView } from "@rokt/react-native-sdk";Rokt.initialize("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "1.0");attributes = {
email: "[email protected]",
firstname: "Jenny",
lastname: "Smith",
mobile: "(323) 867-5309",
postcode: "90210",
country: "US",
};
Rokt.execute("RoktExperience", attributes, null, () =>
console.log("Placement Loaded"),
);constructor(props) {
super(props);
this.placeholder1 = React.createRef();
}In render():
<RoktEmbeddedView
ref={this.placeholder1}
placeholderName={"RoktEmbedded1"}
></RoktEmbeddedView>placeholders = {
RoktEmbedded1: findNodeHandle(this.placeholder1.current),
};
attributes = {
email: "[email protected]",
firstname: "Jenny",
lastname: "Smith",
mobile: "(323) 867-5309",
postcode: "90210",
country: "US",
};
Rokt.execute("RoktEmbeddedExperience", attributes, placeholders, () =>
console.log("Placement Loaded"),
);- React Native (core dependency)
- Native modules for both Android and iOS platforms
- For Android: Rokt SDK from Maven Central (resolved automatically)
- For iOS: Rokt-Widget pod (resolved automatically via CocoaPods)
When developing on Mac M1 machines, follow these additional steps:
- Make sure cocoa pods are installed using gem not brew:
brew uninstall cocoapods
brew uninstall --ignore-dependencies ruby
sudo gem install cocoapods- When integrating with an existing app, ensure the React Native version is compatible with the SDK.
- For Android, MultiDex is required as the SDK increases method count.
- For iOS, ensure CocoaPods is properly installed and initialized.
- Always call
initialize()beforeexecute()and allow sufficient time between the calls.
Copyright 2020 Rokt Pte Ltd
Licensed under the Rokt Software Development Kit (SDK) Terms of Use Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at https://rokt.com/sdk-license-2-0/