I\'d like to be notified when someone makes an In-App Purchase in my App rather than wait until the next day to check iTunes Connect to see wether or not I had any sales.
<
When a purchase takes place, send yourself a datapoint (Track here...)
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transation in transactions {
switch transation.transactionState {
case .purchased:
queue.finishTransaction(transation)
// Track here...
case .purchasing: break
case .restored: break
case .deferred: break
case .failed: break
}
}
}
Use analytics. Replace the // Track here...
comment above by any of the blocks below. Non exhaustive list in alphabetical order:
Accengage
NSString *currencyCode = [
objectForKey:NSLocaleCurrencyCode];
BMA4SPurchasedItem* item =
[BMA4SPurchasedItem itemWithId(t.payment.productIdentifier)
label:t.payment.productName
category:
price:@()
quantity:t.payment.quantity
];
[BMA4STracker trackPurchaseWithId:transaction.identifier
currency:currencyCode
items:@[item]];
Branch
NSDictionary *state = @{
@"itemId": @(t.payment.productIdentifier),
@"price": ,
@"itemName": ,
@"currency":currencyCode };
[[Branch getInstance] userCompletedAction:@"purchase" withState:state];
Fabric (Crashlytics)
NSString *currencyCode = [
objectForKey:NSLocaleCurrencyCode];
[Answers logPurchaseWithPrice:
currency:currencyCode
success:@YES
itemName:
itemType:@"Purchase"
itemId:@(t.payment.productIdentifier)
customAttributes:@{}];
FlightRecorder
FlightRecorder.sharedInstance().trackEventWithCategory(
"Actions",
action: "Purchase",
label: "productIdentifier",
value: t.payment.productIdentifier)
Flurry Analytics
let properties = ["productIdentifier":t.payment.productIdentifier]
Flurry.logEvent("Purchase", withParameters: properties)
Google Analytics
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
id tracker = [[GAI sharedInstance] defaultTracker];
NSString *currencyCode = [
objectForKey:NSLocaleCurrencyCode];
[tracker send:[[GAIDictionaryBuilder
createItemWithTransactionId:transactionIdentifier
name:
sku:t.payment.productIdentifier
category:@"Purchase"
price:
quantity:@(t.payment.quantity)
currencyCode:currencyCode]
build]];
See In-app purchase tracking with Google Analytics iOS SDK.
Heap Analytics
[Heap track:@"Purchase"
withProperties:@{@"productIdentifier":@(t.payment.productIdentifier)}
];
Mixpanel Analytics(*)
Mixpanel.sharedInstance().track("Purchased",
properties: ["productIdentifier":transation.payment.productIdentifier])
properties:@{@"productIdentifier":@(t.payment.productIdentifier)};
(*) Provides support for WiFi reporting (allows to postpone all reporting until WiFi network is available, as to not use cellular data). See mixpanelWillFlush
below.
Parse.com
NSDictionary *dimensions =
@{@"productIdentifier":@(t.payment.productIdentifier)};
[PFAnalytics trackEvent:@“Purchase” dimensions:dimensions];
POST
purchase to a URL, and have in turn the server send you a mail or other notification.
iOS implementation using URLSession
:
if let url = URL(string: "https:///php/purchase.php") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody =
"{\"productIdentifier\":\"\(transaction.payment.productIdentifier)\"}"
.data(using: .utf8)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request as URLRequest,
completionHandler: {_,_,_ in })
task.resume()
}
purchase.php
email sender:
► Find this solution on GitHub and additional details on Swift Recipes.