iOS SDK Integration Guide
This page explains how to implement the iOS SDK for Rokt Ecommerce to deliver more relevant customer experiences at checkout. The SDK allows you to trigger and track these experiences (like displaying offers on confirmation pages) by firing on configured pages and passing user and transaction data back to Rokt for personalization and measurement.
Your dedicated account representative will help configure your account for the iOS SDK. They will provide you with both the key and secret required to initialize the SDK and additional resources needed to render the most relevant experiences for your customers.
These instructions require development resources to complete. If you require additional assistance, please reach out to your Rokt account manager. Shopify stores can set up a Rokt placement in seconds using the Rokt Ecommerce app — no coding needed!
1. Add the Rokt SDK to your iOS app
Using either SPM or CocoaPods, include the Rokt SDK in your application:
CocoaPods
To integrate the SDK using CocoaPods, specify it in your Podfile with:
pod 'mParticle-Apple-SDK', '~> 8.0'
pod 'mParticle-Rokt','~> 8.0'
SPM
To integrate the SDK using Swift Package Manager:
- Open your project in Xcode, and go to the "Package Dependencies" tab.
- Click the + button under the Packages list.
- Enter the repository URL
https://github.com/mParticle/mparticle-apple-sdk
in the search box on the top right, choosemparticle-apple-sdk
from the list of packages, and change "Dependency Rule" to "Up to Next Major Version". - Click the "Add Package" button on the bottom right, and choose either the "Package Product" called
mParticle-Apple-SDK
. If you'd like to use a version of the SDK that doesn't include location tracking support, choosemParticle-Apple-SDK-NoLocation
. - Repeat steps 3 & 4 for the Rokt Kit repository URL
https://github.com/mparticle-integrations/mparticle-apple-integration-rokt.git
.- If you choose the
mParticle-Apple-SDK-NoLocation
package product, you will need to import the SDK usingimport mParticle_Apple_SDK_NoLocation
instead ofimport mParticle_Apple_SDK
.
- If you choose the
2. Initialize the Rokt SDK
To initialize the SDK, insert the following initialization snippet in your AppDelegate file:
- Make sure to replace
your-key
andyour-secret
with the key and secret provided by your dedicated Rokt team.
- Swift
- Objective-C
import mParticle_Apple_SDK
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize the SDK
let options = MParticleOptions(key: "your-key",
secret: "your-secret")
// Specify the data environment with environment:
// Set it to .development if you are still testing your integration.
// Set it to .production if your integration is ready for production data.
// The default is .autoDetect which attempts to detect the environment automatically
options.environment = .development
// Identify the current user:
let identifyRequest = MPIdentityApiRequest.withEmptyUser()
// If you're using an un-hashed email address, set it in 'email'.
identifyRequest.email = "j.smith@example.com"
// If the user is identified with their email address, set additional user attributes.
options.identifyRequest = identifyRequest
options.onIdentifyComplete = {(result: MPIdentityApiResult?, error: Error?) in
if let user = result?.user {
user.setUserAttribute("example attribute key", value: "example attribute value")
} else {
//handle failure - see "Error Handling" section below
}
}
MParticle.sharedInstance().start(with: options)
return true
}
#import <mParticle_Apple_SDK/mParticle.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize the SDK
MParticleOptions *options = [MParticleOptions optionsWithKey:@"your-key"
secret:@"your-secret"];
// Specify the data environment with environment:
// Set it to MPEnvironmentDevelopment if you are still testing your integration.
// Set it to MPEnvironmentProduction if your integration is ready for production data.
// The default is MPEnvironmentAutoDetect which attempts to detect the environment automatically
options.environment = MPEnvironmentDevelopment;
// Identify the current user:
// If you do not have the user's email address, you can pass in a null value
MPIdentityApiRequest *identifyRequest = [MPIdentityApiRequest requestWithEmptyUser];
// If you're using an un-hashed email address, set it in 'email'.
identifyRequest.email = @"j.smith@example.com";
options.identifyRequest = identifyRequest;
// If the user is identified with their email address, set additional user attributes.
options.onIdentifyComplete = ^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
if (apiResult) {
[apiResult.user setUserAttribute:@"example attribute key"
value:@"example attribute value"];
} else {
//handle failure - see https://docs.mparticle.com/developers/sdk/ios/idsync/#error-handling
}
};
[[MParticle sharedInstance] startWithOptions:options];
return YES;
}
2.1 Identify the User on initialization
When the SDK is initialized, it can identify the current user so that any collected data is correctly attributed to them and to ensure they are shown the most relevant ads based on their behavior.
The SDK initialization script includes an object called identifyRequest:
- Swift
- Objective-C
let identifyRequest = MPIdentityApiRequest.withEmptyUser()
identifyRequest.email = "j.smith@example.com"
options.identifyRequest = identifyRequest
MPIdentityApiRequest *identifyRequest = [MPIdentityApiRequest requestWithEmptyUser];
identifyRequest.email = @"j.smith@example.com";
options.identifyRequest = identifyRequest;
2.2 Set additional user attributes
The initialization script includes a callback function that allows you to set additional user attributes for the user if they are successfully identified with their email address:
- Swift
- Objective-C
options.onIdentifyComplete = {(result: MPIdentityApiResult?, error: Error?) in
if let user = result?.user {
user.setUserAttribute("example attribute key", value: "example attribute value")
} else {
//handle failure - see "Error Handling" section below
}
}
options.onIdentifyComplete = ^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
if (apiResult) {
[apiResult.user setUserAttribute:@"example attribute key"
value:@"example attribute value"];
} else {
//handle failure - see https://docs.mparticle.com/developers/sdk/ios/idsync/#error-handling
}
};
3. Identify the User as data becomes available
Whenever the user provides their email address after the SDK has initialized (for example, when they log in or make a purchase), you should call the identify method to pass their email to Rokt. This ensures that data is correctly attributed to the current user.
3.1 Create an identifyRequest
To identify the user, first create an identifyRequest
object to contain the user’s email address.
If you are providing an un-hashed email address, use:
- Swift
- Objective-C
let identifyRequest = MPIdentifyApiRequest.withEmptyUser()
identifyRequest.email = "j.smith@example.com"
MPIdentifyApiRequest *identifyRequest = [MPIdentifyApiRequest requestWithEmptyUser];
identifyRequest.email = @"j.smith@example.com";