As early as possible, ideally within UIApplicationDelegate -willFinishLaunchingWithOptions:, do the following.
Enable logging in DEBUG builds.
Determine if the user is a returning known or authenticated user. If so, set the userId as soon as the authenticated user ID is known, ideally before start.
Determine whether the user has consented to services. If yes, provide the app’s Personalization configuration and start Personalization using startWithClientConfiguration:, ideally before the user begins interacting with your app, as shown in the following examples.
Objective-C example of starting Personalization
1// In AppDelegate.m2#import <Evergage/Evergage.h>34- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {56 Evergage *evergage = [Evergage sharedInstance];78 #ifdef DEBUG9 evergage.logLevel = EVGLogLevelWarn;10 #endif1112 // Recommended to set the authenticated user's ID as soon as known:13 evergage.userId = @"theAuthenticatedUserId";1415 // If using Firebase Messaging, be sure to uncomment and do these before starting Evergage:16 // [FIRApp configure];17 // [FIRMessaging messaging].delegate = self; // Or whatever your delegate is1819 // Start Evergage with your Evergage Configuration:20 [evergage startWithClientConfiguration:^(EVGClientConfigurationBuilder * _Nonnull builder) {21 builder.account = @"yourEvergageAccount";22 builder.dataset = @"yourEvergageDataset";23 builder.usePushNotifications = YES; // If you want to use Evergage push notification campaigns24 }];2526 // ... existing code from your app starts here2728 return YES;29}
Swift example of starting Personalization
1// In AppDelegate.swift:2import Evergage34func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) ->Bool{56 let evergage = Evergage.sharedInstance()78 // For the DEBUG conditional to work in Swift, you must edit your project/target's Build Settings:9 // Expand "Other Swift Flags" and add "-DDEBUG" under Debug. Do not add to Release!10 #if DEBUG11 evergage.logLevel = EVGLogLevel.warn12 #endif1314 // Recommended to set the authenticated user's ID as soon as known:15 evergage.userId = "theAuthenticatedUserId"1617 // If using Firebase Messaging, be sure to uncomment and do these before starting Evergage:18 // FirebaseApp.configure()19 // Messaging.messaging().delegate = self // Or whatever your delegate is2021 // Start Evergage with your Evergage Configuration:22 evergage.start{(clientConfigurationBuilder)in23 clientConfigurationBuilder.account = "yourEvergageAccount"24 clientConfigurationBuilder.dataset = "yourEvergageDataset"25 clientConfigurationBuilder.usePushNotifications = true // If you want to use Evergage push notification campaigns26}2728 // ... existing code from your app starts here2930 return true31}
User Info
To add information about the user, see userId and setUserAttribute:forName:. In the examples above, setting the userId provides Personalization with the user’s authenticated ID. This could also have been called later, if the user’s ID was not known at startup, and would then be sent with subsequent events. Likewise, if your users belong to an account, see accountId and setAccountAttribute:forName:.
Testing
To easily and codelessly manage test campaigns from the device, the app can add an Personalization URL scheme and define one of the iOS open-url delegate methods, see [Evergage(Swizzling) handleOpenURL:]. For more information on testing your campaigns, see the Testing guide.
Lifecycle
Once startWithClientConfiguration: has been called at app launch, the Personalization client will track user activity, send any applicable events to the Personalization server, and support campaigns in response. Personalization will monitor network availability and store events if necessary, sending them when the network becomes available again.
Tracking Screens
The Personalization client supports automatically tracking UIViewControllers and dynamically mapping and re-mapping them to view actions using the Personalization UI. For more information, see Tracking Guide, UIViewController(Evergage), and EVGScreen.
Tracking Item Views and Activity
Personalization can track how the user views and interacts with articles, blogs and products which are collectively called items. Personalization understands the actions that are possible on these items (View, Comment, Purchase, etc.) and also how they relate to each other (categories, brands, keywords, etc.). For more information, see the Tracking Guide and EVGContext.
Campaigns
Campaigns may be served in response to actions generated by the user interacting with the app:
“In-App Message” campaigns are automatically handled by Personalization.
“Push Notification” campaigns require APNS setup, and optionally Firebase. For more information , see Push Notifications.
Before calling startWithClientConfiguration:, the app should ensure that the user has actually consented to the services. We recommend calling startWithClientConfiguration: from willFinishLaunchingWithOptions:. For examples on how to do this, see the startup examples in the Startup section of this article. Once started, subsequent calls have no effect unless reset has been called due to an environment change.
Parameters
Parameter
Description
configurationBlock
Client-specific Personalization configuration defined via EVGClientConfigurationBuilder
You can call this property from any thread. However, for the check-and-observe pattern, perform the isServerConfigLoaded check and notification observation on the main thread in the same call frame. EVGNotificationServerConfigLoaded is posted synchronously on the main thread, and this pattern avoids a race window between the check and addObserver:.
This property resets to NO on reset or if the SDK is disabled by the server.
Observe that EVGNotificationServerConfigLoaded is notified when the server configuration is loaded.
Availability
1.5.0
Declared In
Evergage.h
User Info Properties
userId
The user’s authenticated ID. Setting this property is critical to correlate the same user’s activity across devices and platforms, and also makes it easier to find a user in Personalization.
When the authenticated ID is null, Personalization identifies the user with a generated anonymous ID. See anonymousId for details, including how activity may be merged.
You may call this method with null to make the user anonymous again. At that point, all new activity will be attributed to the anonymous user, and push notifications can only be sent to the anonymous user (based off the anonymous activity). So after a simple log out, you may wish to continue to call this method with the previously-authenticated user ID, to continue to be able to send push notifications to the authenticated user (based off the authenticated activity) etc. You decide when the user becomes truly anonymous again.
Recommended to set this property:
With the ID, when the user successfully authenticates (logs in).
With the ID, on app launch as soon as possible, if the user is still authenticated, or previously authenticated but needs to log back in again. Personalization does not persist the ID across app restarts.
With nil, when the app decides the user should be anonymous again. This may not be on logout - see note about anonymous activity above. Calling this method with nil will also set accountId to nil.
When a user transitions from anonymous to authenticated (via userId), the previous activity while anonymous will be merged into the authenticated user if Merge Anonymous Users is enabled in the Personalization UI.
This ID should be passed in SmartSearch requests if userId returns nil.
The optional account this user belongs to. Set this property to track which of your accounts users belong to inside the Personalization dataset and account. If account is no longer applicable, it can be set to nil.
The new value of the account attribute, which replaces any existing value. Set nil to clear.
attributeName
The name for the account attribute.
Declared In
Evergage.h
Tracking Non-Screen Actions
globalContext
An EVGContext that is not an EVGScreen for tracking and personalization. To be used when an app can’t use a UIViewController for each screen or page of the app.
An app that can use a UIViewController for each screen or page of the app should instead use the EVGScreen returned by [UIViewController(Evergage) evergageScreen], which automatically cleans up resources and stops timing/tracking on UIViewController disappear.
However, some apps might use a development framework that does not produce an accessible UIViewController per screen, or use a decoupled architecture where referencing individual UIViewControllers can be problematic/undesirable. In those scenarios, apps can choose to instead use this globalContext.
Using the globalContext means losing some cleanup/safety mechanisms the SDK automatically provides, and so you must manually do these operations, as applicable:
Stop accumulating view time on an item.
After calling any of the View APIs, you’ll need to indicate when the item is no longer being viewed by calling the a View API method again, supplying nil for the item parameter viewItem:nil.
After using [EVGContext setCampaignHandler:forTarget:], you’ll need to indicate when the handler should be removed by calling it again, supplying nil for the handler parameter, such as setCampaignHandler:nil forTarget:@"sameTargetUsedEarlier".
If the campaign handler concerns a particular screen or page of the app, it should be removed when the screen or page would be removed. Otherwise, the handler might contain strong references, preventing objects from being deallocated or released, and possibly even trying to handle a campaign which could result in crashes or unexpected behavior from UI-related objects being used beyond their expected lifecycle.
Configures the threshold EVGLogLevel of messages to log. Will only log messages at this level of severity or greater.
1@property EVGLogLevel logLevel
Discussion
Recommended to set only during development in DEBUG builds.
Declared In
Evergage.h
reset
Resets the SDK so startWithClientConfiguration: can be called again with a different dataset, in order to support apps that change environments (production, demo, QA, etc) and want to change Personalization datasets as well.
1- (void)reset
Discussion
Reset will clear settings, unsent actions, held campaigns, test campaign settings, allowDesignConnections, userId, accountId, and unsent attributes.
Reset will keep existing campaign handlers, screen visibility, and items being viewed, since it would potentially be cumbersome/confusing to require once again setting handlers, potentially mocking viewWillAppear or re-navigating to screens, re-setting items being viewed, etc.
Ideally the app will avoid environment churn:
On app launch, if the user must select/confirm the environment before using the app, consider delaying the call to startWithClientConfiguration: to when the environment is chosen and avoid calling reset.
Otherwise, if the user can immediately use the app with the current environment, call startWithClientConfiguration: as normal with the corresponding dataset. And when the environment later changes, and a different Personalization dataset is desired, call reset as demonstrated below.
Recommended Usage:
1// If the app is changing to a different server environment and a different Personalization dataset is desired..23// Reset Personalization iOS SDK just before changing the environment:4Evergage *evergage = [Evergage sharedInstance];5[evergage reset];67// ... app changes server environment89// Start Personalization iOS SDK again with the dataset to use with the negitw environment:10// See Evergage class doc for full example code.11evergage.userId = ...;12[evergage startWithClientConfiguration:...];
User Logout / Consent Revocation:
Call reset when a user logs out or revokes consent. After reset, the SDK remains in a pre-start state: no device identifiers are read, no events are queued or sent until startWithClientConfiguration: is called again. This state protects against capturing events between sessions and ensures no identifiers or unsent events from the departing user persist into the next session.
1// On user logout or consent revocation:2Evergage *evergage = [Evergage sharedInstance];3[evergage reset];45// ... user is on login or consent screen; tracking calls during this window are dropped ...67// When a user is ready and consent has been obtained:8evergage.userId = newUserId;9[evergage startWithClientConfiguration:...];