Sending ECommerce events on Android
For more information about reporters, see Usage examples.
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 ECommerceEvent
class method.
The examples below show how to send specific types of events (Java):
Map<String, String> payload = new HashMap<>();
payload.put("configuration", "landscape");
payload.put("full_screen", "true");
// Creating a screen object.
ECommerceScreen screen = new ECommerceScreen()
.setCategoriesPath(Arrays.asList( // Optional.
"Акции",
"Красная цена"
))
.setName("ProductCardActivity") // Optional.
.setSearchQuery("даниссимо кленовый сироп") // Optional.
.setPayload(payload); // Optional.
ECommerceEvent showScreenEvent = ECommerceEvent.showScreenEvent(screen);
// Sending an e-commerce event.
YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(showScreenEvent);
Map<String, String> payload = new HashMap<>(); payload.put("configuration", "landscape"); payload.put("full_screen", "true"); // Creating a screen object. ECommerceScreen screen = new ECommerceScreen() .setCategoriesPath(Arrays.asList( // Optional. "Акции", "Красная цена" )) .setName("ProductCardActivity") // Optional. .setSearchQuery("даниссимо кленовый сироп") // Optional. .setPayload(payload); // Optional. // Creating an actualPrice object. ECommercePrice actualPrice = new ECommercePrice(new ECommerceAmount(4.53, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_570_000, "wood"), new ECommerceAmount(26.89, "iron"), new ECommerceAmount(new BigDecimal(5.1), "gold") )); // Creating an originalPrice object. ECommercePrice originalPrice = new ECommercePrice(new ECommerceAmount(5.78, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_590_000, "wood"), new ECommerceAmount(26.92, "iron"), new ECommerceAmount(new BigDecimal(5.5), "gold") )); // Creating a product object. ECommerceProduct product = new ECommerceProduct("779213") .setActualPrice(actualPrice) // Optional. .setPromocodes(Arrays.asList("BT79IYX", "UT5412EP")) // Optional. .setPayload(payload) // Optional. .setOriginalPrice(originalPrice) // Optional. .setName("Продукт творожный «Даниссимо» 5.9%, 130 г.") // Optional. .setCategoriesPath(Arrays.asList("Продукты", "Молочные продукты", "Йогурты")); // Optional. ECommerceEvent showProductCardEvent = ECommerceEvent.showProductCardEvent(product, screen); // Sending an e-commerce event. YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(showProductCardEvent);
Copied to clipboard
Map<String, String> payload = new HashMap<>(); payload.put("configuration", "landscape"); payload.put("full_screen", "true"); // Creating a screen object. ECommerceScreen screen = new ECommerceScreen() .setCategoriesPath(Arrays.asList( // Optional. "Акции", "Красная цена" )) .setName("ProductCardActivity") // Optional. .setSearchQuery("даниссимо кленовый сироп") // Optional. .setPayload(payload); // Optional. // Creating an actualPrice object. ECommercePrice actualPrice = new ECommercePrice(new ECommerceAmount(4.53, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_570_000, "wood"), new ECommerceAmount(26.89, "iron"), new ECommerceAmount(new BigDecimal(5.1), "gold") )); // Creating an originalPrice object. ECommercePrice originalPrice = new ECommercePrice(new ECommerceAmount(5.78, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_590_000, "wood"), new ECommerceAmount(26.92, "iron"), new ECommerceAmount(new BigDecimal(5.5), "gold") )); // Creating a product object. ECommerceProduct product = new ECommerceProduct("779213") .setActualPrice(actualPrice) // Optional. .setPromocodes(Arrays.asList("BT79IYX", "UT5412EP")) // Optional. .setPayload(payload) // Optional. .setOriginalPrice(originalPrice) // Optional. .setName("Продукт творожный «Даниссимо» 5.9%, 130 г.") // Optional. .setCategoriesPath(Arrays.asList("Продукты", "Молочные продукты", "Йогурты")); // Optional. // Creating a referrer object. ECommerceReferrer referrer = new ECommerceReferrer() .setType("button") // Optional. .setIdentifier("76890") // Optional. .setScreen(screen); // Optional. ECommerceEvent showProductDetailsEvent = ECommerceEvent.showProductDetailsEvent(product, referrer); // Referrer is optional — can be null. // Sending an e-commerce event. YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(showProductDetailsEvent);
Copied to clipboard
Map<String, String> payload = new HashMap<>(); payload.put("configuration", "landscape"); payload.put("full_screen", "true"); // Creating a screen object. ECommerceScreen screen = new ECommerceScreen() .setCategoriesPath(Arrays.asList( // Optional. "Акции", "Красная цена" )) .setName("ProductCardActivity") // Optional. .setSearchQuery("даниссимо кленовый сироп") // Optional. .setPayload(payload); // Optional. // Creating an actualPrice object. ECommercePrice actualPrice = new ECommercePrice(new ECommerceAmount(4.53, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_570_000, "wood"), new ECommerceAmount(26.89, "iron"), new ECommerceAmount(new BigDecimal(5.1), "gold") )); // Creating an originalPrice object. ECommercePrice originalPrice = new ECommercePrice(new ECommerceAmount(5.78, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_590_000, "wood"), new ECommerceAmount(26.92, "iron"), new ECommerceAmount(new BigDecimal(5.5), "gold") )); // Creating a product object. ECommerceProduct product = new ECommerceProduct("779213") .setActualPrice(actualPrice) // Optional. .setPromocodes(Arrays.asList("BT79IYX", "UT5412EP")) // Optional. .setPayload(payload) // Optional. .setOriginalPrice(originalPrice) // Optional. .setName("Продукт творожный «Даниссимо» 5.9%, 130 г.") // Optional. .setCategoriesPath(Arrays.asList("Продукты", "Молочные продукты", "Йогурты")); // Optional. // Creating a referrer object. ECommerceReferrer referrer = new ECommerceReferrer() .setType("button") // Optional. .setIdentifier("76890") // Optional. .setScreen(screen); // Optional. // Creating a cartItem object. ECommerceCartItem addedItems1 = new ECommerceCartItem(product, actualPrice, 1.0) .setReferrer(referrer); // Optional. ECommerceEvent addCartItemEvent = ECommerceEvent.addCartItemEvent(addedItems1); // Sending an e-commerce event. YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(addCartItemEvent); ECommerceEvent removeCartItemEvent = ECommerceEvent.removeCartItemEvent(addedItems1); // Sending an e-commerce event. YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(removeCartItemEvent);
Copied to clipboard
Map<String, String> payload = new HashMap<>(); payload.put("configuration", "landscape"); payload.put("full_screen", "true"); // Creating a screen object. ECommerceScreen screen = new ECommerceScreen() .setCategoriesPath(Arrays.asList( // Optional. "Акции", "Красная цена" )) .setName("ProductCardActivity") // Optional. .setSearchQuery("даниссимо кленовый сироп") // Optional. .setPayload(payload); // Optional. // Creating an actualPrice object. ECommercePrice actualPrice = new ECommercePrice(new ECommerceAmount(4.53, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_570_000, "wood"), new ECommerceAmount(26.89, "iron"), new ECommerceAmount(new BigDecimal(5.1), "gold") )); // Creating an originalPrice object. ECommercePrice originalPrice = new ECommercePrice(new ECommerceAmount(5.78, "USD")) .setInternalComponents(Arrays.asList( // Optional. new ECommerceAmount(30_590_000, "wood"), new ECommerceAmount(26.92, "iron"), new ECommerceAmount(new BigDecimal(5.5), "gold") )); // Creating a product object. ECommerceProduct product = new ECommerceProduct("779213") .setActualPrice(actualPrice) // Optional. .setPromocodes(Arrays.asList("BT79IYX", "UT5412EP")) // Optional. .setPayload(payload) // Optional. .setOriginalPrice(originalPrice) // Optional. .setName("Продукт творожный «Даниссимо» 5.9%, 130 г.") // Optional. .setCategoriesPath(Arrays.asList("Продукты", "Молочные продукты", "Йогурты")); // Optional. // Creating a referrer object. ECommerceReferrer referrer = new ECommerceReferrer() .setType("button") // Optional. .setIdentifier("76890") // Optional. .setScreen(screen); // Optional. // Creating a cartItem object. ECommerceCartItem addedItems1 = new ECommerceCartItem(product, actualPrice, 1.0) .setReferrer(referrer); // Optional. // Creating an order object. ECommerceOrder order = new ECommerceOrder("88528768", Arrays.asList(addedItems1)) .setPayload(payload); // Optional. ECommerceEvent beginCheckoutEvent = ECommerceEvent.beginCheckoutEvent(order); // Sending an e-commerce event. YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(beginCheckoutEvent); ECommerceEvent purchaseEvent = ECommerceEvent.purchaseEvent(order); // Sending an e-commerce event. YandexMetrica.getReporter(getApplicationContext(), "Testing API key").reportECommerce(purchaseEvent);
Copied to clipboard
For different user actions, there are appropriate types of ECommerce events. To create a specific event type, use the appropriate ECommerceEvent
class method.
The examples below show how to send specific types of events (Kotlin):
val payload = mapOf("configuration" to "landscape", "full_screen" to "true")
// Creating a screen object.
val screen = ECommerceScreen().apply {
categoriesPath = listOf("Акции", "Красная цена") // Optional.
name = "ProductCardActivity" // Optional.
searchQuery = "даниссимо кленовый сироп" // Optional.
this.payload = payload // Optional.
}
val showScreenEvent = ECommerceEvent.showScreenEvent(screen)
// Sending an e-commerce event.
YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(showScreenEvent)
val payload = mapOf("configuration" to "landscape", "full_screen" to "true") // Creating a screen object. val screen = ECommerceScreen().apply { categoriesPath = listOf("Акции", "Красная цена") // Optional. name = "ProductCardActivity" // Optional. searchQuery = "даниссимо кленовый сироп" // Optional. this.payload = payload // Optional. } // Creating an actualPrice object. val actualPrice = ECommercePrice(ECommerceAmount(4.53, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30570000, "wood"), ECommerceAmount(26.89, "iron"), ECommerceAmount(BigDecimal(5.1), "gold") ) } // Creating an originalPrice object. val originalPrice = ECommercePrice(ECommerceAmount(5.78, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30590000, "wood"), ECommerceAmount(26.92, "iron"), ECommerceAmount(BigDecimal(5.5), "gold") ) } // Creating a product object. val product = ECommerceProduct("779213") .apply { this.actualPrice = actualPrice // Optional. this.originalPrice = originalPrice // Optional. promocodes = listOf("BT79IYX", "UT5412EP") // Optional. name = "Продукт творожный «Даниссимо» 5.9%, 130 г." // Optional. categoriesPath = listOf("Продукты", "Молочные продукты", "Йогурты") // Optional. this.payload = payload // Optional. } val showProductCardEvent = ECommerceEvent.showProductCardEvent(product, screen) // Sending an e-commerce event. YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(showProductCardEvent)
Copied to clipboard
val payload = mapOf("configuration" to "landscape", "full_screen" to "true") // Creating a screen object. val screen = ECommerceScreen().apply { categoriesPath = listOf("Акции", "Красная цена") // Optional. name = "ProductCardActivity" // Optional. searchQuery = "даниссимо кленовый сироп" // Optional. this.payload = payload // Optional. } // Creating an actualPrice object. val actualPrice = ECommercePrice(ECommerceAmount(4.53, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30570000, "wood"), ECommerceAmount(26.89, "iron"), ECommerceAmount(BigDecimal(5.1), "gold") ) } // Creating an originalPrice object. val originalPrice = ECommercePrice(ECommerceAmount(5.78, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30590000, "wood"), ECommerceAmount(26.92, "iron"), ECommerceAmount(BigDecimal(5.5), "gold") ) } // Creating a product object. val product = ECommerceProduct("779213") .apply { this.actualPrice = actualPrice // Optional. this.originalPrice = originalPrice // Optional. promocodes = listOf("BT79IYX", "UT5412EP") // Optional. name = "Продукт творожный «Даниссимо» 5.9%, 130 г." // Optional. categoriesPath = listOf("Продукты", "Молочные продукты", "Йогурты") // Optional. this.payload = payload // Optional. } // Creating a referrer object. val referrer = ECommerceReferrer() .apply { type = "button" // Optional. identifier = "76890" // Optional. this.screen = screen // Optional. } val showProductDetailsEvent = ECommerceEvent.showProductDetailsEvent(product, referrer) // Referrer is optional - can be null. // Sending an e-commerce event. YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(showProductDetailsEvent)
Copied to clipboard
val payload = mapOf("configuration" to "landscape", "full_screen" to "true") // Creating a screen object. val screen = ECommerceScreen().apply { categoriesPath = listOf("Акции", "Красная цена") // Optional. name = "ProductCardActivity" // Optional. searchQuery = "даниссимо кленовый сироп" // Optional. this.payload = payload // Optional. } // Creating an actualPrice object. val actualPrice = ECommercePrice(ECommerceAmount(4.53, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30570000, "wood"), ECommerceAmount(26.89, "iron"), ECommerceAmount(BigDecimal(5.1), "gold") ) } // Creating an originalPrice object. val originalPrice = ECommercePrice(ECommerceAmount(5.78, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30590000, "wood"), ECommerceAmount(26.92, "iron"), ECommerceAmount(BigDecimal(5.5), "gold") ) } // Creating a product object. val product = ECommerceProduct("779213") .apply { this.actualPrice = actualPrice // Optional. this.originalPrice = originalPrice // Optional. promocodes = listOf("BT79IYX", "UT5412EP") // Optional. name = "Продукт творожный «Даниссимо» 5.9%, 130 г." // Optional. categoriesPath = listOf("Продукты", "Молочные продукты", "Йогурты") // Optional. this.payload = payload // Optional. } // Creating a referrer object. val referrer = ECommerceReferrer() .apply { type = "button" // Optional. identifier = "76890" // Optional. this.screen = screen // Optional. } // Creating a cartItem object. val addedItems1 = ECommerceCartItem(product, actualPrice, 1.0) .apply { this.referrer = referrer // Optional. } val addCartItemEvent = ECommerceEvent.addCartItemEvent(addedItems1) // Sending an e-commerce event. YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(addCartItemEvent) val removeCartItemEvent = ECommerceEvent.removeCartItemEvent(addedItems1) // Sending an e-commerce event. YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(removeCartItemEvent)
Copied to clipboard
val payload = mapOf("configuration" to "landscape", "full_screen" to "true") // Creating a screen object. val screen = ECommerceScreen().apply { categoriesPath = listOf("Акции", "Красная цена") // Optional. name = "ProductCardActivity" // Optional. searchQuery = "даниссимо кленовый сироп" // Optional. this.payload = payload // Optional. } // Creating an actualPrice object. val actualPrice = ECommercePrice(ECommerceAmount(4.53, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30570000, "wood"), ECommerceAmount(26.89, "iron"), ECommerceAmount(BigDecimal(5.1), "gold") ) } // Creating an originalPrice object. val originalPrice = ECommercePrice(ECommerceAmount(5.78, "USD")) .apply { internalComponents = listOf( // Optional. ECommerceAmount(30590000, "wood"), ECommerceAmount(26.92, "iron"), ECommerceAmount(BigDecimal(5.5), "gold") ) } // Creating a product object. val product = ECommerceProduct("779213") .apply { this.actualPrice = actualPrice // Optional. this.originalPrice = originalPrice // Optional. promocodes = listOf("BT79IYX", "UT5412EP") // Optional. name = "Продукт творожный «Даниссимо» 5.9%, 130 г." // Optional. categoriesPath = listOf("Продукты", "Молочные продукты", "Йогурты") // Optional. this.payload = payload // Optional. } // Creating a referrer object. val referrer = ECommerceReferrer() .apply { type = "button" // Optional. identifier = "76890" // Optional. this.screen = screen // Optional. } // Creating a cartItem object. val addedItems1 = ECommerceCartItem(product, actualPrice, 1.0) .apply { this.referrer = referrer // Optional. } // Creating an order object. val order = ECommerceOrder("88528768", listOf(addedItems1)) .apply { this.payload = payload // Optional. } val beginCheckoutEvent = ECommerceEvent.beginCheckoutEvent(order) // Sending an e-commerce event. YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(beginCheckoutEvent) val purchaseEvent = ECommerceEvent.purchaseEvent(order) // Sending an e-commerce event. YandexMetrica.getReporter(applicationContext, "Testing API key").reportECommerce(purchaseEvent)
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 ECommerceEvent instance to the main API key, use the YandexMetrica.reportECommerce(@NonNull ECommerceEvent event) method.
... // Sending an e-commerce event. YandexMetrica.reportECommerce(ecommerceEvent);
Copied to clipboard
... // Sending an e-commerce event. YandexMetrica.reportECommerce(ecommerceEvent)
Copied to clipboard