简体   繁体   中英

Android: Inapp purchase - Google Analytics

I implemented inapp purchase successfully in android app. I just want to log the purchase event in google analytics. If the application is in foreground, i can log the event through PurchaseObserver. But If the application in background, how to log the event in google analytics. Currently i am using EasyTracking library to log the events.

Please Help. thanks in advance.

Try something like this with the new v2 Google Analytics Android API

public void onPurchaseCompleted() {
  Transaction myTrans = new Transaction.Builder(
      "0_123456",                                           // (String) Transaction Id, should be unique.
      (long) 2.16 * 1000000)                                // (long) Order total (in micros)
      .setAffiliation("In-App Store")                       // (String) Affiliation
      .setTotalTaxInMicros((long) 0.17 * 1000000)           // (long) Total tax (in micros)
      .setShippingCostInMicros(0)                           // (long) Total shipping cost (in micros)
      .build();

  myTrans.addItem(new Item.Builder(
      "L_789",                                              // (String) Product SKU
      "Level Pack: Space",                                  // (String) Product name
      (long) 1.99 * 1000000,                                // (long) Product price (in micros)
      (long) 1)                                             // (long) Product quantity
      .setProductCategory("Game expansions")                // (String) Product category
      .build());

    Tracker myTracker = EasyTracker.getTracker(); // Get reference to tracker.
    myTracker.trackTransaction(myTrans); // Track the transaction.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM