增强型电子商务跟踪 - iOS SDK

本文档将大略介绍如何使用 iOS 版 Google Analytics(分析)SDK v3 来衡量应用内电子商务相关操作和展示。

概览

增强型电子商务功能可让您衡量用户在其购物历程中与产品的互动,包括产品展示、产品点击、查看产品详情、将产品添加到购物车、开始结帐流程、交易以及退款。

实现

在您的应用中实现增强型电子商务跟踪功能之前,您必须将增强型电子商务库添加到您的应用。

在将您的应用配置为使用增强型电子商务后,您就可以:

衡量电子商务活动

典型的增强型电子商务实现将会衡量产品展示次数以及以下任一操作:

  • 选择产品。
  • 查看产品详情。
  • 内部促销信息的展示和选择。
  • 向购物车中添加产品或从中移除产品。
  • 开始产品结帐流程。
  • 购买和退款。

衡量展示

要衡量产品展示,请设置 product 值和 impression 值,并将相应信息随匹配一起发送:

id tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommerceProduct *product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12345"];
[product setName:@"Android Warhol T-Shirt"];
[product setCategory:@"Apparel/T-Shirts"];
[product setBrand:@"Google"];
[product setVariant:@"Black"];
[product setCustomDimension:1 value:@"Member"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createScreenView];

// Sets the product impression for the next available slot, starting with 1.
[builder addProductImpression:product
               impressionList:@"Search Results"
             impressionSource:@"From Search"];
[tracker set:kGAIScreenName value:@"My Impression Screen"];
[tracker send:[builder build]];

Product 必须有 name 或 id 值。其他所有值都非必需,可以不用设置。

衡量操作

操作的衡量方法如下:先设置 product 值,然后设置 product action 值来指定对产品执行的操作。

例如,以下代码衡量对搜索结果列表中展示的某个产品的选择:

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommerceProduct *product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12345"];
[product setName:@"Android Warhol T-Shirt"];
[product setCategory:@"Apparel/T-Shirts"];
[product setBrand:@"Google"];
[product setVariant:@"Black"];
[product setCustomDimension:1 value:@"Member"];

GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPAClick];
[action setProductActionList:@"Search Results"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createScreenView];
[builder setProductAction:action];

// Sets the product for the next available slot, starting with 1
[builder addProduct:product];
[tracker set:kGAIScreenName value:@"My Impression Screen"];
[tracker send:[builder build]];

Product 必须有 name 或 id 值。其他所有值都非必需,可以不用设置。

合并展示和操作数据

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

下例显示了如何衡量一次在相关产品部分中的展示以及一次产品详情查看:

// The product from the related products section.
id tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommerceProduct *product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12346"];
[product setName:@"Android Warhol T-Shirt"];
[product setCategory:@"Apparel/T-Shirts"];
[product setBrand:@"Google"];
[product setVariant:@"White"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createScreenView];

// Sets the product impression for the next available slot, starting with 1.
[builder addProductImpression:product
               impressionList:@"Related Products"
             impressionSource:@"From Related"];

// The product being viewed.
product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12345"];
[product setName:@"Android Warhol T-Shirt"];
[product setCategory:@"Apparel/T-Shirts"];
[product setBrand:@"Google"];
[product setVariant:@"Black"];

GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPADetail];
[builder setProductAction:action];
// Sets the product for the next available slot, starting with 1.
[builder addProduct:product];
[tracker set:kGAIScreenName value:@"Related Products Screen"];
[tracker send:[builder build]];

衡量交易

交易的衡量方法如下:先设置 product 值,然后设置 product action 值来指定购买操作。总收入、税费、运费等交易级详情在 product action 值中设置。

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommerceProduct *product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12345"];
[product setName:@"Android Warhol T-Shirt"];
[product setCategory:@"Apparel/T-Shirts"];
[product setBrand:@"Google"];
[product setVariant:@"Black"];
[product setPrice:@29.20];
[product setCouponCode:@"APPARELSALE"];
[product setQuantity:@1];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"Purchase"
                                                                        label:nil
                                                                        value:nil];
GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPAPurchase];
[action setTransactionId:@"T12345"];
[action setAffiliation:@"Google Store - Online"];
[action setRevenue:@37.39];
[action setTax:@2.85];
[action setShipping:@5.34];
[action setCouponCode:@"SUMMER2013"];
[builder setProductAction:action];

// Sets the product for the next available slot, starting with 1
[builder addProduct:product];
[tracker send:[builder build]];

指定货币

默认情况下,您可以通过 Google Analytics(分析)的管理网络界面为所有交易和商品配置一种通用的全局货币。

局部货币必须按 ISO 4217 标准指定。如需支持的完整转换货币列表,请参阅货币代码参考文档。

局部货币可通过为跟踪器设置 currency code 值来指定。例如,此跟踪器将以欧元发送货币金额值:

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTracker"];
[tracker set:kGAIScreenName value:@"transaction"];
[tracker set:kGAICurrencyCode value:@"EUR"]; // Set tracker currency to Euros.
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createScreenView];
[tracker send:[builder build]];

衡量退款

要为整个交易退款,请设置 product action 值来指定交易 ID 和退款操作类型:

// Refund an entire transaction.
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"Refund"
                                                                        label:nil
                                                                        value:nil];
GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPARefund];
[action setTransactionId:@"T12345"];
[builder setProductAction:action];
[tracker send:[builder build]];

如果未找到相符的交易,则退款将不会得到处理。

