本文档简要介绍了如何使用 iOS 版 Google Analytics(分析)SDK v2 来衡量应用内付款和收入。
概览
借助电子商务衡量功能,您可以向 Google Analytics(分析)发送应用内购买和销售数据。Google Analytics(分析)中的电子商务数据通常由交易和商品组成,与共享交易 ID 相关联。在 iOS 版 Google Analytics(分析)SDK 中,您可以通过创建事务对象并向其添加项目来建立这种关系。
电子商务数据会主要用于以下报告:
- 电子商务概览
- 产品业绩
- 销售业绩
- 交易次数
- 购买前所耗时间
实现
使用 Google Analytics(分析)衡量交易需要三个步骤:
- 构建事务对象。
- 构建商品对象并将其添加到事务对象。
- 使用
sendTransaction:
发送交易。
在以下示例中,我们假设用户完成应用内购买后,系统会调用 onPurchaseCompleted
。
- (void)onPurchaseCompleted { GAITransaction *transaction = [GAITransaction transactionWithId:@"0_123456" // (NSString) Transaction ID, should be unique. withAffiliation:@"In-App Store"]; // (NSString) Affiliation transaction.taxMicros = (int64_t)(0.17 * 1000000); // (int64_t) Total tax (in micros) transaction.shippingMicros = (int64_t)(0); // (int64_t) Total shipping (in micros) transaction.revenueMicros = (int64_t)(2.16 * 1000000); // (int64_t) Total revenue (in micros) [transaction addItemWithSKU:@"L_789" // (NSString) Product SKU name:@"Level Pack: Space" // (NSString) Product name category:@"Game expansions" // (NSString) Product category priceMicros:(int64_t)(1.99 * 1000000) // (int64_t) Product price (in micros) quantity:1]; // (NSInteger) Product quantity [[GAI sharedInstance].defaultTracker sendTransaction:transaction]; // Send the transaction. }
货币类型
在 iOS 版 Google Analytics(分析)SDK 中,电子商务货币字段必须以微单位表示(以百万分之一为单位)。
例如,如需发送货币价值 4.5991
,您应在向 Google Analytics(分析)发送交易时将该值转换为微单位(即 4599100
),如上例所示。当 SDK 将该交易分派给 Google Analytics(分析)时,该值会自动转换为定点小数值并作为 4.5991
发送。
货币代码不应包含在您的电子商务代码中,您也不应使用英文逗号。
电子商务货币字段也支持负数货币值,这在退款或退货时可能是必需的。