Google Analytics - get iOS in app purchase values
google-analytics, in-app-purchase, ios, objective-c, transactions
Solution
Here is my final code:
- (void)trackPurchaseWithTransaction:(SKPaymentTransaction *)transaction andAction:(NSString *)action
{
//Only track purchase, not restore
if(![action isEqualToString:IAPHelperProductPurchasedNotification]) return;
id tracker = [[GAI sharedInstance] defaultTracker];
SKPayment *payment = transaction.payment;
NSString *sku = payment.productIdentifier;
SKProduct *product;
for(SKProduct *skProduct in skProducts) {
if([skProduct.productIdentifier isEqualToString:skProduct.productIdentifier]) {
product = skProduct;
}
}
NSLocale *priceLocale = product.priceLocale;
NSString *currencyCode = [priceLocale objectForKey:NSLocaleCurrencyCode];
NSString *transactionId = transaction.transactionIdentifier;
NSNumber *productPrice = product.price;
NSNumber *revenue = @(productPrice.floatValue * payment.quantity);
GAIDictionaryBuilder *transBuilder = [GAIDictionaryBuilder createTransactionWithId:transactionId
affiliation:@"App Store"
revenue:revenue
tax:0
shipping:0
currencyCode:currencyCode];
[tracker send:[transBuilder build]];
GAIDictionaryBuilder *itemBuilder = [GAIDictionaryBuilder createItemWithTransactionId:transactionId
name:product.localizedTitle
sku:sku
category:@"In-App Purchase"
price:productPrice
quantity:@(payment.quantity)
currencyCode:currencyCode];
[tracker send:[itemBuilder build]];
}
Problem
I followed this tutorial to successfully implement in app purchasing in my iOS app. I'm now trying to implement Google Analytics to track purchases with the following code. But on successful purchase completion I'm unsure where to get the actual values from. Any sample code would be greatly appreciated. ``` - (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. } ```