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
profileMapdictionary as a value to theattributeDictparameter 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 Purchases button). Add the following code:code language-swift // Update attributes MobileSDK.shared.updateUserAttribute(attributeName: "isPaidUser", attributeValue: "yes")
-
Navigate to Android
> app > kotlin+java > com.adobe.luma.tutorial.android > models > MobileSDK in the Android Studio navigator and find thefunc 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
profileMapmap as a value to theattributeDictparameter of the API call.
-
-
Navigate to Android
> app > kotlin+java > com.adobe.luma.tutorial.android > views > ProductView.kt and find the call toupdateUserAttributes(within the code for the Purchases button). Add the following code:code 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
.onAppearmodifier. 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
isPaidUserattribute name as single element in theattributeNamesarray. - Then checks for the value of the
isPaidUserattribute and whenyes, places a badge on the icon in the toolbar at the top right.
- Calls the API with the
-
Navigate to Android
> app > kotlin+java > com.adobe.luma.tutorial.androi > views > HomeView.kt in the Android Studio project navigator and find the.onAppearmodifier. 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
isPaidUserattribute name as single element in theattributeNamesarray. - Then checks for the value of the
isPaidUserattribute. Whenyes, the code replaces the person icon with a badge icon in the toolbar at the top right.
- Calls the API with the
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
button. {width="300"}
-
To insert a random email and customer id, select the A | button .
-
Select Login.
{width="300"}
-
Select Products in the tab bar.
-
Select one product.
-
Select
. -
Select
. -
Select
. {width="300"}
-
Return back to Home screen. You should see that a badge has been added
. {width="300"}
-
Select Home in the tab bar.
-
Move the Assurance icon to the left.
-
To open the Login sheet, select the
button. {width="300"}
-
To insert a random email and customer id, select Generate Random Email .
-
Select Login.
{width="300"}
-
Select Products in the tab bar.
-
Select one product.
-
Select
-
Select
. -
Select
. {width="300"}
-
Return back to Home screen. You should see that the person icon is updated.
{width="300"}
In the Assurance UI, you should see a UserProfileUpdate and getUserAttributes events with the updated profileMap value.
Next: Use Places