本文档介绍了旧版 Android SDK。如果您是刚接触 Analytics(分析)的新手,请使用最新的 SDK。了解详情

电子商务跟踪 - Android SDK v2(旧版)

本文档将大略介绍如何使用 Android 版 Google Analytics(分析)SDK v2 来衡量应用内付款和收入。

概览

借助电子商务衡量功能,您可以向 Google Analytics(分析)发送应用内购买和销售数据。Google Analytics(分析)中的电子商务数据通常由交易和商品组成,与共享交易 ID 相关联。在 Android 版 Google Analytics(分析)SDK 中,通过创建事务对象并向其添加项目来建立这种关系。

电子商务数据会主要用于以下报告:

  • 电子商务概览
  • 产品业绩
  • 销售业绩
  • 交易次数
  • 购买前所耗时间

实现

使用 Google Analytics(分析)衡量交易需要三个步骤:

  1. 构建事务对象。
  2. 构建商品对象并将其添加到事务对象。
  3. 使用 sendTransaction(Transaction transObject) 发送交易。

在以下示例中,我们假设用户完成应用内购买后,系统会调用 onPurchaseCompleted()

/**
 * The purchase was processed. We will send the transaction and its associated line items to Google Analytics,
 * but only if the purchase has been confirmed.
 */
public void onPurchaseCompleted() {
  Transaction myTrans = new Transaction.Builder(
      "0_123456",                                           // (String) Transaction Id, should be unique.
      (long) (2.16 * 1000000))                              // (long) Order total (in micros)
      .setAffiliation("In-App Store")                       // (String) Affiliation
      .setTotalTaxInMicros((long) (0.17 * 1000000))         // (long) Total tax (in micros)
      .setShippingCostInMicros(0)                           // (long) Total shipping cost (in micros)
      .build();

  myTrans.addItem(new Item.Builder(
      "L_789",                                              // (String) Product SKU
      "Level Pack: Space",                                  // (String) Product name
      (long) (1.99 * 1000000),                              // (long) Product price (in micros)
      (long) 1)                                             // (long) Product quantity
      .setProductCategory("Game expansions")                // (String) Product category
      .build());

    Tracker myTracker = EasyTracker.getTracker(); // Get reference to tracker.
    myTracker.sendTransaction(myTrans); // Send the transaction.
}

货币类型

在 Android 版 Google Analytics(分析)SDK 中,电子商务货币字段必须以微单位表示(以百万分之一为单位)。

例如,如需发送货币价值 4.5991,您应在向 Google Analytics(分析)发送交易时将该值转换为微单位(即 4599100),如上例所示。当 SDK 将该交易分派给 Google Analytics(分析)时,该值会自动转换为定点小数值并作为 4.5991 发送。

货币代码不应包含在您的电子商务代码中,您也不应使用英文逗号。

电子商务货币字段也支持负数货币值,这在退款或退货时可能是必需的。

指定货币

默认情况下,交易值会被视为采用相应数据视图(配置文件)的货币。

如需替换事务的局部货币,请在构建 Transaction 时调用 setCurrencyCode,如以下示例所示:

/**
 * 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() {
  Transaction myTrans = new Transaction.Builder(
      "0_123456",
      (long) (1.59 * 1000000))
      .setAffiliation("In-App Store")
      .setTotalTaxInMicros((long) (0.13 * 1000000))
      .setShippingCostInMicros(0)
      .setCurrencyCode("EUR")                               // (String) Set currency code to Euros.
      .build();

    Tracker myTracker = EasyTracker.getTracker();
    myTracker.sendTransaction(myTrans);
}

如需查看支持的货币和货币代码的完整列表,请参阅支持的货币参考