電子商務追蹤 - 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);
}

如需支援的貨幣和貨幣代碼的完整清單,請參閱支援的貨幣參考資料