Follow these instructions to set up Iterable's Web SDK for Unknown User Activation. For general guidance about setting up Iterable's Web SDK, see Setting up Iterable's Web SDK and Web SDK GitHub README.
NOTE
Sample code shown in the following sections is for demonstration purposes only — it's not exhaustive, and it's not meant to be used directly in your website. Instead, use it as a guide to understand the various ways you'll interact with the SDK when setting up and using Unknown User Activation
# In this article
# Step 1: Import SDK methods
Import some methods you'll use to set up and use Unknown User Activation:
import { initializeWithConfig, updateUser, updateCart, track, trackPurchase, } from "@iterable/web-sdk";
-
initializeWithConfigallows you to initialize the SDK, and pass in some configuration options -
updateUserallows you to track user profile updates, either locally (for visitors) or in Iterable (for unknown or known users) -
updateCartallows you to track cart update events, either locally (for visitors) or in Iterable (for unknown or known users) -
trackallows you to track custom events, either locally (for visitors) or in Iterable (for unknown or known users) -
trackPurchaseallows you to track purchase events, either locally (for visitors) or in Iterable (for unknown or known users)
# Step 2: Initialize the SDK
Call initializeWithConfig to initialize the SDK. Destructure the various
methods it returns, so you can call them as needed (clearRefresh, setEmail,
setUserID, logout, refreshJwtToken, setVisitorUsageTracked).
const { clearRefresh, setEmail, setUserID, logout, refreshJwtToken, setVisitorUsageTracked } = initializeWithConfig({ authToken: <YOUR_ITERABLE_WEB_API_KEY>, configOptions: { enableUnknownActivation: true, identityResolution: { replayOnVisitorToKnown: true, mergeOnUnknownToKnown: true, }, eventThresholdLimit: 100, onUnknownUserCreated: (unknownId) => { // Tell your server about the unknown userId value, if necessary } }, generateJWT: ({ email, userID }) => { // From your server, fetch a JWT token for your website's unknown or // known user, and return it as a string. This method receives either userID // or email as a parameter, as necessary. } });
When calling initializeWithConfig, provide a JWT-enabled Web API key for authToken.
Then, in configOptions, set the following options:
-
To enable Unknown User Activation, set
enableUnknownActivationtotrue. This property defaults tofalse.NOTE
The Web SDK uses
enableUnknownActivation(without "User" in the name), while the iOS and Android SDKs useenableUnknownUserActivation. -
If you need to override the default values, provide an
identityResolutionobject. If you don't provide anidentityResolutionvalue, the SDK defaults bothreplayOnVisitorToKnownandmergeOnUnknownToKnowntotrue. Use this object to specify:replayOnVisitorToKnown– When you identify a visitor by callingsetEmailorsetUserID, this field specifies whether the SDK should replay locally saved visitor data to their known user profile in Iterable. Defaults totrue. (When an unknown user profile is first created, the SDK always replays locally saved data to that profile. This setting only controls what happens when you identify a visitor before they receive an unknown user profile.)mergeOnUnknownToKnown– When you identify an unknown user by callingsetEmailorsetUserID, this field specifies whether the SDK should merge the unknown user profile with the identified user profile. Defaults totrue. (If the identified user profile does not yet exist, a merge operation creates it. WhenmergeOnUnknownToKnownisfalse, the new user profile is not created until the SDK tracks a user update or an event. Without a merge, data on the unknown user profile is lost.)
NOTE
The Android and Web SDKs use
mergeOnUnknownToKnown, while the iOS SDK usesmergeOnUnknownUserToKnown. -
Specify an
eventThresholdLimitto indicate how many of a visitor's most recent events the SDK should save in local storage, so that they can be synced to Iterable when the user satisfies your profile creation criteria and receives an unknown user profile.This value defaults to 100. If a visitor triggers more than the maximum number of events before converting to an unknown user, the earliest saved events are deleted first.
-
(Optional, but strongly recommended) Provide an
onUnknownUserCreatedcallback for the SDK to call after it creates auserIdfor a new unknown user, but before it tries to fetch a JWT token for that user.You can use this callback to notify your server about the SDK-generated
userIdfor the new unknown user. This lets your server create JWT tokens for thatuserIdin future JWT requests. To do this, set up an authenticated web service that this callback can call to inform your server about the new unknownuserId.IMPORTANT
Since the Web SDK requires JWT-enabled API keys, your JWT server needs to know about SDK-generated unknown
userIdvalues before it can issue tokens for them. This callback is the mechanism for that — without it, your JWT server may fail to issue tokens for new unknown users.
The Web SDK refreshes profile creation criteria on each page load. Unlike the
iOS and Android SDKs, this article does not document a separate
enableForegroundCriteriaFetch configuration option for Web.
-
Provide a
generateJWTmethod to fetch a JWT token (from your server) for the current unknown or known user. Since Iterable's Web SDK requires JWT-enabled API keys, this step is required.The method should take the passed-in
userIDoremailand call your JWT server to fetch a valid JWT token for the signed-in user, whether unknown or known. The SDK can then use that JWT token for future API requests.- For unknown users, this method always receives a
userIDparameter (an SDK-generated UUID value). - For known users, this method receives either an
emailor auserIDparameter, depending on how you've identified the user to the SDK (by callingsetEmailorsetUserID).
- For unknown users, this method always receives a
# Step 3: Get user consent and track local data
Before the SDK tracks local data about the current visitor, get their consent to do so. Then, explicitly tell the SDK to start tracking local data.
// Call after you have user consent to track local data setVisitorUsageTracked(true); // Call to revoke user consent, if necessary setVisitorUsageTracked(false); // Call to track an update cart event updateCart(data).then((response) => { console.log("Updated cart: ", response); }).catch((error) => { console.error("Error updating cart: ", error); }); // Call to track a purchase trackPurchase(data).then((response) => { console.log("Tracked purchase: ", response); }).catch((error) => { console.error("Error tracking purchase: ", error); }); // Call to track a custom event track(data).then((response) => { console.log("Tracked event: ", response); }).catch((error) => { console.error("Error tracking event: ", error); }); // Call to update user profile data updateUser(data).then((response) => { console.log("Updated user: ", response); }).catch((error) => { console.error("Error updating user: ", error); });
When you have user consent, call setVisitorUsageTracked(true). When you do this:
- The SDK fetches profile creation criteria for your Iterable project (and refreshes them on each page load).
- For calls to
updateCart,trackPurchase,updateUser, andtrack, the SDK stores data in local storage (this data isn't sent to Iterable yet — it's only stored locally).
If the user revokes consent, call setVisitorUsageTracked(false). This clears
any locally saved visitor data, and it prevents local storage of visitor data
until setVisitorUsageTracked(true) is called again.
WARNING
Calling setVisitorUsageTracked(true) clears any previously stored local
visitor data (events, user updates, and session data) before starting fresh
tracking. If your website calls this method on each page load, any visitor data
stored from a previous page that was not yet synced to Iterable will be lost.
To track events and user updates, call updateCart, trackPurchase, track,
and updateUser, wherever needed.
# Step 4: Create an unknown user profile
If and when the website visitor satisfies your Iterable project's profile creation criteria, the SDK creates an unknown user profile for them, as described here.
- The SDK generates a userId (a UUID) for a new unknown user profile.
- The SDK calls the consent endpoint and logs the time of consent and user type.
It then calls
POST /api/unknownuser/events/sessionto create the unknown user profile in Iterable and adds anunknownSessionevent on the unknown user profile (which you can use to trigger a system webhook). - The SDK calls your
onUnknownUserCreatedcallback, as described above. - Calls your
generateJWTmethod, to fetch a JWT token to use for the new unknownuserId. - The SDK replays locally saved visitor data (cart updates, purchase, user profile updates, and custom events) to the unknown user profile in Iterable, and then removes it from local storage.
NOTE
In the Web SDK, consent tracking is logged before the unknownSession event is
created. This differs from the iOS and Android SDKs, which log consent later in
their unknown-user flows.
# Step 5: Identify the user
When you know the current user's user ID or email (depending on your Iterable
project type), identify them to the SDK by calling setUserID or setEmail.
When you identify the user:
// Call to identify the user by userId setUserID(userId, identityResolutionOverride).then((response) => { console.log("Set userId: ", response); }).catch((error) => { console.error("Error setting userId: ", error); }); // Call to identify the user by email setEmail(email, identityResolutionOverride).then((response) => { console.log("Set email: ", response); }).catch((error) => { console.error("Error setting email: ", error); });
If the current user is a visitor (a user who doesn't have an unknown profile in Iterable, because they haven't yet satisfied your Iterable project's profile creation criteria):
-
If
replayOnVisitorToKnownis set totrue, the SDK:- Calls
generateJWTto fetch a JWT token for the known user profile. - Sends visitor data from the browser's local storage (user profile data and events) to the known user profile in Iterable. Sending this data to Iterable creates the known user profile in Iterable if it doesn't already exist.
- Clears visitor data from local storage.
- Sends future user updates and events to the known user profile to Iterable (not local storage).
- Calls
-
If
replayOnVisitorToKnownis set tofalse, the SDK:- Calls
generateJWTto fetch a JWT token for the known user profile. - Clears visitor data from local storage, without sending it to Iterable.
- Sends future user updates and events to the known user profile in Iterable (not to local storage). If the known user profile doesn't yet exist in your Iterable project, these updates create it.
- Calls
If the current user is unknown (has been given an unknown user profile in Iterable):
-
If
mergeOnUnknownToKnownis set totrue, the SDK:- Calls
generateJWTto fetch a JWT token for the known user profile. - Calls the User Merge API to merge the unknown user profile with the known user profile (including all data).
- If the known profile doesn't yet exist, this API call creates it.
- Then, the API deletes the unknown profile.
- It can take a few minutes for all of the data from the unknown user profile to appear on the known user profile.
- Sends future user updates and events to the known user profile in Iterable (not to the unknown user profile).
- Calls
-
If
mergeOnUnknownToKnownis set tofalse, the SDK:- Calls
generateJWTto fetch a JWT token for the known user profile. - Does not call the User Merge API.
- Sends future user updates and events to Iterable, to the known user profile (not to the unknown user profile). The unknown user profile remains in Iterable.
- Calls