使用Google Analytics iOS SDK进行应用内购买跟踪

时间:2022-08-21 15:19:00

I would like to track in-app purchases with the Google Analytics SDK for iOS v2, as indicated in their Ecommerce Tracking guide.

我希望使用Google Analytics SDK for iOS v2跟踪应用内购买情况,如电子商务跟踪指南中所示。

I'm currently doing the following after receiving a SKPaymentTransactionStatePurchased transaction update:

我在收到SKPaymentTransactionStatePurchased交易更新后正在执行以下操作:

- (void) trackTransaction:(SKPaymentTransaction*)transaction
{
    NSString *transactionIdentifier = transaction.transactionIdentifier;
    GAITransaction *gaiTransaction = [GAITransaction transactionWithId:transactionIdentifier withAffiliation:@"App Store"];

    SKPayment *payment = transaction.payment;
    NSString *productIdentifier = payment.productIdentifier;
    SKProduct *product = [self productForIdentifier:productIdentifier];
    NSString *productTitle = product.localizedTitle;
    int64_t priceInMicros = product.price.floatValue * 1000000; // FIXME: Doesn't consider different currencies
    [gaiTransaction addItemWithCode:productIdentifier name:productTitle category:nil priceMicros:priceInMicros quantity:payment.quantity];

    gaiTransaction.revenueMicros = priceInMicros * payment.quantity; // FIXME: doesn't consider Apple's cut

    id<GAITracker> tracker = [GAI sharedInstance].defaultTracker;
    [tracker trackTransaction:gaiTransaction];
}

Is the above the right way of tracking in-app purchases? I detect two problems at the very least:

以上是跟踪应用内购买的正确方法吗?我至少发现了两个问题:

  1. SKProduct returns a localized price and if I track it as-is revenue aggregation will be incorrect. Is there a way to normalize the price?
  2. SKProduct返回本地化价格,如果我跟踪它,收入汇总将是不正确的。有没有办法规范价格?
  3. The revenue returned doesn't take Apple's cut into account, which is not always 30%. Is it possible to obtain the net revenue within the app?
  4. 退回的收入并没有考虑Apple的削减,这并不总是30%。是否有可能获得应用程序内的净收入?

2 个解决方案

#1


11  

SKProduct returns a localized price and if I track it as-is revenue aggregation will be incorrect. Is there a way to normalize the price?

SKProduct返回本地化价格,如果我跟踪它,收入汇总将是不正确的。有没有办法规范价格?

Google Analytics SKD for iOS v3 added support for currencies. Tracking a transaction looks like this:

适用于iOS v3的Google Analytics SKD增加了对货币的支持。跟踪事务如下所示:

- (void)storePaymentTransactionFinished:(NSNotification *)notification
{
    SKPaymentTransaction *paymentTransaction = notification.transaction;
    if (paymentTransaction.transactionState == SKPaymentTransactionStateRestored) return;

    SKPayment *payment = paymentTransaction.payment;
    NSString *sku = payment.productIdentifier;
    SKProduct *product = [[RMStore defaultStore] productForIdentifier:sku];
    NSLocale *priceLocale = product.priceLocale;
    NSString *currencyCode = [priceLocale objectForKey:NSLocaleCurrencyCode];
    NSString *transactionId = paymentTransaction.transactionIdentifier;
    NSNumber *productPrice = product.price;
    {
        NSNumber *revenue = @(productPrice.floatValue * payment.quantity);
        GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createTransactionWithId:transactionId
                                                                          affiliation:@"App Store"
                                                                              revenue:revenue
                                                                                  tax:0
                                                                             shipping:0
                                                                         currencyCode:currencyCode];
        NSDictionary *parameters = [builder build];
        [_tracker send:parameters];
    }
    {
        GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createItemWithTransactionId:transactionId
                                                                                     name:product.localizedTitle
                                                                                      sku:sku
                                                                                 category:@"In-App Purchase"
                                                                                    price:productPrice
                                                                                 quantity:@(payment.quantity)
                                                                             currencyCode:currencyCode];
        NSDictionary *parameters = [builder build];
        [_tracker send:parameters];
    }
}

The above code uses RMStore for convenience.

上面的代码使用RMStore来方便。

The revenue returned doesn't take Apple's cut into account, which is not always 30%. Is it possible to obtain the net revenue within the app?

退回的收入并没有考虑Apple的削减,这并不总是30%。是否有可能获得应用程序内的净收入?

No.

没有。

#2


2  

Good question, shame you've got no answers to this as I've just come up against the same issue.

好问题,惭愧你没有得到答案,因为我刚刚遇到同样的问题。

