Configuring sending profile attributes
You can configure the AppMetrica SDK to send predefined and custom profile attributes. Predefined attributes are already defined in the SDK and have a special format of sending. To send your own custom attributes, you must add the attribute in the application settings.
This section explains the steps for setting up AppMetrica to send attributes:
Step 1. Adding an attribute in the app settings
- In the AppMetrica interface, go to the app settings from the menu on the left.
- Open the Profile Attributes tab.
- In the Custom attributes section enter the name of the new attribute in the corresponding field.
- Select a variable type from the drop-down list and click Add.
There is a list of all attributes of the app and their status in the Profile attributes settings section. To stop collecting an attribute and remove it from reports, click .
Step 2. Setting up the attribute values sending in AppMetrica SDK
There are examples below of how to send profile attributes.
To send profile attributes, pass to the instance of the UserProfile class required attributes and send this instance using the YandexMetrica.reportUserProfile(UserProfile profile) method. To create profile attributes, use methods of the Attribute class.
// Creating the UserProfile instance. UserProfile userProfile = UserProfile.newBuilder() // Updating predefined attributes. .apply(Attribute.name().withValue("John")) .apply(Attribute.gender().withValue(GenderAttribute.Gender.MALE)) .apply(Attribute.birthDate().withAge(24)) .apply(Attribute.notificationsEnabled().withValue(false)) // Updating custom attributes. .apply(Attribute.customString("string_attribute").withValue("string")) .apply(Attribute.customNumber("number_attribute").withValue(55)) .apply(Attribute.customCounter("counter_attribute").withDelta(1)) .build(); // Setting the ProfileID using the method of the YandexMetrica class. YandexMetrica.setUserProfileID("id"); // Sending the UserProfile instance. YandexMetrica.reportUserProfile(userProfile);
Copied to clipboard
To send profile attributes, pass in the method +reportUserProfile:onFailure: of the YMMYandexMetrica class the following parameters:
userProfile
— The YMMUserProfile instance that contains an array of attribute updates. To create profile attributes, use methods of the YMMProfileAttribute class.onFailure
— The block the error is passed to. If you do not want to track the error, passnil
for this block.
YMMMutableUserProfile *profile = [[YMMMutableUserProfile alloc] init]; // Updating a single user profile attribute. id<YMMCustomCounterAttribute> timeLeftAttribute = [YMMProfileAttribute customCounter:@"time_left"]; [profile apply:[timeLeftAttribute withDelta:-4.42]]; // Updating multiple attributes. [profile applyFromArray:@[ // Updating predefined attributes. [[YMMProfileAttribute name] withValue:@"John"], [[YMMProfileAttribute gender] withValue:YMMGenderTypeMale], [[YMMProfileAttribute birthDate] withAge:24], [[YMMProfileAttribute notificationsEnabled] withValue:NO], // Updating custom attributes. [[YMMProfileAttribute customString:@"born_in"] withValueIfUndefined:@"Moscow"], [[YMMProfileAttribute customString:@"address"] withValueReset], [[YMMProfileAttribute customNumber:@"age"] withValue:24], [[YMMProfileAttribute customCounter:@"logins_count"] withDelta:1], [[YMMProfileAttribute customBool:@"has_premium"] withValue:YES] ]]; // ProfieID is set using the method of the YMMYandexMetrica class. [YMMYandexMetrica >setUserProfileID:@"id"]; // Sending profile attributes. [YMMYandexMetrica reportUserProfile:[profile copy] onFailure:^(NSError *error) { NSLog(@"Error: %@", error); }];
Copied to clipboard
To send profile attributes, pass in the method reportUserProfile(_:onFailure:) of the YMMYandexMetrica class the following parameters:
userProfile
— The YMMUserProfile instance that contains an array of attribute updates. To create profile attributes, use methods of the YMMProfileAttribute class.onFailure
— The block the error is passed to. If you do not want to track the error, passnil
for this block.
let profile = YMMMutableUserProfile() // Updating a single user profile attribute. let timeLeftAttribute: YMMCustomCounterAttribute = YMMProfileAttribute.customCounter("time_left") profile.apply(timeLeftAttribute.withDelta(-4.42)) // Updating multiple attributes. profile.apply(from: [ // Updating predefined attributes. YMMProfileAttribute.name().withValue("John"), YMMProfileAttribute.gender().withValue(YMMGenderType.male), YMMProfileAttribute.birthDate().withAge(24), YMMProfileAttribute.notificationsEnabled().withValue(false), // Updating custom attributes. YMMProfileAttribute.customString("born_in").withValueIfUndefined("Moscow"), YMMProfileAttribute.customString("address").withValueReset(), YMMProfileAttribute.customNumber("age").withValue(24), YMMProfileAttribute.customCounter("logins_count").withDelta(1), YMMProfileAttribute.customBool("has_premium").withValue(true) ]) // ProfieID is set using the method of the YMMYandexMetrica class. YMMYandexMetrica.setUserProfileID("id") // Sending profile attributes. YMMYandexMetrica.report(profile, onFailure: { (error) in print("REPORT ERROR: %@", error.localizedDescription) })
Copied to clipboard