AWSMobileAnalyticsAppleMonetizationEventBuilder Class Reference

Inherits from AWSMobileAnalyticsMonetizationEventBuilder : NSObject
Declared in AWSMobileAnalyticsAppleMonetizationEventBuilder.h
AWSMobileAnalyticsAppleMonetizationEventBuilder.m

Overview

AWSMobileAnalyticsAppleMonetizationEventBuilder builds monetization events to track purchases from Apple’s IAP Framework. In order to build a monetization event, you must call all of the setters on this builder. You will need to access properties on both the SKProduct class and the SKPurchaseTransaction class in order to build an Apple Monetization event.

The example below demonstrates how to create a monetization event after you recieve a SKPaymentTransaction from the SKPaymentTransactionObserver. Since Apple does not give you any product information in the SKPaymentTransaction object, you must have a way to access your SKProduct store inventory when the purchase is being finalized. In the example below, we have a product catalog object that can return SKProduct’s based on the product id returned in an SKPaymentTransaction object. This product catalog was built after receiving a SKProductsRequest from the SKProductsRequestDelegate.

Example:

 -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction* transaction in transactions) {
       switch (transaction.transactionState) {
          case SKPaymentTransactionStatePurchased:

          // when an item is purchased, give the user access to the content, and
          // record a monetization purchase event.
          [self completeTransaction:transaction];
             break;
          case SKPaymentTransactionStateFailed:
             // ... handle a failed transaction
             break;
          case SKPaymentTransactionStateRestored:
             // ... restore the transaction
             break;
       }
    }
 }

 -(void)completeTransaction:(SKPaymentTransaction*)transaction{
    // provide the content that the user purchased
    [self provideContentForProductIdentifier:transaction.payment.productIdentifier];

    // get the SKProduct for the product identifier that was purchased
    SKProduct* product = [productCatalog objectForKey:transaction.payment.productIdentifier];

    // get the event client for the builder
    idAWSMobileAnalyticsEventClient eventClient = insights.eventClient;

    // create a builder that can record purchase events from Apple
    AWSMobileAnalyticsAppleMonetizationEventBuilder* builder = [AWSMobileAnalyticsAppleMonetizationEventBuilder builderWithEventClient:eventClient];

    // set the product id of the purchased item (obtained from the SKPurchaseTransaction object)
    [builder withProductId:transaction.payment.productIdentifier];

    // set the item price and price locale (obtained from the SKProduct object)
    [builder withItemPrice:[product.price doubleValue]
            andPriceLocale:product.priceLocale];

    // set the quantity of item(s) purchased (obtained from the SKPurchaseTransaction object)
    [builder withQuantity:transaction.payment.quantity];

    // set the transactionId of the transaction (obtained from the SKPurchaseTransaction object)
    [builder withTransactionId:transaction.transactionIdentifier];

    // build the monetization event
    idAWSMobileAnalyticsEvent purchaseEvent = [builder build];

    // add any additional metrics/attributes and record
    [eventClient recordEvent:purchaseEvent];

    // finalize the transaction as required by Apple
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
 }

Other Methods

+ builderWithEventClient:

Create a monetization builder that can record purchase events from Apple’s IAP Framework.

+ (AWSMobileAnalyticsAppleMonetizationEventBuilder *)builderWithEventClient:(id<AWSMobileAnalyticsEventClient>)theEventClient

Parameters

theEventClient

The event client used to create the event

Return Value

a monetization event builder for Apple purchases

Declared In

AWSMobileAnalyticsAppleMonetizationEventBuilder.h

– withProductId:

Sets the product id for the item(s) being purchased

- (void)withProductId:(NSString *)theProductId

Parameters

withProductId

The product id of the item(s) being purchased

Declared In

AWSMobileAnalyticsAppleMonetizationEventBuilder.h

– withItemPrice:andPriceLocale:

Sets the item price and locale of the item being purchased. (Obtained from the SKProduct object)

- (void)withItemPrice:(double)theItemPrice andPriceLocale:(NSLocale *)thePriceLocale

Parameters

theItemPrice

The numerical item price

thePriceLocale

The locale representing the item price’s currency

Declared In

AWSMobileAnalyticsAppleMonetizationEventBuilder.h

– withQuantity:

Sets the quantity of item(s) being purchased. (Obtained from the SKPurchaseTransaction object)

- (void)withQuantity:(NSInteger)theQuantity

Parameters

theQuantity

The quantity of items purchased

Declared In

AWSMobileAnalyticsAppleMonetizationEventBuilder.h

– withTransactionId:

Sets the transaction id of the purchase (Obtained from the SKPurchaseTransaction object)

- (void)withTransactionId:(NSString *)theTransactionId

Parameters

theTransactionId

The transaction id of the purchase

Declared In

AWSMobileAnalyticsAppleMonetizationEventBuilder.h

– build

Builds the monetization event using the parameters set on the builder. You must still record the event.

- (id<AWSMobileAnalyticsEvent>)build

Return Value

an AWSMobileAnalyticsEvent to record

Declared In

AWSMobileAnalyticsAppleMonetizationEventBuilder.h

Other Methods

– initWithEventClient:

Initializes the AWSMobileAnalyticsMonetizationEventBuilder. This class is not meant to be initialized directly. This method should only be called/overridden by derived builders.

- (AWSMobileAnalyticsAppleMonetizationEventBuilder *)initWithEventClient:(id<AWSMobileAnalyticsEventClient>)theEventClient

Declared In

AWSMobileAnalyticsMonetizationEventBuilder.h

– isValid

Determines if this builder is in a valid state. This method must be overridden in derived classes.

- (BOOL)isValid

Return Value

YES if the Builder is valid, NO otherwise.

Declared In

AWSMobileAnalyticsMonetizationEventBuilder.h