Google Analytics(分析)支持 Universal Analytics 媒体资源中提供的增强型电子商务功能。增强型电子商务功能可让您衡量用户在其购物历程中与商品的互动,包括促销信息展示、促销信息点击、商品展示、商品点击、商品详情浏览、将商品添加到购物车、从购物车移除商品、开始结帐流程、交易及退款。
最新的移动应用版 Google 跟踪代码管理器和 Google Analytics(分析)均可与 Google 的移动应用平台 Firebase 结合使用。使用 Firebase SDK 衡量应用时,您将可以获取一系列自动生成的移动应用报告,而且还可以使用应用内代码进一步自定义和补充这些报告。这些报告将自动包含由 iTunes 上的 App Store 以及 Google Play 处理的应用内购买的相关数据。此外,您还可以为电子商务应用实现建议的事件,从而生成其他电子商务相关报告。目前,只有 Universal Analytics 媒体资源提供有关购物行为的深度报告(即增强型电子商务)。
本文档介绍了如何结合使用移动应用版跟踪代码管理器和 Firebase SDK,将增强型电子商务数据发送到 Universal Analytics 媒体资源。
初始步骤
开始之前,请为您的应用设置以下前提条件:
- 在您的应用中安装和配置 Firebase 和 Google 跟踪代码管理器。请确保您使用的是适用于 Android 的 Firebase SDK 版本 11(或更高版本)。
- 导入以下两个程序包:
import com.google.firebase.analytics.FirebaseAnalytics.Event; import com.google.firebase.analytics.FirebaseAnalytics.Param;
- 在您的跟踪代码管理器容器中,使用以下设置创建名为“promotions”的用户定义的变量:
- 事件类型:自定义参数
- 事件参数键:promotions
- 默认值:undefined
实现
以下小节介绍了如何使用必要的参数来记录事件,从而衡量下列增强型电子商务活动:
商品展示
通过 ITEM_LIST
参数以及用相关字段定义的一种或多种商品来记录事件,从而衡量商品展示。
// Define products with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); product1.putLong( Param.INDEX, 1 ); // Position of the item in the list Bundle product2 = new Bundle(); product2.putString( Param.ITEM_ID, "sku5678"); product2.putString( Param.ITEM_NAME, "Android Workout Capris"); product2.putString( Param.ITEM_CATEGORY, "Apparel/Women/Pants"); product2.putString( Param.ITEM_VARIANT, "Black"); product2.putString( Param.ITEM_BRAND, "Google"); product2.putDouble( Param.PRICE, 39.99 ); product2.putString( Param.CURRENCY, "USD" ); product2.putLong( Param.INDEX, 2 ); // Prepare ecommerce bundle ArrayListitems = new ArrayList (); items.add(product1); items.add(product2); Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putParcelableArrayList( "items", items ); // Set relevant bundle-level parameters ecommerceBundle.putString( Param.ITEM_LIST, "Search Results" ); // List name // Log view_search_results or view_item_list event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.VIEW_SEARCH_RESULTS, ecommerceBundle );
商品点击/选择
通过用相关字段定义的商品来记录 SELECT_CONTENT
事件,从而衡量商品点击:
// Define product with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product1.putLong( Param.INDEX, 1 ); // Position of the item in the list // Prepare ecommerce bundle Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putBundle( "items", product1 ); // Set relevant action-level parameters ecommerceBundle.putString( Param.ITEM_LIST, "Search Results" ); // Optional list name // Log select_content event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.SELECT_CONTENT, ecommerceBundle );
商品详情浏览
通过用相关字段定义的商品来记录 VIEW_ITEM 事件,从而衡量商品详情获得的浏览:
// Define product with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today // Prepare ecommerce bundle Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putBundle( "items", product1 ); // Log view_item event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.VIEW_ITEM, ecommerceBundle );
将商品添加到购物车
通过用相关字段定义的商品来记录 ADD_TO_CART 事件,从而衡量向购物车添加商品的情况:
// Define product with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product1.putLong( Param.QUANTITY, 1 ); // Prepare ecommerce bundle Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putBundle( "items", product1 ); // Log add_to_cart event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.ADD_TO_CART, ecommerceBundle );
从购物车移除商品
通过用相关字段定义的商品来记录 REMOVE_FROM_CART 事件,从而衡量从购物车移除商品的情况:
// Define product with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product1.putLong( Param.QUANTITY, 1 ); // Prepare ecommerce bundle Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putBundle( "items", product1 ); // Log remove_from_cart event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.REMOVE_FROM_CART, ecommerceBundle );
促销信息展示
通过用相关字段定义的促销信息来记录 VIEW_ITEM、VIEW_ITEM_LIST 或 VIEW_SEARCH_RESULTS 事件,从而衡量促销信息展示:
// Define promotion with relevant parameters Bundle promotion = new Bundle(); promotion.putString( Param.ITEM_ID, "PROMO_1234" ); // promotion ID; either ITEM_ID or ITEM_NAME is required promotion.putString( Param.ITEM_NAME, "Summer Sale" ); // promotion name promotion.putString( Param.CREATIVE_NAME, "summer_banner2" ); promotion.putString( Param.CREATIVE_SLOT, "banner_slot1" ); // Prepare ecommerce bundle ArrayListpromotions = new ArrayList (); promotions.add(promotion); Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putParcelableArrayList("promotions", promotions ); // Log view_item, view_item_list, or view_search_results event with ecommerce bundle mFirebaseAnalytics.logEvent(Event.VIEW_ITEM, ecommerceBundle );
促销信息点击/选择
通过用相关字段定义的促销信息来记录 SELECT_CONTENT
事件,从而衡量促销信息点击:
// Define promotion with relevant parameters Bundle promotion = new Bundle(); promotion.putString( Param.ITEM_ID, "PROMO_1234"); // promotion ID; either ITEM_ID or ITEM_NAME is required promotion.putString( Param.ITEM_NAME, "Summer Sale"); // promotion name promotion.putString( Param.CREATIVE_NAME, "summer_banner2"); promotion.putString( Param.CREATIVE_SLOT, "banner_slot1"); // Prepare ecommerce bundle ArrayListpromotions = new ArrayList (); promotions.add(promotion); Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putParcelableArrayList("promotions", promotions ); // Set properties for the event to be shown in the Google Analytics (Firebase) reports. // These properties will not impact the Universal Analytics reporting. ecommerceBundle.putString( Param.CONTENT_TYPE, “Internal Promotions” ); ecommerceBundle.putString( Param.ITEM_ID, "PROMO_1234" ); // Log select_content, view_item_list, or view_search_results event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.SELECT_CONTENT, ecommerceBundle );
结帐流程
开始结帐
通过用相关字段定义的一种或多种商品来记录 BEGIN_CHECKOUT 事件,从而衡量结帐流程的第一步:
// Define products with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product1.putLong( Param.QUANTITY, 1 ); // Prepare ecommerce bundle ArrayListitems = new ArrayList (); items.add(product1); Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putParcelableArrayList( "items", items ); // Set checkout step and optional checkout option ecommerceBundle.putLong( Param.CHECKOUT_STEP, 1 ); // Optional for first step ecommerceBundle.putString( Param.CHECKOUT_OPTION, "Visa" ); // Optional // Log BEGIN_CHECKOUT event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.BEGIN_CHECKOUT, ecommerceBundle );
其他结帐步骤
通过用相关字段定义的一种或多种商品来记录 CHECKOUT_PROGRESS 事件,从而衡量结帐流程中的其他步骤:
// Define products with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product1.putLong( Param.QUANTITY, 1 ); // Prepare ecommerce bundle ArrayListitems = new ArrayList (); items.add(product1); Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putParcelableArrayList( "items", items ); // Set checkout step and optional checkout option ecommerceBundle.putLong( Param.CHECKOUT_STEP, 2 ); ecommerceBundle.putString( Param.CHECKOUT_OPTION, "Visa" ); // Optional // Log CHECKOUT_PROGRESS event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.CHECKOUT_PROGRESS, ecommerceBundle );
结帐选项
通过结帐选项,您可以衡量关于结帐流程状态的其他信息。您可以将结帐选项作为结帐步骤事件的一部分进行衡量(如上文所示),也可以在系统记录指定结帐步骤的事件后用户选择相应选项时进行衡量。
通过相应的 CHECKOUT_STEP
和 CHECKOUT_OPTION
参数来记录 SET_CHECKOUT_OPTION
事件,从而衡量用户在某个结帐步骤后选择的结帐选项:
Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putLong( Param.CHECKOUT_STEP, 2 ); ecommerceBundle.putString( Param.CHECKOUT_OPTION, "Mastercard" ); mFirebaseAnalytics.logEvent( Event.SET_CHECKOUT_OPTION, ecommerceBundle );
购买
通过用相关字段定义的一种或多种商品来记录 ECOMMERCE_PURCHASE 事件,从而衡量购买:
// Define product with relevant parameters Bundle product1 = new Bundle(); product1.putString( Param.ITEM_ID, "sku1234"); // ITEM_ID or ITEM_NAME is required product1.putString( Param.ITEM_NAME, "Donut Friday Scented T-Shirt"); product1.putString( Param.ITEM_CATEGORY, "Apparel/Men/Shirts"); product1.putString( Param.ITEM_VARIANT, "Blue"); product1.putString( Param.ITEM_BRAND, "Google"); product1.putDouble( Param.PRICE, 29.99 ); product1.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product1.putLong( Param.QUANTITY, 1 ); Bundle product2 = new Bundle(); product2.putString( Param.ITEM_ID, "sku5678"); product2.putString( Param.ITEM_NAME, "Android Workout Capris"); product2.putString( Param.ITEM_CATEGORY, "Apparel/Women/Pants"); product2.putString( Param.ITEM_VARIANT, "Black"); product2.putString( Param.ITEM_BRAND, "Google"); product2.putDouble( Param.PRICE, 39.99 ); product2.putString( Param.CURRENCY, "USD" ); // Item-level currency unused today product2.putLong( Param.QUANTITY, 1 ); // Prepare ecommerce bundle ArrayListitems = new ArrayList (); items.add(product1); items.add(product2); Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putParcelableArrayList( "items", items ); // Set relevant transaction-level parameters ecommerceBundle.putString( Param.TRANSACTION_ID, "T12345" ); ecommerceBundle.putString( Param.AFFILIATION, "Google Store - Online" ); ecommerceBundle.putDouble( Param.VALUE, 37.39 ); // Revenue ecommerceBundle.putDouble( Param.TAX, 2.85 ); ecommerceBundle.putDouble( Param.SHIPPING, 5.34 ); ecommerceBundle.putString( Param.CURRENCY, "USD" ); ecommerceBundle.putString( Param.COUPON, "SUMMER2017" ); // Log ecommerce_purchase event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.ECOMMERCE_PURCHASE, ecommerceBundle );
退款
通过指定的相关交易 ID,以及用商品 ID 和数量定义的一种或多种商品(根据需要使用,用于部分退款)来记录 PURCHASE_REFUND 事件,从而衡量退款情况:
// Prepare ecommerce bundle with transaction ID to be refunded Bundle ecommerceBundle = new Bundle(); ecommerceBundle.putString( Param.TRANSACTION_ID, "T12345" ); // Required ecommerceBundle.putDouble( Param.VALUE, 37.39 ); // Optional in Universal Analytics // (OPTIONAL) For partial refunds, define the item IDs and quantities of products being refunded Bundle refundedProduct = new Bundle(); refundedProduct.putString( Param.ITEM_ID, "sku1234" ); // Required for partial refund refundedProduct.putLong( Param.QUANTITY, 1 ); // Required for partial refund ArrayListitems = new ArrayList (); items.add(refundedProduct); ecommerceBundle.putParcelableArrayList( "items", items ); // Log purchase_refund event with ecommerce bundle mFirebaseAnalytics.logEvent( Event.PURCHASE_REFUND, ecommerceBundle );