Is There A Way To Get Notified When Someone Makes An In-App Purchase?

后端 未结 4 1794
星月不相逢
星月不相逢 2021-02-19 01:22

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.

<
相关标签:
4条回答
  • 2021-02-19 02:03

    Add Parse to your project. To do so, follow the Quick Start Guide: https://parse.com/apps/quickstart#parse_data/mobile/ios/native/existing

    Once parse is setup, add PFObject *testObject = [PFObject objectWithClassName:@"TestObject"]; testObject[@"foo"] = @"bar"; [testObject saveInBackground]; to your completeTransaction code for each in-app purchase. For example:

    - (void)completeTransaction:(SKPaymentTransaction *)transaction {
    NSLog(@"completeTransaction...");
    
    [self provideContentForProductIdentifier:transaction.payment.productIdentifier];
    // NEW CODE
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iapra"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"No" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iap"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"YES" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iap"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"YES" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iap"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"YES" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iap"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"YES" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iap"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"YES" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    if ([transaction.payment.productIdentifier isEqualToString:@"company.app.iap"]){
        [[NSUserDefaults standardUserDefaults] setObject: @"YES" forKey:KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        PFObject *testObject = [PFObject objectWithClassName:@"IAP"];
        testObject[@"TEST"] = @"Purchase Successful";
        [testObject saveInBackground];
    }
    // NEW CODE ^^
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }
    

    Don't forget to add #import <Parse/Parse.h> to the top of your header.h file.

    I'm not quite sure there are ANY other methods out there like this. It's pretty cool, so enjoy and have fun watching your in-app purchase notifications appear in real time!

    0 讨论(0)
  • 2021-02-19 02:12

    Track StoreKit Purchase Events

    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
            }
        }
    }
    

    Leverage Libraries

    Use analytics. Replace the // Track here... comment above by any of the blocks below. Non exhaustive list in alphabetical order:

    Accengage

    NSString *currencyCode = [<SKProduct.priceLocale>
                              objectForKey:NSLocaleCurrencyCode];
    BMA4SPurchasedItem* item =
        [BMA4SPurchasedItem itemWithId(t.payment.productIdentifier)
            label:t.payment.productName
            category:<product.type>
            price:@(<SKProduct.price>)
            quantity:t.payment.quantity
    ];
    [BMA4STracker trackPurchaseWithId:transaction.identifier
                             currency:currencyCode
                                items:@[item]];
    

    Branch

    NSDictionary *state = @{ 
        @"itemId": @(t.payment.productIdentifier),
        @"price": <SKProduct.price>, 
        @"itemName": <SKProduct.name>, 
        @"currency":currencyCode };
    [[Branch getInstance] userCompletedAction:@"purchase" withState:state];
    

    Fabric (Crashlytics)

    NSString *currencyCode = [<SKProduct.priceLocale>
                              objectForKey:NSLocaleCurrencyCode];
    [Answers logPurchaseWithPrice:<SKProduct.price>
                         currency:currencyCode
                          success:@YES
                         itemName:<product name>
                         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<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
    NSString *currencyCode = [<SKProduct.priceLocale>
                              objectForKey:NSLocaleCurrencyCode];
    [tracker send:[[GAIDictionaryBuilder
                    createItemWithTransactionId:transactionIdentifier
                    name:<product.localizedTitle>
                    sku:t.payment.productIdentifier
                    category:@"Purchase"
                    price:<SKProduct.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];
    

    Send an email from a server

    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://<yoursite>/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:

    <?php
    try {
        header('Content-type: application/json');
        $to = 'bounce@stackoverflow.com';
        $subject = 'Purchase';
        $message = $_POST['productIdentifier'];
        $headers = "From: " . $to . "\n";
        @mail($to, $subject, $message, $headers)
    } catch (Exception $e) {}
    ?>
    

    ► Find this solution on GitHub and additional details on Swift Recipes.

    0 讨论(0)
  • 2021-02-19 02:15

    Fabric (formerly Crashlytics), in addition to being a fantastic (and free) crash logging system, also includes a component called Answers, which tracks usage stats in real time:

    Recently, they added the ability to add custom event tracking, so it's a simple matter to add a "Product Purchased" event to you app. Adding Crashlytics to your app takes seconds (and they walk you through the process), and adding a custom event like that takes a single line of code. From then on, you'll be able to track all sorts of info about purchases made, number of users, and any other metadata you want to record, all with a delay of about 3 seconds.

    I've been using Crashlytics for years (actually based on a StackOverflow question of my own), and I CANNOT recommend it highly enough. It's free, easy, and incredibly effective.

    0 讨论(0)
  • 2021-02-19 02:24

    I record all IAP purchases to table in Parse.com. It is really easy to push all of the data from an IAP receipt up to parse. I am also doing non-renewable subscriptions and I use parse to sync the data between a user's device since StoreKit does not automatically do that for non renewable subscriptions.

    0 讨论(0)
提交回复
热议问题