📜 ⬆️ ⬇️

Sales tracking with Google Analytics

Trying to adhere to the tradition, I publish the translation of the next article from the android-developers blog. This time, the article is devoted to tracking sales within applications using the Google Analytics Android SDK.

Why is it important?


If you distribute certain products through your application, then most likely you will want to know what the real income generated by sales. And also, for example, a list of the most popular or best-selling goods.


Before you start


First, you need to configure to use the Google Analytics Android SDK. If you haven’t already done this, check out the SDK documentation . The following implies that you have configured your application to interact with the SDK. Also, we will assume that in your code you have already declared the “tracking” object of Google Analytics:
')
GoogleAnalyticsTracker tracker; 


The object must be initialized in the onCreate () method, and the start () method is called for it.

 tracker = GoogleAnalyticsTracker.getInstance(); tracker.start("UA-YOUR-ACCOUNT-HERE", 30, this); 


Transaction tracking


The best place to track a single transaction will be the confirmation of the purchase. Suppose you have a method called after confirming a purchase. The tracking code should be placed there:

 public void onPurchaseConfirmed(List purchases) { //   Google Analytics      } 


The Google Analytcs SDK provides a transaction object to store all the information you need to collect. The next step is to copy the information from the list of purchase objects (PurchaseObjects) into the transaction object (Transaction).

The transaction object uses a builder pattern, in which the constructor needs to pass the required arguments, and the remaining arguments are set optionally using setters:

 Transaction.Builder builder = new Transaction.Builder( purchase.getOrderId(), purchase.getTotal()) .setTotalTax(purchase.getTotalTax()) .setShippingCost(purchase.getShippingCost() .setStoreName(purchase.getStoreName()); 


Now you can pass the information to the transaction object:

 tracker.addTransaction(builder.build()); 


Tracking all parts of a transaction


The next step is tracking parts of the transaction. This is somewhat similar to tracking a single transaction. Use the Item class from the Google Analytics SDK. Google Analytics uses OrderID as a common identifier to associate multiple items in a transaction. Each object of sale (PurchaseObjects) has its own list of items. You can walk through them with an elementary iterator:

 for (ListItem listItem : purchase.getListItems()) { Item.Builder itemBuilder = new Item.Builder( purchase.getOrderId(), listItem.getItemSKU(), listItem.getPrice(), listItem.getCount()) .setItemCategory(listItem.getItemCategory()) .setItemName(listItem.getItemName()); //      . orderId -  // Google Analytics       . tracker.addItem(itemBuilder.build()); } 


Submitting data to Google Analytics


After all transactions and items have been added, you can call the method:

 tracker.trackTransactions(); 


And it will send your data to Google Analytics.

View reports


After the data has been collected, you can view reports through Conversions -> Ecommerce -> Product Performance .

image

You will see how much revenue individual goods bring you. Can you identify the most "running" and so on. The picture shows that people buy a lot of potions, which generate the main income :-)
It is also clear that blue swords buy more often red, which means that people like blue things more; and do not need to add to the application a lot of red.

Conclusion


You can learn more about this SDK from the documentation . It also promises that the upcoming I / O session will be dedicated to the use of Google Analytics in Android applications.

Source: https://habr.com/ru/post/118867/


All Articles