Отправка ECommerce-событий на iOS

В AppMetrica нет возможности сегментировать ECommerce-события на тестовые и не тестовые. Если для отладки покупок вы используете основной API key, то тестовые события будут попадать в общую статистику. Поэтому, чтобы отладить отправку ECommerce-событий, используйте отправку статистики на дополнительный API key с помощью репортера. Подробнее о работе репортера в разделе Примеры использования.

Шаг 1. Создайте тестовое приложение в AppMetrica

Заполните параметры приложения: ссылка в магазине приложений (если приложение еще не опубликовано — оставьте поле пустым), название, категория, часовой пояс для построения отчетов.

Чтобы добавить еще одно приложение, нажмите кнопку Добавить приложение в выпадающем списке в интерфейсе AppMetrica.

Шаг 2. Настройте отправку ECommerce-событий на тестовый API key

Для различных действий пользователя есть соответствующие типы ECommerce-событий. Чтобы создать конкретный тип события, используйте нужный метод класса YMMECommerce.

Ниже приведены примеры отправки конкретных типов событий.

Открытие страницы
// Creating a screen object. 
YMMECommerceScreen *screen = [[YMMECommerceScreen alloc] initWithName:@"ProductCardScreen"
                                                   categoryComponents:@[ @"Акции", @"Красная цена" ]
                                                          searchQuery:@"даниссимо кленовый сироп"
                                                              payload:@{ @"full_screen": @"true" }];// Sending an e-commerce event.
id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:@"Testing API key"];
[reporter reportECommerce:[YMMECommerce showScreenEventWithScreen:screen] onFailure:nil];
Просмотр карточки товара
// Creating a screen object. 
YMMECommerceScreen *screen = [[YMMECommerceScreen alloc] initWithName:@"ProductCardScreen"
                                                   categoryComponents:@[ @"Акции", @"Красная цена" ]
                                                          searchQuery:@"даниссимо кленовый сироп"
                                                              payload:@{ @"full_screen": @"true" }];// Creating an actualPrice object.
YMMECommerceAmount *actualFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"4.53"]];
YMMECommerceAmount *woodActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30570000"]];
YMMECommerceAmount *ironActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.89"]];
YMMECommerceAmount *goldActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.1"]];
YMMECommercePrice *actualPrice = [[YMMECommercePrice alloc] initWithFiat:actualFiat
                                                      internalComponents:@[ woodActualPrice, ironActualPrice, goldActualPrice ]];// Creating an originalPrice object.
YMMECommerceAmount *originalFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"5.78"]];
YMMECommerceAmount *woodOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30590000"]];
YMMECommerceAmount *ironOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.92"]];
YMMECommerceAmount *goldOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.5"]];
YMMECommercePrice *originalPrice = [[YMMECommercePrice alloc] initWithFiat:originalFiat
                                                        internalComponents:@[ woodOriginalPrice, ironOriginalPrice, goldOriginalPrice ]];// Creating a product object.
YMMECommerceProduct *product = [[YMMECommerceProduct alloc] initWithSKU:@"779213"
                                                                   name:@"Продукт творожный «Даниссимо» 5.9%, 130 г."
                                                     categoryComponents:@[ @"Продукты", @"Молочные продукты", @"Йогурты" ]
                                                                payload:@{ @"full_screen" : @"true" }
                                                            actualPrice:actualPrice
                                                          originalPrice:originalPrice
                                                             promoCodes:@[ @"BT79IYX", @"UT5412EP" ]];// Sending an e-commerce event.
id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:@"Testing API key"];
[reporter reportECommerce:[YMMECommerce showProductCardEventWithProduct:product screen:screen] onFailure:nil];
Просмотр страницы товара
// Creating a screen object. 
YMMECommerceScreen *screen = [[YMMECommerceScreen alloc] initWithName:@"ProductCardScreen"
                                                   categoryComponents:@[ @"Акции", @"Красная цена" ]
                                                          searchQuery:@"даниссимо кленовый сироп"
                                                              payload:@{ @"full_screen": @"true" }];// Creating an actualPrice object.
