Ecommerce Tracking - Android SDK

This document provides an overview of how to measure in-app payments and revenue using the Google Analytics SDK for Android v3.

Overview

Ecommerce measurement allows you to send in-app purchases and sales to Google Analytics. Ecommerce data in Google Analytics is comprised of transaction and item hits, related by a shared transaction ID.

Transactions have the following fields:

Field Name Tracker Field Type Required Description
Transaction ID Fields.TRANSACTION_ID String Yes A unique ID representing the transaction. This ID should not collide with other transaction IDs.
Affiliation Fields.TRANSACTION_AFFILIATION String Yes An entity with which the transaction should be affiliated (e.g. a particular store)
Revenue Fields.TRANSACTION_REVENUE Double Yes The total revenue of a transaction, including tax and shipping
Tax Fields.TRANSACTION_TAX Double Yes The total tax for a transaction
Shipping Fields.TRANSACTION_SHIPPING Double Yes The total cost of shipping for a transaction
Currency code Fields.CURRENCY_CODE String No The local currency of a transaction. Defaults to the currency of the view (profile) in which the transactions are being viewed.

Items have the following fields:

Field Name Tracker Field Type Required Description
Transaction ID Fields.TRANSACTION_ID String Yes The transaction ID with which the item should be associated
Name Fields.ITEM_NAME String Yes The name of the product
SKU Fields.ITEM_SKU String Yes The SKU of a product
Category Fields.ITEM_CATEGORY String No A category to which the product belongs
Price Fields.ITEM_PRICE Double Yes The price of a product
Quantity Fields.ITEM_QUANTITY Long Yes The quantity of a product
Currency code Fields.CURRENCY_CODE String No The local currency of a transaction. Defaults to the currency of the view (profile) in which the transactions are reported.

Ecommerce data is used primarily in the following standard reports:

  • Ecommerce Overview
  • Product Performance
  • Sales Performance
  • Transactions
  • Time to Purchase

Implementation

Sending transaction and item data to Google Analytics requires setting the transaction and item field values on the tracker and sending them, one at a time. For example:

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

public class CheckoutActivity extends Activity {

  /*
   * Called when a purchase is processed and verified.
   */
  public void onPurchaseCompleted() {

    // May return null if EasyTracker has not yet been initialized with a
    // property ID.
    EasyTracker easyTracker = EasyTracker.getInstance(this);

    easyTracker.send(MapBuilder
        .createTransaction("0_123456",       // (String) Transaction ID
                           "In-app Store",   // (String) Affiliation
                           2.16d,            // (Double) Order revenue
                           0.17d,            // (Double) Tax
                           0.0d,             // (Double) Shipping
                           "USD")            // (String) Currency code
        .build()
    );

    easyTracker.send(MapBuilder
        .createItem("0_123456",               // (String) Transaction ID
                    "Level Pack: Space",      // (String) Product name
                    "L_789",                  // (String) Product SKU
                    "Game expansions",        // (String) Product category
                    1.99d,                    // (Double) Product price
                    1L,                       // (Long) Product quantity
                    "USD")                    // (String) Currency code
        .build()
    );
  }

  // ... Rest of the Activity definition
}

Ecommerce currency fields support negative currency values, as may be necessary in the case of refunds or returns.

Specifying Currencies

By default, transaction values are assumed to be in the currency of the view (profile) in which they are reported.

To override the local currency of a transaction and any associated products, set the currency code field of the transaction and item hits with the new currency code. For the complete list of supported currencies and currency codes, see the Supported Currencies Reference.

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

public class CheckoutActivity extends Activity {
  /**
   * In this example, the currency of the transaction is set to Euros. The
   * currency values will appear in reports using the global currency
   * type of the view (profile).
   */
  public void onPurchaseCompleted() {

    // Assumes a tracker has already been initialized with a property ID, otherwise
    // this call returns null.
    EasyTracker easyTracker = EasyTracker.getInstance(this);

    easyTracker.send(MapBuilder
        .createTransaction("0_123456",       // (String) Transaction ID, should be unique among transactions.
                           "In-app Store",   // (String) Affiliation
                           (long) 2.16,      // (long) Order revenue (includes tax and shipping)
                           (long) 0.17,      // (long) Tax
                           0.0,              // (long) Shipping cost
                           "EUR")            // (String) Currency code
        .build();

  }
  // ... Rest of the Activity definition.
}