Настройка отправки атрибутов профилей
В AppMetrica SDK можно настроить отправку предопределенных и собственных атрибутов профиля. Предопределенные атрибуты поддержаны заранее и имеют установленный формат отправки. Для отправки собственных атрибутов необходимо добавить атрибут в настройках приложения.
Ниже описаны этапы настройки отправки атрибутов:
Шаг 1. Добавьте атрибут в настройках приложения
- В интерфейсе AppMetrica перейдите в настройки приложения из меню слева.
- Откройте вкладку Атрибуты профилей.
- В разделе Собственные атрибуты введите название нового атрибута в соответствующее поле.
- Выберите тип переменной из выпадающего списка и нажмите Добавить.
В настройках Атрибуты профилей отображается список всех атрибутов и их статус. Чтобы прекратить сбор атрибута и убрать его из отчетов, нажмите кнопку .
Шаг 2. Настройте отправку значений атрибутов в AppMetrica SDK
Ниже представлены примеры отправки атрибутов профиля.
Чтобы отправить атрибуты профиля, передайте в объект UserProfile необходимые атрибуты и отправьте этот объект с помощью метода YandexMetrica.reportUserProfile(UserProfile profile). Атрибуты профиля создаются с помощью методов класса Attribute.
// 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);
Скопировано
Чтобы отправить атрибуты профиля, передайте в метод +reportUserProfile:onFailure: класса YMMYandexMetrica следующие параметры:
userProfile
— объект YMMUserProfile, который содержит массив обновлений атрибутов. Атрибуты профиля создаются с помощью методов класса YMMProfileAttribute.onFailure
— блок, в который передается ошибка. Если вы не хотите отслеживать ошибку, то передайте в качестве блока значениеnil
.
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); }];
Скопировано
Чтобы отправить атрибуты профиля, передайте в метод reportUserProfile(_:onFailure:) класса YMMYandexMetrica следующие параметры:
userProfile
— объект YMMUserProfile, который содержит массив обновлений атрибутов. Атрибуты профиля создаются с помощью методов класса YMMProfileAttribute.onFailure
— блок, в который передается ошибка. Если вы не хотите отслеживать ошибку, то передайте в качестве блока значениеnil
.
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) })
Скопировано