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

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

概览

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

以下报告中主要使用电子商务数据:

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

实现

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

  1. 构建交易对象。
  2. 构建 item 对象,并将其添加到交易对象中。
  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);
}

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