iOS v3 - 增强型电子商务

本指南介绍如何使用 iOS 版 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": @{
                    @"currencyCode": @"EUR",                      // Local currency is optional.
                    @"impressions": @[
                      @{@"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},
                      @{@"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 push:@{@"event": @"productClick",
                  @"ecommerce": @{
                    @"click": @{
                      @"actionField": @{
                        @"list": @"Search Results"},              // Optional list property.
                      @"products": @[
                        @{@"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": @{
                    @"detail": @{
                      @"actionField": @{
                        @"list": @"Apparel Gallery"},             // 'detail' actions have an optional list property.
                      @"products": @[
                        @{@"name": @"Triblend Android T-Shirt",   // Name or ID is required.
                          @"id": @"12345",
                          @"price": @"15.25",
                          @"brand": @"Google",
                          @"category": @"Apparel",
                          @"variant": @"Gray"}]}}}];

衡量在购物车中添加或移除商品的情况

  • 衡量的电子商务活动:addremove
  • 接受的数据:list,由 productFieldObjects 组成的数组

使用 addremove actionFieldObject 以及 productFieldObjects 列表衡量在购物车中添加或移除商品的操作:

向购物车添加商品

// Measure adding a product to a shopping cart by using an "add"
// actionFieldObject and a list of productFieldObjects.
[dataLayer push:@{@"event": @"addToCart",
                  @"ecommerce": @{
                    @"currencyCode": @"EUR",
                    @"add": @{                                  // 'add' actionFieldObject measures.
                      @"products": @[
                        @{@"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 push:@{@"event": @"removeFromCart",
                  @"ecommerce": @{
                    @"remove": @{                               // 'remove' actionFieldObject measures.
                      @"products": @[
                        @{@"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": @{
                    @"promoView": @{
                      @"promotions": @[                     // Array of promoFieldObjects.
                        @{@"id": @"JUNE_PROMO13",           // ID or Name is required.
                          @"name": @"June Sale",
                          @"creative": @"banner1",
                          @"position": @"slot1"},
                        @{@"id": @"FREE_SHIP13",
                          @"name": @"Free Shipping Promo",
                          @"creative": @"skyscraper1",
                          @"position": @"slot2"}]}}}];

衡量促销信息的点击/选择

如需衡量所选的促销信息,请将 promoClick 操作连同一个包含 promoFieldObject(用于描述所选促销信息)的数组推送到数据层:

[dataLayer push:@{@"event": @"promotionClick",
                  @"ecommerce": @{
                    @"promoClick": @{
                      @"promotions": @[
                        @{@"id":  @"JUNE_PROMO13",          // Name or ID is required.
                          @"name": @"June Sale",
                          @"creative": @"banner1",
                          @"position": @"slot1"}]}}}];

衡量结账情况

要衡量结账流程中的每个步骤,您需要:

  1. 使用 checkout 操作衡量结账流程中的每个步骤。
  2. 如果适用,使用 checkout_option 操作衡量结账选项。
  3. (可选)设置直观易懂的步骤名称以用于结账漏斗报告,方法是在网页界面的管理部分中配置电子商务设置

1. 衡量结账步骤

  • 衡量的电子商务活动:checkout
  • 接受的数据:step,由 productFieldObjects 组成的数组

要衡量结账流程(可能包括一个结账按钮以及一个或多个供用户输入配送和付款信息的结账屏幕),请使用 checkout 操作和 step 字段来指明所衡量的是结账流程的哪个阶段。 您还可以使用 option 字段提供有关结账流程的一段额外数据,例如用户选择的付款方式。

[dataLayer push:@{@"event": @"checkout",
                  @"ecommerce": @{
                    @"checkout": @{
                      @"actionField": @{
                        @"step": @1,
                        @"option": @"Visa"},
                      @"products": @[
                        @{@"name": @"Triblend Android T-Shirt",
                          @"id": @"12345",
                          @"price": @"15.25",
                          @"brand": @"Google",
                          @"category": @"Apparel",
                          @"variant": @"Gray",
                          @"quantity": @1}]}}}];

2. 衡量结账选项

  • 衡量的电子商务活动:checkout_option
  • 接受的数据:stepoption

结账选项在以下情形中很有用:您已衡量某个结账步骤,但想要捕获有关该结账步骤的更多信息。例如,用户选择的配送方式。 要衡量结账选项,请使用 checkout_option 操作以及 stepoption 字段。

[dataLayer push:@{@"event": @"checkoutOption",
                  @"ecommerce": @{
                    @"checkout_option": @{
                      @"actionField": @{
                        @"step": @1,
                        @"option": @"Express"}}}}];

3. 结账漏斗配置

(可选)您可以为结账流程中的每一步指定一个描述性的名称,以在报告中使用。要配置此类名称,请转到 Google Analytics(分析)网页界面的管理部分,选择相应数据视图(配置文件),然后点击电子商务设置。请按照相应电子商务设置说明,为要跟踪的每个结账步骤设置标签。

Google Analytics(分析)管理界面中的电子商务设置。某个结账漏斗设定了 4 个步骤:1. 查看购物车;2. 收集付款信息;3. 确认订单详情;4. 收据。
图 1:结账漏斗的电子商务设置。

衡量购买情况

  • 衡量的电子商务活动: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": @{
                    @"purchase": @{
                      @"actionField": @{
                        @"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": @[                             // List of productFieldObjects.
                        @{@"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.
                        @{@"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": @{
                    @"refund": @{
                      @"actionField": @{
                        @"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": @{
                    @"refund": @{
                      @"actionField": @{
                        @"id": @"T12345"},      // Transaction ID.
                      @"products": @[
                        @{@"id": @"P4567",
                          @"quantity": @1},     // Product ID & quantity. Required for partial refunds.
                        @{@"id": @"P8901",
                          @"quantity": @2}]}}}];

合并展示和操作数据

既有产品展示又有操作时,可以将两者合并到同一次命中中进行衡量。

下例显示了如何衡量相关商品部分中的商品详情查看商品展示

[dataLayer push:@{@"ecommerce": @{
                    @"impressions": @[
                      @{@"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},
                      @{@"name": @"Donut Friday Scented T-Shirt",
                        @"id": @"67890",
                        @"price": @"33.75",
                        @"brand": @"Google",
                        @"category": @"Apparel",
                        @"variant": @"Black",
                        @"list": @"Related Products",
                        @"position": @2}],
                    @"detail": @{
                      @"actionField": @{
                        @"list": @"Apparel Gallery"},               // 'detail' actions have an optional list property.
                      @"products": @[
                        @{@"name": @"Triblend Android T-Shirt",     // Name or ID is required.
                          @"id": @"12345",
                          @"price": @"15.25",
                          @"brand": @"Google",
                          @"category": @"Apparel",
                          @"variant": @"Gray"}]}}}];