So to stimulate discussion on this topic I'd like to suggest this "workaround".

因此,为了激发关于这个主题的讨论,我想建议这个“解决方法”。

NSString *affiliation = [product.priceLocale objectForKey:NSLocaleCurrencyCode];  
GAITransaction *gaiTransaction = [GAITransaction transactionWithId:transactionIdentifier withAffiliation:affiliation];

The idea being that you can't do anything about different currencies in-App. Instead you need to move the problem to Analytics and use it's filtering to work out earnings per national App Store.

这个想法是你不能对应用程序中的不同货币做任何事情。相反,您需要将问题移至Google Analytics并使用其过滤来计算每个国家/地区App Store的收入。

So if it's a US transaction the affiliate will be "USD".

因此,如果是美国交易,会员将是“美元”。

As for

至于

take Apple's cut into account, which is not always 30%

考虑Apple的削减,这并不总是30%

this is news to me. I was of the impression the 30% was set in stone by Apple. So I'm just multiplying revenue by 0.7

这对我来说是新闻。我的印象是苹果公司30%的成功。所以我只是将收入乘以0.7

#1


11  

SKProduct returns a localized price and if I track it as-is revenue aggregation will be incorrect. Is there a way to normalize the price?

SKProduct返回本地化价格,如果我跟踪它,收入汇总将是不正确的。有没有办法规范价格?

Google Analytics SKD for iOS v3 added support for currencies. Tracking a transaction looks like this:

适用于iOS v3的Google Analytics SKD增加了对货币的支持。跟踪事务如下所示:

- (void)storePaymentTransactionFinished:(NSNotification *)notification
{
    SKPaymentTransaction *paymentTransaction = notification.transaction;
    if (paymentTransaction.transactionState == SKPaymentTransactionStateRestored) return;

    SKPayment *payment = paymentTransaction.payment;
    NSString *sku = payment.productIdentifier;
    SKProduct *product = [[RMStore defaultStore] productForIdentifier:sku];
    NSLocale *priceLocale = product.priceLocale;
    NSString *currencyCode = [priceLocale objectForKey:NSLocaleCurrencyCode];
    NSString *transactionId = paymentTransaction.transactionIdentifier;
    NSNumber *productPrice = product.price;
    {
        NSNumber *revenue = @(productPrice.floatValue * payment.quantity);
        GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createTransactionWithId:transactionId
                                                                          affiliation:@"App Store"
                                                                              revenue:revenue
                                                                                  tax:0
                                                                             shipping:0
                                                                         currencyCode:currencyCode];
        NSDictionary *parameters = [builder build];
        [_tracker send:parameters];
    }
    {
        GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createItemWithTransactionId:transactionId
                                                                                     name:product.localizedTitle
                                                                                      sku:sku
                                                                                 category:@"In-App Purchase"
                                                                                    price:productPrice
                                                                                 quantity:@(payment.quantity)
                                                                             currencyCode:currencyCode];
        NSDictionary *parameters = [builder build];
        [_tracker send:parameters];
    }
}

The above code uses RMStore for convenience.

上面的代码使用RMStore来方便。

The revenue returned doesn't take Apple's cut into account, which is not always 30%. Is it possible to obtain the net revenue within the app?

退回的收入并没有考虑Apple的削减,这并不总是30%。是否有可能获得应用程序内的净收入?

No.

没有。

#2


2  

Good question, shame you've got no answers to this as I've just come up against the same issue.

好问题,惭愧你没有得到答案,因为我刚刚遇到同样的问题。

So to stimulate discussion on this topic I'd like to suggest this "workaround".

因此,为了激发关于这个主题的讨论,我想建议这个“解决方法”。

NSString *affiliation = [product.priceLocale objectForKey:NSLocaleCurrencyCode];  
GAITransaction *gaiTransaction = [GAITransaction transactionWithId:transactionIdentifier withAffiliation:affiliation];

The idea being that you can't do anything about different currencies in-App. Instead you need to move the problem to Analytics and use it's filtering to work out earnings per national App Store.

这个想法是你不能对应用程序中的不同货币做任何事情。相反,您需要将问题移至Google Analytics并使用其过滤来计算每个国家/地区App Store的收入。

So if it's a US transaction the affiliate will be "USD".

因此,如果是美国交易,会员将是“美元”。

As for

至于

take Apple's cut into account, which is not always 30%

考虑Apple的削减,这并不总是30%

this is news to me. I was of the impression the 30% was set in stone by Apple. So I'm just multiplying revenue by 0.7

这对我来说是新闻。我的印象是苹果公司30%的成功。所以我只是将收入乘以0.7