YMMECommerceAmount *actualFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"4.53"]];
YMMECommerceAmount *woodActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30570000"]];
YMMECommerceAmount *ironActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.89"]];
YMMECommerceAmount *goldActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.1"]];
YMMECommercePrice *actualPrice = [[YMMECommercePrice alloc] initWithFiat:actualFiat
                                                      internalComponents:@[ woodActualPrice, ironActualPrice, goldActualPrice ]];// Creating an originalPrice object.
YMMECommerceAmount *originalFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"5.78"]];
YMMECommerceAmount *woodOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30590000"]];
YMMECommerceAmount *ironOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.92"]];
YMMECommerceAmount *goldOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.5"]];
YMMECommercePrice *originalPrice = [[YMMECommercePrice alloc] initWithFiat:originalFiat
                                                        internalComponents:@[ woodOriginalPrice, ironOriginalPrice, goldOriginalPrice ]];// Creating a product object.
YMMECommerceProduct *product = [[YMMECommerceProduct alloc] initWithSKU:@"779213"
                                                                   name:@"Продукт творожный «Даниссимо» 5.9%, 130 г."
                                                     categoryComponents:@[ @"Продукты", @"Молочные продукты", @"Йогурты" ]
                                                                payload:@{ @"full_screen" : @"true" }
                                                            actualPrice:actualPrice
                                                          originalPrice:originalPrice
                                                             promoCodes:@[ @"BT79IYX", @"UT5412EP" ]];// Creating a referrer object.
YMMECommerceReferrer *referrer = [[YMMECommerceReferrer alloc] initWithType:@"button"
                                                                 identifier:@"76890"
                                                                     screen:screen];// Sending an e-commerce event.
id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:@"Testing API key"];
[reporter reportECommerce:[YMMECommerce showProductDetailsEventWithProduct:product referrer:referrer] onFailure:nil];
Добавление или удаление товара из корзины
// Creating a screen object. 
YMMECommerceScreen *screen = [[YMMECommerceScreen alloc] initWithName:@"ProductCardScreen"
                                                   categoryComponents:@[ @"Акции", @"Красная цена" ]
                                                          searchQuery:@"даниссимо кленовый сироп"
                                                              payload:@{ @"full_screen": @"true" }];// Creating an actualPrice object.
YMMECommerceAmount *actualFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"4.53"]];
YMMECommerceAmount *woodActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30570000"]];
YMMECommerceAmount *ironActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.89"]];
YMMECommerceAmount *goldActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.1"]];
YMMECommercePrice *actualPrice = [[YMMECommercePrice alloc] initWithFiat:actualFiat
                                                      internalComponents:@[ woodActualPrice, ironActualPrice, goldActualPrice ]];// Creating an originalPrice object.
YMMECommerceAmount *originalFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"5.78"]];
YMMECommerceAmount *woodOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30590000"]];
YMMECommerceAmount *ironOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.92"]];
YMMECommerceAmount *goldOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.5"]];
YMMECommercePrice *originalPrice = [[YMMECommercePrice alloc] initWithFiat:originalFiat
                                                        internalComponents:@[ woodOriginalPrice, ironOriginalPrice, goldOriginalPrice ]];// Creating a product object.
YMMECommerceProduct *product = [[YMMECommerceProduct alloc] initWithSKU:@"779213"
                                                                   name:@"Продукт творожный «Даниссимо» 5.9%, 130 г."
                                                     categoryComponents:@[ @"Продукты", @"Молочные продукты", @"Йогурты" ]
                                                                payload:@{ @"full_screen" : @"true" }
                                                            actualPrice:actualPrice
                                                          originalPrice:originalPrice
                                                             promoCodes:@[ @"BT79IYX", @"UT5412EP" ]];// Creating a referrer object.
YMMECommerceReferrer *referrer = [[YMMECommerceReferrer alloc] initWithType:@"button"
                                                                 identifier:@"76890"
                                                                     screen:screen];// Creating a cartItem object.
NSDecimalNumber *quantity = [NSDecimalNumber decimalNumberWithString:@"1"];
YMMECommerceCartItem *addedItems = [[YMMECommerceCartItem alloc] initWithProduct:product
                                                                      referrer:referrer
                                                                      quantity:quantity
                                                                       revenue:actualPrice];// Sending an e-commerce event.
