電子商務追蹤 - Android SDK v2 (舊版)

本文件概述如何使用 Google Analytics SDK for Android 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);
}

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