完成购买前的Facebook iOS事件跟踪

时间:2014-11-30 01:23:15

标签: ios facebook flurry facebook-sdk-3.0 mixpanel

Facebook正在跟踪我的inapp购买情况,但看起来它是在完成inapp购买完成导致错误值之前。下面是我正在使用的代码,看到所有交易时我也遇到同样的问题 - 不仅仅是在Mixpanel& amp;乱舞。

- (void) completeTransaction: (SKPaymentTransaction *)transaction {

if([transaction.payment.productIdentifier isEqualToString:kIAPPro]) {
    [self becomeProWithReceipt:transaction.transactionReceipt];
}
else{
    SKProduct *product = nil;
    int credits = 0;

    if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits20]) {
        credits = 20;
    }
    else if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits30]) {
        credits = 30;
    }
    else if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits60]) {
        credits = 60;
    }
    else if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits100]) {
        credits = 100;
    }
    else if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits200]) {
        credits = 200;
    }
    else if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits425]) {
        credits = 425;
    }
    else if ([transaction.payment.productIdentifier isEqualToString:kIAPCredits650]) {
        credits = 650;
    }
    else if([transaction.payment.productIdentifier isEqualToString:kIAPCredits1100]) {
        credits = 1100;
    }
    else if([transaction.payment.productIdentifier isEqualToString:kIAPCredits2500]) {
        credits = 2500;
    }
    else if([transaction.payment.productIdentifier isEqualToString:kIAPCredits3800]) {
        credits = 3800;
    }
    else if([transaction.payment.productIdentifier isEqualToString:kIAPCredits5000]) {
        credits = 5000;
    }

    // get the product associated with this transaction
    NSArray *filteredProducts = [self.products filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"productIdentifier = %@", transaction.payment.productIdentifier]];
    if ([filteredProducts count] > 0) {
        product = [filteredProducts objectAtIndex:0];
    }

    if(product)
    {
        // extract transaction product quantity
        int quantity = transaction.payment.quantity;

        // extract unit price of the product
        float unitPrice = [product.price floatValue];

        // assign revenue generated from the current product
        float revenue = unitPrice * quantity;

        // assign the currency code extracted from the transaction
        NSString *currencyCode = [product.priceLocale objectForKey:NSLocaleCurrencyCode];

        // Log to our server backend
        NSString *encodedReceipt = transaction.transactionReceipt.base64EncodedString;
        JBRequest *request = [RequestHelper sendRequestOfAddCredits:self userID:[GlobalPool sharedInstance].loggedInUser.userID receiptData:encodedReceipt];
        request.userInfo = [NSNumber numberWithInt:credits];

        // Localytics
        [[LocalyticsSession shared] tagEvent:[NSString stringWithFormat:@"Add %d Credits", credits]];

        // Flurry
        [Flurry logEvent:[NSString stringWithFormat:@"Add %d Credits", credits]];

        // Mixpanel
        [[Mixpanel sharedInstance] track:[NSString stringWithFormat:@"Add %d Credits", credits]];
        [[Mixpanel sharedInstance].people trackCharge:[NSNumber numberWithFloat:revenue] withProperties:@{@"$time": [DateUtils getStringFromDateWithFormat:@"YYYY-MM-dd'T'HH:mm:ss" date:[NSDate date] adjustGmt:NO]}];

        // Facebook Events
        [FBAppEvents logPurchase:revenue currency:currencyCode parameters:@{FBAppEventParameterNameContentID: transaction.payment.productIdentifier}];

        // create MAT measurement event item
        MATEventItem *eventItem = [MATEventItem eventItemWithName:product.localizedTitle unitPrice:unitPrice quantity:quantity revenue:revenue attribute1:@"attr1" attribute2:@"attr2" attribute3:@"attr3" attribute4:@"attr4" attribute5:@"attr5"];

        NSArray *arrEventItems = @[ eventItem ];
        NSLog(@"Event Item = %@", eventItem);

        // default measurement event name
        NSString *eventName = @"purchase";

        // measure the purchase transaction event

        // Any extra revenue that might be generated over and above the revenues generated from event items.
        // Total event revenue = sum of even item revenues in arrEventItems + extraRevenue
        float extraRevenue = 0; // default to zero

        [MobileAppTracker measureAction:eventName
                             eventItems:arrEventItems
                            referenceId:transaction.transactionIdentifier
                          revenueAmount:extraRevenue
                           currencyCode:currencyCode
                       transactionState:transaction.transactionState
                                receipt:transaction.transactionReceipt
         ];

        NSLog(@"Transaction event measured: %@", eventName);
    }
}

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

0 个答案:

没有答案