id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:@"Testing API key"];
[reporter reportECommerce:[YMMECommerce addCartItemEvent:addedItems] onFailure:nil];
// Or:
[YMMYandexMetricareportECommerce:[YMMECommerce removeCartItemEventWithItem:addedItems] onFailure:nil];
Начало оформления и завершение покупки
// Creating a screen object. 
YMMECommerceScreen *screen = [[YMMECommerceScreen alloc] initWithName:@"ProductCardScreen"
                                                   categoryComponents:@[ @"Акции", @"Красная цена" ]
                                                          searchQuery:@"даниссимо кленовый сироп"
                                                              payload:@{ @"full_screen": @"true" }];// Creating an actualPrice object.
YMMECommerceAmount *actualFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"4.53"]];
YMMECommerceAmount *woodActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30570000"]];
YMMECommerceAmount *ironActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.89"]];
YMMECommerceAmount *goldActualPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.1"]];
YMMECommercePrice *actualPrice = [[YMMECommercePrice alloc] initWithFiat:actualFiat
                                                      internalComponents:@[ woodActualPrice, ironActualPrice, goldActualPrice ]];// Creating an originalPrice object.
YMMECommerceAmount *originalFiat =
        [[YMMECommerceAmount alloc] initWithUnit:@"USD" value:[NSDecimalNumber decimalNumberWithString:@"5.78"]];
YMMECommerceAmount *woodOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"wood" value:[NSDecimalNumber decimalNumberWithString:@"30590000"]];
YMMECommerceAmount *ironOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"iron" value:[NSDecimalNumber decimalNumberWithString:@"26.92"]];
YMMECommerceAmount *goldOriginalPrice =
        [[YMMECommerceAmount alloc] initWithUnit:@"gold" value:[NSDecimalNumber decimalNumberWithString:@"5.5"]];
YMMECommercePrice *originalPrice = [[YMMECommercePrice alloc] initWithFiat:originalFiat
                                                        internalComponents:@[ woodOriginalPrice, ironOriginalPrice, goldOriginalPrice ]];// Creating a product object.
YMMECommerceProduct *product = [[YMMECommerceProduct alloc] initWithSKU:@"779213"
                                                                   name:@"Продукт творожный «Даниссимо» 5.9%, 130 г."
                                                     categoryComponents:@[ @"Продукты", @"Молочные продукты", @"Йогурты" ]
                                                                payload:@{ @"full_screen" : @"true" }
                                                            actualPrice:actualPrice
                                                          originalPrice:originalPrice
                                                             promoCodes:@[ @"BT79IYX", @"UT5412EP" ]];// Creating a referrer object.
YMMECommerceReferrer *referrer = [[YMMECommerceReferrer alloc] initWithType:@"button"
                                                                 identifier:@"76890"
                                                                     screen:screen];// Creating a cartItem object.
NSDecimalNumber *quantity = [NSDecimalNumber decimalNumberWithString:@"1"];
YMMECommerceCartItem *addedItems = [[YMMECommerceCartItem alloc] initWithProduct:product
                                                                      referrer:referrer
                                                                      quantity:quantity
                                                                       revenue:actualPrice];
// Creating an order object.
YMMECommerceOrder *order = [[YMMECommerceOrder alloc] initWithIdentifier:@"88528768"
                                                               cartItems:@[ addedItems ]
                                                                 payload:@{ @"black_friday" : @"true"}];
