Follow these instructions to set up Iterable's React Native SDK for Unknown User Activation. For general setup of the SDK, see Installing Iterable's React Native SDK.
NOTE
Sample code in this article is for illustration. Adapt it to your app. Values shown as placeholders (for example, API keys or tokens) aren't real secrets.
# In this article
# Requirements
Unknown User Activation on React Native requires Iterable's React Native SDK
4.0.0 or later. Set IterableConfig.enableUnknownUserActivation to true and
obtain visitor consent with Iterable.setVisitorUsageTracked, as described
below.
# Step 1: Import SDK classes
In any file that initializes or configures the SDK, import what you need:
import { Iterable, IterableConfig } from '@iterable/react-native-sdk';
# Step 2: Initialize the SDK and set up handlers
Configure the SDK, turn on Unknown User Activation, and wire JWT and unknown user callbacks when your project uses a JWT-enabled mobile API key.
let pendingUnknownUserId: string | null = null; const config = new IterableConfig(); config.enableUnknownUserActivation = true; config.eventThresholdLimit = 100; config.enableForegroundCriteriaFetch = true; config.identityResolution = { replayOnVisitorToKnown: true, mergeOnUnknownToKnown: true, }; config.unknownUserHandler = (userId: string) => { pendingUnknownUserId = userId; }; config.authHandler = async () => { const unknownUserId = pendingUnknownUserId; pendingUnknownUserId = null; const id = unknownUserId ?? (await Iterable.getUserId()) ?? (await Iterable.getEmail()); return id ? fetchJwtForUser(id) : null; }; // Calls your JWT server for this user ID or email. async function fetchJwtForUser(id: string): Promise<string> { const response = await fetch( `https://your-server.example.com/jwt?id=${encodeURIComponent(id)}`, ); return response.text(); } await Iterable.initialize('<YOUR_MOBILE_API_KEY>', config);
Create an IterableConfig object, set the fields you need, and pass it to
Iterable.initialize(apiKey, config). For more on JWT setup, see
JWT Authentication with Iterable's React Native SDK.
These IterableConfig fields control Unknown User Activation:
enableUnknownUserActivation(boolean, defaultfalse)
Whentrue, the SDK creates an unknown user profile when your project's criteria are met and the visitor has consented. This setting alone doesn't store visitor data. You also need consent throughsetVisitorUsageTracked, described in Step 3.eventThresholdLimit(number, default100)
Maximum number of recent visitor-side events the SDK keeps locally before an unknown profile exists or before identification. Older events drop first when the limit is exceeded.enableForegroundCriteriaFetch(boolean, defaulttrue)
Whentrue, the SDK can refresh unknown user criteria when the app enters the foreground. Set tofalseif you only want criteria fetched at launch (and when consent-driven flows run).-
identityResolution(IterableIdentityResolutionobject, both flags default totrue)replayOnVisitorToKnown: When you callsetEmailorsetUserIdfor a visitor who hasn't yet received an unknown profile,truesends locally stored visitor data to the known profile, andfalsedoesn't.mergeOnUnknownToKnown: When you identify someone who already has an unknown profile,truemerges that profile into the known profile, andfalsedoesn't.
React Native uses
mergeOnUnknownToKnown, the same name as the native iOS, Android, and Web SDKs. unknownUserHandler(optional callback,(userId: string) => void)
Called when the native SDK creates a unique user ID for a new unknown user, before identity has switched for JWT. If you need the ID for token minting, storeuserIdsynchronously in this callback. Don't callIterable.getUserId()insideunknownUserHandler, because native identity hasn't updated yet.authHandler(required when using a JWT-enabled API key)
Return a JWT string or anIterableAuthResponse. For unknown user creation, read the pending ID fromunknownUserHandlerand clear it. For later refreshes and known users, fall back toIterable.getUserId(). For users identified by email,getUserId()returnsnull, so fall back toIterable.getEmail()and mint the JWT for the email.
IMPORTANT
If you don't use a JWT-enabled API key, don't set authHandler.
# Step 3: Get user consent and track local visitor data
Unknown User Activation stores visitor-side events locally only when both of these are true:
enableUnknownUserActivationistruein the config you pass toinitialize.The user has opted in through
Iterable.setVisitorUsageTracked(true).
Call setVisitorUsageTracked only after Iterable.initialize has resolved.
Before that, the call is a no-op, and getVisitorUsageTracked resolves to
false.
// After `Iterable.initialize` from Step 2 resolves, and only when the user // grants consent (not on every launch): Iterable.setVisitorUsageTracked(true);
When the user revokes consent, call setVisitorUsageTracked(false). That
clears locally stored visitor data. Read the current flag with
getVisitorUsageTracked().
WARNING
Each setVisitorUsageTracked(true) call clears any prior local visitor
storage before tracking starts again. Don't call it on every app launch unless
you intend to discard unsubmitted visitor data from earlier sessions.
After consent, track events and profile updates with the usual Iterable
methods (trackEvent, trackPurchase, updateCart, updateUser, and so on).
Those calls follow the same Unknown User Activation rules as on native iOS and
Android.
# Step 4: Create an unknown user profile
When a visitor meets your project's profile creation criteria, the native SDK
creates an unknown user profile and replays stored visitor data to it. It also
calls your unknownUserHandler, and then your authHandler if you use a
JWT-enabled API key.
For details about how visitors become unknown and then known users, see Configure the iOS SDK and Configure the Android SDK.
# Step 5: Identify the user
When you know the current user's userId or email (depending on your Iterable
project type), provide it to the SDK by calling setUserId or setEmail on
Iterable.
// Identify the user by email or userId. Iterable.setEmail('user@example.com'); Iterable.setUserId('<USER_ID>');
React Native doesn't expose a per-call identity override on setEmail or
setUserId. Defaults come only from IterableConfig.identityResolution.
If the current user is a visitor (they don't have an unknown profile, because they haven't yet satisfied your project's profile creation criteria):
-
If
replayOnVisitorToKnownistrue, the SDK:- Calls
authHandlerto fetch a JWT for the known user profile (if you use a JWT-enabled API key). - Sends visitor data (including the consent timestamp) from local storage to the known user profile in Iterable. This creates the known user profile if it doesn't already exist.
- Clears visitor data from local storage.
- Sends future user updates and events to the known user profile in Iterable.
- Calls
-
If
replayOnVisitorToKnownisfalse, the SDK:- Calls
authHandlerto fetch a JWT for the known user profile (if you use a JWT-enabled API key). - Clears visitor data from local storage without sending it to Iterable.
- Sends future user updates and events to the known user profile in Iterable. If the known user profile doesn't exist yet, these updates create it.
- Calls
If the current user is unknown (they have an unknown user profile in Iterable):
-
If
mergeOnUnknownToKnownistrue, the SDK:- Calls
authHandlerto fetch a JWT for the known user profile (if you use a JWT-enabled API key). - Calls the User Merge API to merge the unknown user profile into the known user profile, including all data. It can take a few minutes for all of the data to appear on the known user profile.
- Sends future user updates and events to the known user profile.
- Calls
-
If
mergeOnUnknownToKnownisfalse, the SDK:- Calls
authHandlerto fetch a JWT for the known user profile (if you use a JWT-enabled API key). - Doesn't call the User Merge API.
- Sends future user updates and events to the known user profile. The unknown user profile remains in Iterable.
- Calls