增强型电子商务

作为 Google Analytics(分析)在 Universal Analytics 媒体资源中提供的一项功能,增强型电子商务可让您衡量用户在整个购物体验中的所有商品互动,包括商品展示、商品点击、商品详情获得的浏览、将商品添加到购物车、开始结帐流程、交易及退款。

最新一代的移动应用版 Google 跟踪代码管理器和 Google Analytics(分析)均可与 Google 的移动应用平台 Firebase 结合使用。当通过 Firebase SDK 衡量应用时,您将有权访问大量自动生成的移动应用报告,而这些报告还可通过应用内代码做进一步的自定义和补充。在这些报告中,将自动包含关于 iTunes 上的 App Store 以及 Google Play 所处理的应用内购买的数据。此外,您还可以为电子商务应用实现建议的事件,从而生成其他电子商务相关报告。目前,仅在 Universal Analytics 媒体资源中提供有关购物行为(即增强型电子商务)的深度报告。

本文档介绍了如何结合使用移动应用版跟踪代码管理器和 Firebase SDK,将增强型电子商务数据发送到 Universal Analytics 媒体资源。所使用的事件和参数语法应始终相同,以便为将来的其他电子商务报告做好准备。

初始步骤

开始之前:

  1. 在您的应用中安装和配置 Firebase 和 Google 跟踪代码管理器。请确保您使用的是适用于 Android 的 Firebase SDK 版本 11(或更高版本)。
  2. 在您的跟踪代码管理器容器中,使用以下设置配置一个名为“promotions”的用户定义的变量
    • 事件类型:自定义参数
    • 事件参数键:promotions
    • 默认值:undefined

实现

以下部分将介绍如何使用必要的参数来记录事件,从而衡量下列增强型电子商务活动:

商品展示

在衡量商品展示时,应使用 kFIRParameterItemID 参数以及通过相关字段定义的一种或多种商品记录各种事件。

// Define product with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterIndex : @1          // Position of the item in the list.
};

NSDictionary *product2 = @{
   kFIRParameterItemID : @"sku5678", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Capri",
   kFIRParameterItemCategory : @"Apparel/Women/Pants",
   kFIRParameterItemVariant : @"Black",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @35.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterIndex : @2          // Position of the item in the list.
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1, product2];

NSDictionary *ecommerce = @{
   @"items" : items,
   kFIRParameterItemList : @"Search Results" // List name.
};

// Log select_content event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventViewSearchResults
                    parameters:ecommerce];

商品点击/选择

在衡量商品点击时,应通过相关字段所定义的商品记录 kFIREventSelectContent 事件,具体代码如下:

// Define product with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterIndex : @1          // Position of the item in the list.
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1];

NSDictionary *ecommerce = @{
   @"items" : items,
   kFIRParameterItemList : @"Search Results" // List name.
};

// Log select_content event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventSelectContent
                    parameters:ecommerce];

商品详情获得的浏览

在衡量商品详情获得的浏览时,应通过相关字段所定义的商品记录 kFIREventViewItem 事件,具体代码如下:

// Define product with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1];

NSDictionary *ecommerce = @{
   @"items" : items,
   kFIRParameterItemList : @"Search Results" // List name.
};

// Log view_item event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventViewItem
                    parameters:ecommerce];

向购物车添加商品

在衡量向购物车添加的商品时,应通过相关字段所定义的商品记录 kFIREventAddToCart 事件,具体代码如下:

// Define product with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterQuantity : @1
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1];

NSDictionary *ecommerce = @{
   @"items" : items
};

// Log add_to_cart event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventAddToCart
                    parameters:ecommerce];

从购物车中移除商品

在衡量从购物车中移除的商品时,应通过相关字段所定义的商品记录 kFIREventRemoveFromCart 事件,具体代码如下:

// Define product with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterQuantity : @1
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1];

NSDictionary *ecommerce = @{
   @"items" : items
};

// Log remove_from_cart event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventRemoveFromCart
                    parameters:ecommerce];

促销信息展示

在衡量促销信息展示时,应通过相关字段所定义的促销商品记录 kFIREventViewItem 事件,具体代码如下:

// Define promotion with relevant parameters.
NSDictionary *promotion = @{
   kFIRParameterItemID : @"PROMO_1234", // promotion ID; either ITEM_ID or ITEM_NAME is.
   kFIRParameterItemName : @"Summer Sale", // promotion name.
   kFIRParameterCreativeName : @"summer_banner2",
   kFIRParameterCreativeSlot : @"banner_slot1"
};

// Prepare ecommerce dictionary.
NSArray *promotions = @[promotion];
NSDictionary *ecommerce = @{
   @"promotions" : promotions
};

// Log view_item, view_item_list, or view_search_results
// event with ecommerce bundle.
[FIRAnalytics logEventWithName:kFIREventViewItem
                    parameters:ecommerce];

促销信息点击/选择

在衡量促销信息点击时,应通过相关字段所定义的促销信息记录 kFIREventSelectContent 事件,具体代码如下:

