Sending ECommerce events on iOS
Step 1. Create a test app in AppMetrica
Specify the app parameters: link in the app store (if the app hasn't been published yet, leave the field empty), name, category, and time zone for generating reports.
To add another app, click Add app in the drop-down list in the AppMetrica interface.
Step 2. Configure sending ECommerce events to the test API key
For different user actions, there are appropriate types of ECommerce events. To create a specific event type, use the appropriate YMMECommerce class method.
The examples below show how to send specific types of events (Objective-C):
// 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];
Copied to clipboard
// 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];
Copied to clipboard
// 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: [YMMYandexMetrica reportECommerce:[YMMECommerce removeCartItemEventWithItem:addedItems] onFailure:nil];
Copied to clipboard
// 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]; [YMMYandexMetrica reportECommerce:[YMMECommerce purchaseEventWithOrder:order] onFailure:nil];
Copied to clipboard
For different user actions, there are appropriate types of ECommerce events. To create a specific event type, use the appropriate YMMECommerce class method.
The examples below show how to send specific types of events (Swift):
// 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)
Copied to clipboard
// 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)
Copied to clipboard
// 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)
Copied to clipboard
// 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)
Copied to clipboard
Step 3. Check the test application's report
Make in-app test purchases. After a while, check the “Purchase analysis” report in the AppMetrica interface. Make sure that the report shows ECommerce events.
For more information about the report, see Purchase analysis.
Step 4. Configure sending ECommerce to the main API Key
After successful testing, configure sending ECommerce events to the main API key.
To send the YMMECommerce instance to the main API key, use the +reportECommerce:onFailure: method of the YMMYandexMetrica class.
... // Sending an e-commerce event. [YMMYandexMetrica reportECommerce:[YMMECommerce showScreenEventWithScreen:screen] onFailure:nil];
Copied to clipboard
... // Sending an e-commerce event. YMMYandexMetrica.report(eCommerce: .showScreenEvent(screen: screen), onFailure: nil)
Copied to clipboard