// Sending an e-commerce event.
id<YMMYandexMetricaReporting> reporter = [YMMYandexMetrica reporterForApiKey:@"Testing API key"];
[reporter reportECommerce:[YMMECommerce beginCheckoutEventWithOrder:order] onFailure:nil];
[YMMYandexMetricareportECommerce:[YMMECommerce purchaseEventWithOrder:order] onFailure:nil];
Открытие страницы
// Creating a screen object. 
let screen = YMMECommerceScreen(
        name: "ProductCardScreen",
        categoryComponents: ["Акции", "Красная цена"],
        searchQuery: "даниссимо кленовый сироп",
        payload: ["full_screen": "true"]
)// Sending an e-commerce event.
YMMYandexMetrica.report(eCommerce: .showScreenEvent(screen: screen), onFailure: nil)
Просмотр карточки товара
// Creating a screen object. 
let screen = YMMECommerceScreen(
        name: "ProductCardScreen",
        categoryComponents: ["Акции", "Красная цена"],
        searchQuery: "даниссимо кленовый сироп",
        payload: ["full_screen": "true"]
)// Creating an actualPrice object.
let actualPrice = YMMECommercePrice(
        fiat: .init(unit: "USD", value: .init(string: "4.53")),
        internalComponents: [
            .init(unit: "wood", value: .init(string: "30570000")),
            .init(unit: "iron", value: .init(string: "26.89")),
            .init(unit: "gold", value: .init(string: "5.1")),
        ]
)// Creating a product object.
let product = YMMECommerceProduct(
        sku: "779213",
        name: "Продукт творожный «Даниссимо» 5.9%, 130 г.",
        categoryComponents: ["Продукты", "Молочные продукты", "Йогурты"],
        payload: ["full_screen": "true"],
        actualPrice: actualPrice,
        originalPrice: .init(
                fiat: .init(unit: "USD", value: .init(string: "5.78")),
                internalComponents: [
                    .init(unit: "wood", value: .init(string: "30590000")),
                    .init(unit: "iron", value: .init(string: "26.92")),
                    .init(unit: "gold", value: .init(string: "5.5")),
                ]
        ),
        promoCodes: ["BT79IYX", "UT5412EP"]
)// Sending an e-commerce event.
YMMYandexMetrica.report(eCommerce: .showProductCardEvent(product: product, screen: screen), onFailure: nil)
Просмотр страницы товара
// Creating a screen object. 
let screen = YMMECommerceScreen(
        name: "ProductCardScreen",
        categoryComponents: ["Акции", "Красная цена"],
        searchQuery: "даниссимо кленовый сироп",
        payload: ["full_screen": "true"]
)// Creating an actualPrice object.
let actualPrice = YMMECommercePrice(
        fiat: .init(unit: "USD", value: .init(string: "4.53")),
        internalComponents: [
            .init(unit: "wood", value: .init(string: "30570000")),
            .init(unit: "iron", value: .init(string: "26.89")),
            .init(unit: "gold", value: .init(string: "5.1")),
        ]
)// Creating a product object.
let product = YMMECommerceProduct(
        sku: "779213",
        name: "Продукт творожный «Даниссимо» 5.9%, 130 г.",
        categoryComponents: ["Продукты", "Молочные продукты", "Йогурты"],
        payload: ["full_screen": "true"],
        actualPrice: actualPrice,
        originalPrice: .init(
                fiat: .init(unit: "USD", value: .init(string: "5.78")),
                internalComponents: [
                    .init(unit: "wood", value: .init(string: "30590000")),
                    .init(unit: "iron", value: .init(string: "26.92")),
                    .init(unit: "gold", value: .init(string: "5.5")),
                ]
        ),
        promoCodes: ["BT79IYX", "UT5412EP"]
)// Creating a referrer object.
let referrer = YMMECommerceReferrer(type: "button", identifier: "76890", screen: screen)// Sending an e-commerce event.
YMMYandexMetrica.report(eCommerce: .showProductDetailsEvent(product: product, referrer: referrer), onFailure: nil)
Добавление или удаление товара из корзины
// Creating a screen object. 
let screen = YMMECommerceScreen(
        name: "ProductCardScreen",
        categoryComponents: ["Акции", "Красная цена"],
        searchQuery: "даниссимо кленовый сироп",
        payload: ["full_screen": "true"]
)// Creating an actualPrice object.
let actualPrice = YMMECommercePrice(
        fiat: .init(unit: "USD", value: .init(string: "4.53")),
        internalComponents: [
            .init(unit: "wood", value: .init(string: "30570000")),
            .init(unit: "iron", value: .init(string: "26.89")),
            .init(unit: "gold", value: .init(string: "5.1")),
        ]
)// Creating a product object.
let product = YMMECommerceProduct(
        sku: "779213",
        name: "Продукт творожный «Даниссимо» 5.9%, 130 г.",
        categoryComponents: ["Продукты", "Молочные продукты", "Йогурты"],
        payload: ["full_screen": "true"],
        actualPrice: actualPrice,
        originalPrice: .init(
                fiat: .init(unit: "USD", value: .init(string: "5.78")),
                internalComponents: [
                    .init(unit: "wood", value: .init(string: "30590000")),
                    .init(unit: "iron", value: .init(string: "26.92")),
                    .init(unit: "gold", value: .init(string: "5.5")),
                ]
        ),
        promoCodes: ["BT79IYX", "UT5412EP"]
)// Creating a referrer object.
let referrer = YMMECommerceReferrer(type: "button", identifier: "76890", screen: screen)// Creating a cartItem object.
let addedItems = YMMECommerceCartItem(
        product: product,
        referrer: referrer,
        quantity: .init(string: "1"),
        revenue: actualPrice
)// Sending an e-commerce event.
YMMYandexMetrica.report(eCommerce: .addCartItemEvent(cartItem: addedItems), onFailure: nil)
// Or:
YMMYandexMetrica.report(eCommerce: .removeCartItemEvent(cartItem: addedItems), onFailure: nil)
Начало оформления и завершение покупки
// Creating a screen object. 
let screen = YMMECommerceScreen(
        name: "ProductCardScreen",
        categoryComponents: ["Акции", "Красная цена"],
        searchQuery: "даниссимо кленовый сироп",
        payload: ["full_screen": "true"]
)// Creating an actualPrice object.
let actualPrice = YMMECommercePrice(
        fiat: .init(unit: "USD", value: .init(string: "4.53")),
        internalComponents: [
            .init(unit: "wood", value: .init(string: "30570000")),
            .init(unit: "iron", value: .init(string: "26.89")),
            .init(unit: "gold", value: .init(string: "5.1")),
        ]
)// Creating a product object.
let product = YMMECommerceProduct(
        sku: "779213",
        name: "Продукт творожный «Даниссимо» 5.9%, 130 г.",
        categoryComponents: ["Продукты", "Молочные продукты", "Йогурты"],
        payload: ["full_screen": "true"],
        actualPrice: actualPrice,
        originalPrice: .init(
                fiat: .init(unit: "USD", value: .init(string: "5.78")),
                internalComponents: [
                    .init(unit: "wood", value: .init(string: "30590000")),
                    .init(unit: "iron", value: .init(string: "26.92")),
                    .init(unit: "gold", value: .init(string: "5.5")),
                ]
        ),
        promoCodes: ["BT79IYX", "UT5412EP"]
)// Creating a referrer object.
let referrer = YMMECommerceReferrer(type: "button", identifier: "76890", screen: screen)// Creating a cartItem object.
let addedItems = YMMECommerceCartItem(
        product: product,
        referrer: referrer,
        quantity: .init(string: "1"),
        revenue: actualPrice
)
// Creating an order object.
let order = YMMECommerceOrder(
        identifier: "88528768",
        cartItems: [addedItems],
        payload: ["black_friday": "true"]
)
// Sending an e-commerce event.
YMMYandexMetrica.report(eCommerce: .beginCheckoutEvent(order:order), onFailure: nil)
YMMYandexMetrica.report(eCommerce: .purchaseEvent(order: order), onFailure: nil)

Шаг 3. Проверьте отчет тестового приложения

Совершите тестовые покупки в приложении. Через некоторое время в интерфейсе AppMetrica проверьте отчет "Анализ покупок". Убедитесь, что ECommerce-события отображаются в отчете.

Подробнее об отчете в разделе Анализ покупок.

Шаг 4. Настройте отправку ECommerce на основной API Key

После успешного тестирования настройте отправку ECommerce-событий на основной API key.

Чтобы отправить объект YMMECommerce на основной API key, используйте метода +reportECommerce:onFailure: класса YMMYandexMetrica.

// ...
// Sending an e-commerce event.
[YMMYandexMetricareportECommerce:[YMMECommerce showScreenEventWithScreen:screen] onFailure:nil];
// ...
// Sending an e-commerce event.
YMMYandexMetrica.report(eCommerce: .showScreenEvent(screen: screen), onFailure: nil)

Если вы не нашли ответ на свой вопрос, то вы можете задать его через форму обратной связи. Пожалуйста, опишите возникшую проблему как можно подробнее. Если возможно, приложите скриншот.

Написать в службу поддержки