// Define promotion with relevant parameters.
NSDictionary *promotion = @{
   kFIRParameterItemID : @"PROMO_1234", // promotion ID; either ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Summer Sale", // promotion name.
   kFIRParameterCreativeName : @"summer_banner2",
   kFIRParameterCreativeSlot : @"banner_slot1"
};

// Prepare ecommerce dictionary.
NSArray *promotions = @[promotion];

// Set properties for the event to be shown in the Google Analytics (Firebase) reports.
// These properties will not impact the Universal Analytics reporting.
NSDictionary *ecommerce = @{
   kFIRParameterItemID : @"PROMO_1234",
   kFIRParameterContentType : @"Internal Promotions",
   @"promotions" : promotions
};

// Log select_content, view_item_list, or view_search_results event with ecommerce bundle.
[FIRAnalytics logEventWithName:kFIREventSelectContent
                    parameters:ecommerce];

结帐流程

开始结帐

在衡量结帐流程的第一步时,应通过相关字段所定义的一种或多种商品来记录 kFIREventBeginCheckout 事件,具体代码如下:

// Define products with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterQuantity : @1
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1];

// Set checkout step and optional checkout option.
NSDictionary *ecommerce = @{
   @"items" : items,
   kFIRParameterCheckoutStep : @1, // Optional for first step.
   kFIRParameterCheckoutOption : @"Visa" // Optional.
};

// Log BEGIN_CHECKOUT event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventBeginCheckout
                    parameters:ecommerce];

结帐选项

通过各种结帐选项,您还可以衡量关于结帐流程状态的其他额外信息。就这些结帐选项而言,您可以将其纳入到结帐步骤事件中进行衡量(如上文所示),也可以在用户于给定结帐步骤的事件记录完毕后选择相应选项时进行衡量。

在结帐步骤后衡量结帐选项时,应通过相应的 kFIRParameterCheckoutStepkFIRParameterCheckoutOption 参数记录 kFIREventCheckoutProgress 事件,具体代码如下:

// Define products with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterQuantity : @1
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1];

// Set checkout step and optional checkout option.
NSDictionary *ecommerce = @{
   @"items" : items,
   kFIRParameterCheckoutStep : @2, // Optional for first step.
   kFIRParameterCheckoutOption : @"Visa" // Optional.
};

// Log CHECKOUT_PROGRESS event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventCheckoutProgress
                   parameters:ecommerce];

购买

在衡量购买时,应通过相关字段所定义的一种或多种商品记录 kFIREventEcommercePurchase 事件,具体代码如下:

// Define products with relevant parameters.
NSDictionary *product1 = @{
   kFIRParameterItemID : @"sku1234", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Jogger Sweatpants",
   kFIRParameterItemCategory : @"Apparel/Men/Pants",
   kFIRParameterItemVariant : @"Blue",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @39.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterQuantity : @1
};

NSDictionary *product2 = @{
   kFIRParameterItemID : @"sku5678", // ITEM_ID or ITEM_NAME is required.
   kFIRParameterItemName : @"Android Capri",
   kFIRParameterItemCategory : @"Apparel/Women/Pants",
   kFIRParameterItemVariant : @"Black",
   kFIRParameterItemBrand : @"Google",
   kFIRParameterPrice : @35.99,
   kFIRParameterCurrency : @"USD",  // Item-level currency unused today.
   kFIRParameterQuantity : @1
};

// Prepare ecommerce dictionary.
NSArray *items = @[product1, product2];

NSDictionary *ecommerce = @{
   @"items" : items,
   kFIRParameterItemList : @"Search Results", // List name.
   kFIRParameterTransactionID : @"T12345",
   kFIRParameterAffiliation : @"Google Store - Online",
   kFIRParameterValue : @75.98, // Revenue.
   kFIRParameterTax : @3.80,
   kFIRParameterShipping : @5.34,
   kFIRParameterCurrency : @"USD",
   kFIRParameterCoupon : @"SUMMER2017"
};

// Log ecommerce_purchase event with ecommerce dictionary.
[FIRAnalytics logEventWithName:kFIREventEcommercePurchase
                    parameters:ecommerce];

退款

在衡量退款情况时,应通过指定的相关交易 ID 以及使用商品 ID 和数量定义的一种或多种商品(可选,且仅适用于部分退款)记录 kFIREventPurchaseRefund 事件,具体代码如下:

// (OPTIONAL) For partial refunds, define the item IDs and
// quantities of products being refunded.
NSDictionary *refundedProduct = @{
   kFIRParameterItemID : @"sku1234", // Required for partial refund.
   kFIRParameterQuantity : @1
};

// Prepare ecommerce bundle with transaction ID to be refunded.
NSDictionary *ecommerce = @{
   @"items" : @[ refundedProduct ],
   kFIRParameterTransactionID : @"T12345", // Required.
   kFIRParameterValue : @75.98 // Optional in Universal Analytics.
};

// Log purchase_refund event with ecommerce.
[FIRAnalytics logEventWithName:kFIREventPurchaseRefund
                    parameters:ecommerce];