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 / or personalization in the app to quickly know if a user has made a purchase in the past or recently. Let’s 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 Purchases {width="15/"} button). Add the following code:code language-swift // Update attributes MobileSDK.shared.updateUserAttribute(attributeName: "isPaidUser", attributeValue: "yes")
Get user attributes
Once you have updated a user’s attribute, it is available to other ÃÛ¶¹ÊÓƵ 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 the{width="20/"}
icon in the toolbar at the top right.
-
Additional documentation can be found .
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.
-
Move the Assurance icon to the left.
-
Select Home in the tab bar.
-
To open the Login sheet, select the {width="15/"} button.
{width="300"}
-
To insert a random email and customer id, select the {width="15/"} button .
-
Select Login.
{width="300"}
-
Select Products in the tab bar.
-
Select one product.
-
Select {width="15/"}.
-
Select {width="20/"}.
-
Select {width="15/"}.
{width="300"}
-
Return back to Home screen. You should see that a badge has been added {width="15/"}.
{width="300"}
-
-
In the Assurance UI, you should see a UserProfileUpdate and getUserAttributes events with the updated
profileMap
value.
Next: Use Places