Collect profile data
Learn how to collect profile data in a mobile app.
You can use the Profile extension to store attributes about your user on the client. This information can be used later to target and personalize messages during online or offline scenarios, without having to connect to a server for optimal performance.
The Profile extension manages the Client-Side Operation Profile (CSOP), provides a way to react to APIs, updates user profile attributes, and shares the user profile attributes with the rest of the system as a generated event.
The Profile data is used by other extensions to perform profile-related actions. An example is the Rules Engine extension that consumes the profile data and runs rules based on the profile data. Learn more about the in the documentation
Prerequisites
- Successfully built and run app with SDKs installed and configured.
Learning objectives
In this lesson, you will:
- Set or update user attributes.
- Retrieve user attributes.
Set and update user attributes
It would be helpful for targeting and personalization in the app to know quickly if a user has made a purchase in the past or recently. Let鈥檚 set that up in the Luma app.
-
Navigate to Luma > Luma > Utils > MobileSDK in the Xcode Project navigator and find the
func updateUserAttribute(attributeName: String, attributeValue: String)
function. Add the following code:code language-swift // Create a profile map, add attributes to the map and update profile using the map var profileMap = [String: Any]() profileMap[attributeName] = attributeValue UserProfile.updateUserAttributes(attributeDict: profileMap)
This code:
-
Sets up an empty dictionary named
profileMap
. -
Adds an element to the dictionary using
attributeName
(for exampleisPaidUser
), andattributeValue
(for exampleyes
). -
Uses the
profileMap
dictionary as a value to theattributeDict
parameter of the API call.
-
-
Navigate to Luma > Luma > Views > Products > ProductView in the Xcode Project navigator and find the call to
updateUserAttributes
(within the code for the Purchasescode language-swift // Update attributes MobileSDK.shared.updateUserAttribute(attributeName: "isPaidUser", attributeValue: "yes")
-
Navigate to Android
func updateUserAttribute(attributeName: String, attributeValue: String)
function. Add the following code:code language-kotlin // Create a profile map, add attributes to the map and update profile using the map val profileMap = mapOf(attributeName to attributeValue) UserProfile.updateUserAttributes(profileMap)
This code:
-
Sets up an empty map named
profileMap
. -
Adds an element to the map using
attributeName
(for exampleisPaidUser
), andattributeValue
(for exampleyes
). -
Uses the
profileMap
map as a value to theattributeDict
parameter of the API call.
-
-
Navigate to Android
updateUserAttributes
(within the code for the Purchasescode language-kotlin // Update attributes MobileSDK.shared.updateUserAttribute("isPaidUser", "yes")
Get user attributes
Once you have updated a user鈥檚 attribute, it is available to other 51黑料不打烊 SDKs but you can also retrieve attributes explicitly, to let your app behave the way you want.
-
Navigate to Luma > Luma > Views > General > HomeView in the Xcode Project navigator and find the
.onAppear
modifier. Add the following code:code language-swift // Get attributes UserProfile.getUserAttributes(attributeNames: ["isPaidUser"]) { attributes, error in if attributes?.count ?? 0 > 0 { if attributes?["isPaidUser"] as? String == "yes" { showBadgeForUser = true } else { showBadgeForUser = false } } }
This code:
-
Calls the API with the
isPaidUser
attribute name as single element in theattributeNames
array. -
Then checks for the value of the
isPaidUser
attribute and whenyes
, places a badge on theicon in the toolbar at the top right.
-
-
Navigate to Android
.onAppear
modifier. Add the following code:code language-kotlin // Get attributes UserProfile.getUserAttributes(listOf("isPaidUser")) { attributes -> showBadgeForUser = attributes?.get("isPaidUser") == "yes" }
This code:
-
Calls the API with the
isPaidUser
attribute name as single element in theattributeNames
array. -
Then checks for the value of the
isPaidUser
attribute. Whenyes
, the code replaces the person icon with a badge on theicon in the toolbar at the top right.
-
See the for more information.
Validate with Assurance
- Review the setup instructions section to connect your simulator or device to Assurance.
- Run the app to log in and interact with a product.
-
Select Home in the tab bar.
-
Move the Assurance icon to the left.
-
To open the Login sheet, select the
-
To insert a random email and customer id, select the
-
Select Login.
-
Select Products in the tab bar.
-
Select one product.
-
Select
-
Select
-
Select
-
Return back to Home screen. You should see that a badge has been added
-
Select Home in the tab bar.
-
Move the Assurance icon to the left.
-
To open the Login sheet, select the
-
To insert a random email and customer id, select the
-
Select Login.
-
Select Products in the tab bar.
-
Select one product.
-
Select
-
Select
-
Select
-
Return back to Home screen. You should see that the person icon is updated.
In the Assurance UI, you should see a UserProfileUpdate and getUserAttributes events with the updated profileMap
value.
Next: Use Places