要衡量部分退款,请设置 product action 值来指定要退款的交易 ID、产品 ID 和产品数量:

// Refund a single product.
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"Refund"
                                                                        label:nil
                                                                        value:nil];
GAIEcommerceProduct *product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12345"]; // Product ID is required for partial refund.
[product setQuantity:@1]; // Quanity is required for partial refund.
[builder addProduct:product];

GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPARefund];
[action setTransactionId:@"T12345"]; // Transaction ID is required for partial refund.
[builder setProductAction:action];
[tracker send:[builder build]];

为退款使用非互动事件

如果您需要使用事件来发送退款数据,但该事件不属于通常衡量的行为(即并非由用户发起),则建议您发送非互动事件。这可让特定指标免受该事件的影响。例如:

// Refund an entire transaction.
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"Refund"
                                                                        label:nil
                                                                        value:nil];

[builder set:@"1" forKey:kGAINonInteraction];

GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPARefund];
[action setTransactionId:@"T12345"];

[builder setProductAction:action];
[tracker send:[builder build]];

衡量结帐流程

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

  1. 添加跟踪代码,以衡量结帐流程中的每一步。
  2. 如果适用,添加跟踪代码以衡量结帐选项。
  3. (可选)设置直观易懂的步骤名称以用于结帐渠道报告,方法是在网络界面的“管理”部分中配置电子商务设置

1. 衡量结帐步骤

对于结帐流程中的每一步,您都需要实现相应的跟踪代码,以便向 Google Analytics(分析)发送数据。

Step 字段

对于要衡量的每一个结帐步骤,您都应加入 step 值。此值用于将结帐操作映射到您在电子商务设置中为每个步骤配置的标签。

Option 字段

在衡量某个结帐步骤时,如果您有关于此步骤的更多信息,则可以为 checkout 操作设置 option 字段来捕获此信息,例如用户的默认付款方式(如“Visa”)。

衡量某个结帐步骤

要衡量某个结帐步骤,请先设置 product 值,然后设置 product action 值来指示结帐操作。如果适用,还可以设置该结帐步骤的 step 和 option 值。

下例显示了如何衡量结帐流程的第一步(一个产品,拥有关于付款方式的额外信息):

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommerceProduct *product = [[GAIEcommerceProduct alloc] init];
[product setId:@"P12345"];
[product setName:@"Android Warhol T-Shirt"];
[product setCategory:@"Apparel/T-Shirts"];
[product setBrand:@"Google"];
[product setVariant:@"Black"];
[product setPrice:@29.20];
[product setCouponCode:@"APPARELSALE"];
[product setQuantity:@1];

GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"Checkout"
                                                                        label:nil
                                                                        value:nil];

// Add the step number and additional info about the checkout to the action.
GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPACheckout];
[action setCheckoutStep:@1];
[action setCheckoutOption:@"Visa"];

[builder addProduct:product];
[builder setProductAction:action];
[tracker send:[builder build]];

2. 衡量结帐选项

结帐选项可让您衡量关于结帐状态的额外信息。有时您已经衡量了某个结帐步骤,但在用户设置了选项之后,关于此步骤有了新的额外信息,在这种情况下,结帐选项就可以派上用场。例如,用户选择了送货方式。

要衡量结帐选项,请设置 product action 值来指示结帐选项,并加入步骤序号和选项说明信息。

您很可能希望在用户执行特定操作进入结帐流程中的下一步时衡量此操作。例如:

// (On "Next" button click.)
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"CheckoutOption"
                                                                        label:nil
                                                                        value:nil];

GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPACheckoutOption];
[action setCheckoutStep:@1];
[action setCheckoutOption:@"Fedex"];

[builder setProductAction:action];
[tracker send:[builder build]];
// Advance to next page.

3. 结帐渠道配置

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

Google Analytics(分析)网络界面“管理”部分中的“电子商务设置”。开启了电子商务功能,为 4 个结帐渠道步骤指定了标签:1. 检查购物车,2. 填写付款信息,3. 确认订单详情,4. 收据
图 1:电子商务设置 - 结账渠道

衡量内部促销

增强型电子商务功能支持对内部促销信息的展示次数和选择次数进行衡量,例如对促销活动进行宣传的横幅。

促销信息展示

内部促销信息的展示一般在初始屏幕浏览或事件发生时衡量,方法为设置 promotion 值。例如:

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommercePromotion *promotion = [[GAIEcommercePromotion alloc] init];
[promotion setId:@"PROMO_1234"];
[promotion setName:@"Summer Sale"];
[promotion setCreative:@"summer_banner2"];
[promotion setPosition:@"banner_slot1"];

GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Ecommerce"
                                                                       action:@"Promotion"
                                                                        label:nil
                                                                        value:nil];

[builder addPromotion:promotion];
[tracker send:[builder build]];

促销信息点击

内部促销信息点击的衡量方法如下:先设置 promotion 值,然后设置 product action 值来指示促销信息点击操作。例如:

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"MyTrackingId"];
GAIEcommercePromotion *promotion = [[GAIEcommercePromotion alloc] init];
[promotion setId:@"PROMO_1234"];
[promotion setName:@"Summer Sale"];
[promotion setCreative:@"summer_banner2"];
[promotion setPosition:@"banner_slot1"];

GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:@"Internal Promotions"
                                                                       action:@"click"
                                                                        label:@"Summer Sale"
                                                                        value:nil];

[builder set:kGAIPromotionClick forKey:kGAIPromotionAction];
[builder addPromotion:promotion];
[tracker send:[builder build]];