本指南介绍了如何使用 Android 版 Google 跟踪代码管理器 SDK 实现 Universal Analytics 增强型电子商务功能。
概览
借助 Google Analytics(分析)增强型电子商务功能,您可以随自己的任何 Google Analytics(分析)屏幕浏览和事件一起发送产品展示、促销和销售数据。
准备工作
我们建议您查看 Google Analytics(分析)增强型电子商务开发指南(网站)中的增强型电子商务数据类型和操作部分,以便规划自己的实现方案。该指南可帮助您了解对于要衡量的每次电子商务互动,哪些字段是必需的,哪些是可选的。
实现
下面几部分将向您介绍如何使用数据层来衡量以下增强型电子商务活动:
衡量商品展示情况
- 衡量的电子商务活动:
impressions
- 接受的数据:由
impressionFieldObjects
组成的数组
通过使用 impression
操作以及一个或多个 impressionFieldObjects
来衡量商品展示。以下示例假定在显示屏幕时已知所展示商品的详细信息:
// Product impressions are sent by pushing an impressions object // containing one or more impressionFieldObjects. dataLayer.push("ecommerce", DataLayer.mapOf( "currencyCode", "EUR", // Local currency is optional. "impressions", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", // Name or ID is required. "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray", "list", "Search Results", "position", 1), DataLayer.mapOf( "name", "Donut Friday Scented T-Shirt", "id", "67890", "price", "33.75", "brand", "Google", "category", "Apparel", "variant", "Black", "list", "Search Results", "position", 2))));
衡量商品获得的点击/选择
- 衡量的电子商务活动:
click
- 接受的数据:
list
、由productFieldObjects
组成的数组
通过将 click
操作以及表示所选商品的 productFieldObject
推送到数据层来衡量商品选择:
dataLayer.pushEvent("productClick", DataLayer.mapOf( "ecommerce", DataLayer.mapOf( "click", DataLayer.mapOf( "actionField", DataLayer.mapOf( "list", "Search Results"), // Optional list property. "products", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", // Name or ID is required. "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray"))))));
衡量商品详情的查看情况
- 衡量的电子商务活动:
detail
- 接受的数据:
list
、由productFieldObjects
组成的数组
通过将 detail
操作连同一个或多个表示所查看商品的 productFieldObjects
推送到数据层来衡量商品详情的查看情况:
// Measure a view of product details. dataLayer.push("ecommerce", DataLayer.mapOf( "detail", DataLayer.mapOf( "actionField", DataLayer.mapOf( "list", "Apparel Gallery"), // detail actions have an optional list property. "products", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", // Name or ID is required. "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray")))));
衡量在购物车中添加或移除商品的情况
- 衡量的电子商务活动:
add
、remove
- 接受的数据:
list
、由productFieldObjects
组成的数组
使用 add
或 remove
actionFieldObject
以及 productFieldObjects
列表衡量在购物车中添加或移除商品的情况:
向购物车添加商品
// Measure adding a product to a shopping cart by using an "add" // actionFieldObject and a list of productFieldObjects. dataLayer.pushEvent("addToCart", DataLayer.mapOf( "ecommerce", DataLayer.mapOf( "currencyCode", "EUR", "add", DataLayer.mapOf( // 'add' actionFieldObject measures. "products", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray", "quantity", 1))))));
从购物车移除商品
// Measure the removal of a product from a shopping cart. dataLayer.pushEvent("removeFromCart", DataLayer.mapOf( "ecommerce", DataLayer.mapOf( "remove", DataLayer.mapOf( // 'remove' actionFieldObject measures. "products", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray", "quantity", 1))))));
衡量促销信息
您可以衡量内部应用推广活动(例如为宣传特定商品的促销活动或免运费优惠而展示的横幅广告)的展示次数和选择情况。
衡量促销信息的展示情况
- 衡量的电子商务活动:
promoView
- 接受的数据:由
promoFieldObjects
组成的数组
如需衡量促销活动的展示情况,请将电子商务数据层中的 promoView
键设置为 promoFieldObject
,用于描述要在屏幕上显示给用户的促销活动:
// An example of measuring promotion views. This example assumes that // information about the promotions displayed is available when the screen is // displayed. dataLayer.push("ecommerce", DataLayer.mapOf( "promoView", DataLayer.mapOf( "promotions", DataLayer.listOf( // List of promoFieldObjects. DataLayer.mapOf( "id", "JUNE_PROMO13", // ID or Name is required. "name", "June Sale", "creative", "banner1", "position", "slot1"), DataLayer.mapOf( "id", "FREE_SHIP13", "name", "Free Shipping Promo", "creative", "skyscraper1", "position", "slot2")))));
衡量促销信息的点击/选择情况
要衡量促销活动选择,请将promoClick
操作推送到包含promoFieldObject
描述所选促销活动的数组的数据层:
dataLayer.pushEvent("promotionClick", DataLayer.mapOf( "ecommerce", DataLayer.mapOf( "promoClick", DataLayer.mapOf( "promotions", DataLayer.listOf( DataLayer.mapOf( "id", "JUNE_PROMO13", // Name or ID is required. "name", "June Sale", "creative", "banner1", "position", "slot1"))))));
衡量结帐情况
要衡量结账流程中的每个步骤,您需要:
- 使用
checkout
操作衡量结账流程中的每个步骤。 - 如果适用,使用
checkout_option
操作衡量结账选项。 - (可选)设置直观易懂的步骤名称以用于结账漏斗报告,方法是在网页界面的管理部分中配置电子商务设置。
1. 衡量结账步骤
- 衡量的电子商务活动:
checkout
- 接受的数据:
step
、由productFieldObjects
组成的数组
如需衡量结账流程(流程中可能包含一个结账按钮,以及一个或多个供用户输入配送和付款信息的结账屏幕),请使用 checkout
操作和 step
字段来指明所衡量的是结账流程中的哪个阶段。
您还可以使用 option
字段提供有关结账流程的一段额外数据,例如用户选择的付款方式。
dataLayer.pushEvent("checkout", DataLayer.mapOf( "ecommerce", DataLayer.mapOf( "checkout", DataLayer.mapOf( "actionField", DataLayer.mapOf( "step", 1, "option", "Visa"), "products", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray", "quantity", 1))))));
2. 衡量结账选项
- 衡量的电子商务活动:
checkout_option
- 接受的数据:
step
、option
结账选项在以下情形中很有用:您已衡量某个结账步骤,但想要捕获有关该结账步骤的更多信息。例如,用户选择的配送方式。
要衡量结帐选项,请使用 checkout_option
操作以及 step
和 option
字段。
dataLayer.pushEvent("checkoutOption", DataLayer.mapOf( "ecommerce", DataLayer.mapOf( "checkout_option", DataLayer.mapOf( "products", DataLayer.listOf(), // checkout_option expects a list of products. The workaround is to provide an empty list. "actionField", DataLayer.mapOf( "step", 1, "option", "Express")))));
3. 结帐漏斗配置
(可选)您可以为结账流程中的每一步指定一个描述性的名称,以在报告中使用。要配置此类名称,请转到 Google Analytics(分析)网页界面的管理部分,选择相应数据视图(配置文件),然后点击电子商务设置。请按照相应电子商务设置说明,为要跟踪的每个结账步骤设置标签。

衡量购买情况
- 衡量的电子商务活动:
purchase
- 接受的数据:
id
(交易 ID)、由productFieldObjects
组成的数组
使用 purchase
操作以及 event
(后者会触发已启用增强型电子商务的代码),将您的交易详情推送到数据层。在此示例中,显示屏幕时已知事务详情:
// Send transaction data with a screenview if possible. // Otherwise, use an event when the transaction data becomes available. dataLayer.push("ecommerce", DataLayer.mapOf( "purchase", DataLayer.mapOf( "actionField", DataLayer.mapOf( "id", "T12345", // Transaction ID. Required for purchases and refunds. "affiliation", "Online Store", "revenue", "35.43", // Total transaction value (incl. tax and shipping) "tax", "4.90", "shipping", "5.99", "coupon", "SUMMER_SALE"), "products", DataLayer.listOf( // List of productFieldObjects. DataLayer.mapOf( "name", "Triblend Android T-Shirt", // Name or ID is required. "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray", "quantity", 1, "coupon", ""), // Optional fields may be omitted or set to empty string. DataLayer.mapOf( "name", "Donut Friday Scented T-Shirt", "id", "67890", "price", "33.75", "brand", "Google", "category", "Apparel", "variant", "Black", "quantity", 1)))));
衡量退款情况
- 衡量的电子商务活动:
refund
- 接受的数据:
id
(交易 ID)、由productFieldObjects
组成的数组
如需衡量交易的全额退款情况,请推送 refund
actionFieldObject
以及所退款交易的交易 ID:
// Refund an entire transaction by providing the transaction ID. This example // assumes the details of the completed refund are available when the screen // is displayed. dataLayer.push("ecommerce", DataLayer.mapOf( "refund", DataLayer.mapOf( "products", DataLayer.listOf(), // refund expects a list of products. The workaround is to provide an empty list. "actionField", DataLayer.mapOf( "id", "T12345")))); // Transaction ID. Required for refunds.
如需衡量部分退款情况,请添加 productFieldObjects
列表,包括退款的商品 ID 和数量:
// Measure a partial refund by providing an array of productFieldObjects and // specifying the ID and quantity of each product being returned. This example // assumes the partial refund details are known at the time the screen is // displayed: dataLayer.push("ecommerce", DataLayer.mapOf( "refund", DataLayer.mapOf( "products", DataLayer.listOf( DataLayer.mapOf( "id", "P4567", "quantity", 1), // Product ID & quantity. Required for partial refunds. DataLayer.mapOf( "id", "P8901", "quantity", 2)), "actionField", DataLayer.mapOf( "id", "T12345")))); // Transaction ID.
合并展示和操作数据
既有商品展示又有操作时,可以将两者合并到同一次命中中进行衡量。
dataLayer.push("ecommerce", DataLayer.mapOf( "impressions", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", // Name or ID is required. "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray", "list", "Related Products", "position", 1), DataLayer.mapOf( "name", "Donut Friday Scented T-Shirt", "id", "67890", "price", "33.75", "brand", "Google", "category", "Apparel", "variant", "Black", "list", "Related Products", "position", 2)), "detail", DataLayer.mapOf( "actionField", DataLayer.mapOf( "list", "Apparel Gallery"), // 'detail' actions have an optional list property. "products", DataLayer.listOf( DataLayer.mapOf( "name", "Triblend Android T-Shirt", // Name or ID is required. "id", "12345", "price", "15.25", "brand", "Google", "category", "Apparel", "variant", "Gray")))));