From dd815fef843523969d24f63272eae9091319448a Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Sat, 7 Jan 2017 19:16:17 +0800 Subject: [PATCH] improved analyzer events --- .../GooglePlayInAppPurchaseActivity.kt | 42 ++++--------------- .../mariotaku/twidere/util/FabricAnalyzer.kt | 23 ++++++---- .../model/ParcelableMediaExtensions.kt | 5 +-- .../model/ParcelableStatusExtensions.kt | 2 +- ...ExtraFeaturesIntroductionDialogFragment.kt | 8 ++++ .../twidere/fragment/StatusFragment.kt | 5 ++- .../fragment/filter/FilteredUsersFragment.kt | 7 +++- .../ExtraFeaturesIntroductionCardFragment.kt | 10 ++++- .../twidere/model/analyzer/PurchaseConfirm.kt | 15 +++++++ .../{Purchase.kt => PurchaseFinished.kt} | 16 ++----- .../model/analyzer/PurchaseIntroduction.kt | 16 +++++++ .../twidere/model/analyzer/StatusView.kt | 26 +++++++++++- 12 files changed, 111 insertions(+), 64 deletions(-) create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseConfirm.kt rename twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/{Purchase.kt => PurchaseFinished.kt} (67%) create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseIntroduction.kt diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/activity/GooglePlayInAppPurchaseActivity.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/activity/GooglePlayInAppPurchaseActivity.kt index 7a3530f1f..cbe90090b 100644 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/activity/GooglePlayInAppPurchaseActivity.kt +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/activity/GooglePlayInAppPurchaseActivity.kt @@ -59,7 +59,7 @@ class GooglePlayInAppPurchaseActivity : BaseActivity(), BillingProcessor.IBillin override fun onBillingInitialized() { // See https://github.com/anjlab/android-inapp-billing-v3/issues/156 if (intent.action == ACTION_RESTORE_PURCHASE) { - performRestorePurchase() + getProductDetailsAndFinish() } else { billingProcessor.purchase(this, productId) } @@ -91,10 +91,11 @@ class GooglePlayInAppPurchaseActivity : BaseActivity(), BillingProcessor.IBillin finish() } - private fun performRestorePurchase() { + + private fun getProductDetailsAndFinish() { executeAfterFragmentResumed { val weakThis = WeakReference(it as GooglePlayInAppPurchaseActivity) - val dfRef = WeakReference(ProgressDialogFragment.show(it.supportFragmentManager, "consume_purchase_progress")) + val dfRef = WeakReference(ProgressDialogFragment.show(it.supportFragmentManager, TAG_PURCHASE_PROCESS)) task { val activity = weakThis.get() ?: throw PurchaseException(BILLING_RESPONSE_RESULT_USER_CANCELED) val productId = activity.productId @@ -116,38 +117,7 @@ class GooglePlayInAppPurchaseActivity : BaseActivity(), BillingProcessor.IBillin }.alwaysUi { weakThis.get()?.executeAfterFragmentResumed { val fm = weakThis.get()?.supportFragmentManager - val df = dfRef.get() ?: (fm?.findFragmentByTag("consume_purchase_progress") as? DialogFragment) - df?.dismiss() - } - } - } - } - - private fun getProductDetailsAndFinish() { - executeAfterFragmentResumed { - val weakThis = WeakReference(it as GooglePlayInAppPurchaseActivity) - val dfRef = WeakReference(ProgressDialogFragment.show(it.supportFragmentManager, "consume_purchase_progress")) - task { - val activity = weakThis.get() ?: throw PurchaseException(BILLING_RESPONSE_RESULT_USER_CANCELED) - val bp = activity.billingProcessor - bp.loadOwnedPurchasesFromGoogle() - val skuDetails = bp.getPurchaseListingDetails(productId) - ?: throw PurchaseException(BILLING_RESPONSE_RESULT_ERROR) - val transactionDetails = bp.getPurchaseTransactionDetails(productId) - ?: throw PurchaseException(BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED) - return@task Pair(skuDetails, transactionDetails) - }.successUi { result -> - weakThis.get()?.handlePurchased(result.first, result.second) - }.failUi { error -> - if (error is PurchaseException) { - weakThis.get()?.handleError(error.code) - } else { - weakThis.get()?.handleError(BILLING_RESPONSE_RESULT_ERROR) - } - }.alwaysUi { - weakThis.get()?.executeAfterFragmentResumed { - val fm = weakThis.get()?.supportFragmentManager - val df = dfRef.get() ?: (fm?.findFragmentByTag("consume_purchase_progress") as? DialogFragment) + val df = dfRef.get() ?: (fm?.findFragmentByTag(TAG_PURCHASE_PROCESS) as? DialogFragment) df?.dismiss() } } @@ -170,6 +140,8 @@ class GooglePlayInAppPurchaseActivity : BaseActivity(), BillingProcessor.IBillin class PurchaseException(val code: Int) : Exception() companion object { + private const val TAG_PURCHASE_PROCESS = "get_purchase_process" + const val EXTRA_PRODUCT_ID = "product_id" const val ACTION_RESTORE_PURCHASE = "${INTENT_PACKAGE_PREFIX}RESTORE_PURCHASE" } diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt index dfb76fb73..730b012a7 100644 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt @@ -22,7 +22,6 @@ package org.mariotaku.twidere.util import android.accounts.Account import android.accounts.AccountManager import android.accounts.OnAccountsUpdateListener -import android.app.Activity import android.app.Application import android.os.Build import com.crashlytics.android.Crashlytics @@ -33,10 +32,7 @@ import org.mariotaku.ktextension.configure import org.mariotaku.twidere.BuildConfig import org.mariotaku.twidere.Constants import org.mariotaku.twidere.TwidereConstants.ACCOUNT_TYPE -import org.mariotaku.twidere.model.analyzer.Purchase -import org.mariotaku.twidere.model.analyzer.Search -import org.mariotaku.twidere.model.analyzer.Share -import org.mariotaku.twidere.model.analyzer.SignIn +import org.mariotaku.twidere.model.analyzer.* import java.math.BigDecimal import java.util.* @@ -85,10 +81,23 @@ class FabricAnalyzer : Analyzer(), Constants { putAttributes(event) }) } - is Purchase -> { + is PurchaseConfirm -> { + answers.logStartCheckout(configure(StartCheckoutEvent()) { + event.forEachValues { name, value -> + putCustomAttribute(name, value) + } + }) + } + is PurchaseIntroduction -> { + answers.logAddToCart(configure(AddToCartEvent()) { + event.forEachValues { name, value -> + putCustomAttribute(name, value) + } + }) + } + is PurchaseFinished -> { answers.logPurchase(configure(PurchaseEvent()) { putItemName(event.productName) - putSuccess(event.resultCode == Activity.RESULT_OK) if (!event.price.isNaN() && event.currency != null) { putCurrency(Currency.getInstance(event.currency) ?: Currency.getInstance(Locale.getDefault())) putItemPrice(BigDecimal(event.price)) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMediaExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMediaExtensions.kt index c0f705ad7..7c66c9e4c 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMediaExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMediaExtensions.kt @@ -6,8 +6,7 @@ import org.mariotaku.twidere.model.ParcelableMedia * Created by mariotaku on 2017/1/7. */ -fun parcelableMediaTypeString(@ParcelableMedia.Type type: Int): String { - if (type <= 0) return "none" +fun parcelableMediaTypeString(@ParcelableMedia.Type type: Int): String? { return when (type) { ParcelableMedia.Type.IMAGE -> "image" ParcelableMedia.Type.VIDEO -> "video" @@ -15,6 +14,6 @@ fun parcelableMediaTypeString(@ParcelableMedia.Type type: Int): String { ParcelableMedia.Type.CARD_ANIMATED_GIF -> "gif" ParcelableMedia.Type.EXTERNAL_PLAYER -> "external" ParcelableMedia.Type.VARIABLE_TYPE -> "variable" - else -> "unknown" + else -> null } } \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt index 165890101..f496a7cf8 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt @@ -6,4 +6,4 @@ import org.mariotaku.twidere.model.ParcelableStatus * Created by mariotaku on 2017/1/7. */ val ParcelableStatus.media_type: Int - get() = (media ?: quoted_media)?.firstOrNull()?.type ?: 0 \ No newline at end of file + get() = media?.firstOrNull()?.type ?: 0 \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtraFeaturesIntroductionDialogFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtraFeaturesIntroductionDialogFragment.kt index 70cb9e25e..e7db50b4e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtraFeaturesIntroductionDialogFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtraFeaturesIntroductionDialogFragment.kt @@ -6,6 +6,10 @@ import android.support.v7.app.AlertDialog import android.view.View import org.mariotaku.twidere.R import org.mariotaku.twidere.constant.IntentConstants.EXTRA_REQUEST_CODE +import org.mariotaku.twidere.model.analyzer.PurchaseConfirm +import org.mariotaku.twidere.model.analyzer.PurchaseFinished +import org.mariotaku.twidere.model.analyzer.PurchaseIntroduction +import org.mariotaku.twidere.util.Analyzer import org.mariotaku.twidere.util.premium.ExtraFeaturesService /** @@ -36,6 +40,7 @@ class ExtraFeaturesIntroductionDialogFragment : BaseDialogFragment() { } else { activity.startActivityForResult(purchaseIntent, requestCode) } + Analyzer.log(PurchaseConfirm(PurchaseFinished.NAME_EXTRA_FEATURES)) } builder.setNegativeButton(R.string.action_later) { dialog, which -> @@ -55,6 +60,9 @@ class ExtraFeaturesIntroductionDialogFragment : BaseDialogFragment() { View.GONE } } + if (savedInstanceState == null) { + Analyzer.log(PurchaseIntroduction(PurchaseFinished.NAME_EXTRA_FEATURES, "introduction dialog")) + } return dialog } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt index e5cb29969..52d00e0a6 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt @@ -437,7 +437,10 @@ class StatusFragment : BaseSupportFragment(), LoaderCallbacks { - Analyzer.log(Purchase.fromActivityResult(Purchase.NAME_EXTRA_FEATURES, resultCode, data)) + if (resultCode == Activity.RESULT_OK) { + Analyzer.log(PurchaseFinished.create(PurchaseFinished.NAME_EXTRA_FEATURES, data)) + } } } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/premium/ExtraFeaturesIntroductionCardFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/premium/ExtraFeaturesIntroductionCardFragment.kt index ec6d1559e..3ec1846db 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/premium/ExtraFeaturesIntroductionCardFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/premium/ExtraFeaturesIntroductionCardFragment.kt @@ -13,7 +13,9 @@ import org.mariotaku.twidere.R import org.mariotaku.twidere.constant.RESULT_NOT_PURCHASED import org.mariotaku.twidere.constant.RESULT_SERVICE_UNAVAILABLE import org.mariotaku.twidere.fragment.BaseSupportFragment -import org.mariotaku.twidere.model.analyzer.Purchase +import org.mariotaku.twidere.model.analyzer.PurchaseConfirm +import org.mariotaku.twidere.model.analyzer.PurchaseFinished +import org.mariotaku.twidere.model.analyzer.PurchaseIntroduction import org.mariotaku.twidere.util.Analyzer import org.mariotaku.twidere.util.premium.ExtraFeaturesService @@ -33,6 +35,7 @@ class ExtraFeaturesIntroductionCardFragment : BaseSupportFragment() { super.onActivityCreated(savedInstanceState) extraFeaturesService = ExtraFeaturesService.newInstance(context) purchaseButton.setOnClickListener { + Analyzer.log(PurchaseConfirm(PurchaseFinished.NAME_EXTRA_FEATURES)) startActivityForResult(extraFeaturesService.createPurchaseIntent(context), REQUEST_PURCHASE) } val restorePurchaseIntent = extraFeaturesService.createRestorePurchaseIntent(context) @@ -47,14 +50,17 @@ class ExtraFeaturesIntroductionCardFragment : BaseSupportFragment() { restorePurchaseButton.visibility = View.GONE restorePurchaseButton.setOnClickListener(null) } + if (savedInstanceState == null) { + Analyzer.log(PurchaseIntroduction(PurchaseFinished.NAME_EXTRA_FEATURES, "enhanced features dashboard")) + } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { when (requestCode) { REQUEST_PURCHASE -> { - Analyzer.log(Purchase.fromActivityResult(Purchase.NAME_EXTRA_FEATURES, resultCode, data)) when (resultCode) { Activity.RESULT_OK -> { + Analyzer.log(PurchaseFinished.create(PurchaseFinished.NAME_EXTRA_FEATURES, data)) activity?.recreate() } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseConfirm.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseConfirm.kt new file mode 100644 index 000000000..6b9c27f53 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseConfirm.kt @@ -0,0 +1,15 @@ +package org.mariotaku.twidere.model.analyzer + +import org.mariotaku.twidere.util.Analyzer + +/** + * Created by mariotaku on 2017/1/7. + */ + +data class PurchaseConfirm(val productName: String) : Analyzer.Event { + override val name: String = "Purchase Confirm" + override val accountType: String? = null + override fun forEachValues(action: (String, String?) -> Unit) { + action("Product Name", productName) + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/Purchase.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseFinished.kt similarity index 67% rename from twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/Purchase.kt rename to twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseFinished.kt index 764bfd92e..909629b20 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/Purchase.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseFinished.kt @@ -9,19 +9,12 @@ import org.mariotaku.twidere.util.Analyzer * Created by mariotaku on 2017/1/7. */ -data class Purchase(val productName: String) : Analyzer.Event { - override val name: String = "Purchase" +data class PurchaseFinished(val productName: String) : Analyzer.Event { + override val name: String = "Purchase Finished" override var accountType: String? = null - var resultCode: Int = Activity.RESULT_OK var price: Double = Double.NaN var currency: String? = null - override fun forEachValues(action: (String, String?) -> Unit) { - if (resultCode != Activity.RESULT_OK) { - action("Fail reason", getFailReason(resultCode)) - } - } - companion object { const val NAME_EXTRA_FEATURES = "Enhanced Features" @@ -35,9 +28,8 @@ data class Purchase(val productName: String) : Analyzer.Event { } } - fun fromActivityResult(name: String, resultCode: Int, data: Intent?): Purchase { - val result = Purchase(name) - result.resultCode = resultCode + fun create(name: String, data: Intent?): PurchaseFinished { + val result = PurchaseFinished(name) if (data != null) { result.price = data.getDoubleExtra(EXTRA_PRICE, Double.NaN) result.currency = data.getStringExtra(EXTRA_CURRENCY) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseIntroduction.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseIntroduction.kt new file mode 100644 index 000000000..3c6d9e519 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/PurchaseIntroduction.kt @@ -0,0 +1,16 @@ +package org.mariotaku.twidere.model.analyzer + +import org.mariotaku.twidere.util.Analyzer + +/** + * Created by mariotaku on 2017/1/7. + */ + +data class PurchaseIntroduction(val productName: String, val source: String) : Analyzer.Event { + override val name: String = "Purchase Introduction" + override val accountType: String? = null + override fun forEachValues(action: (String, String?) -> Unit) { + action("Product Name", productName) + action("Source", source) + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/StatusView.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/StatusView.kt index 12a3c0149..e712bd4a4 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/StatusView.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/analyzer/StatusView.kt @@ -3,6 +3,7 @@ package org.mariotaku.twidere.model.analyzer import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.extension.model.parcelableMediaTypeString import org.mariotaku.twidere.model.ParcelableMedia +import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.util.Analyzer /** @@ -15,8 +16,31 @@ data class StatusView( ) : Analyzer.Event { override val name: String = "Status View" + var type: String? = null + var source: String? = null override fun forEachValues(action: (String, String?) -> Unit) { - action("Media Type", parcelableMediaTypeString(mediaType)) + val mediaType = parcelableMediaTypeString(mediaType) + if (mediaType != null) { + action("Media Type", mediaType) + } + if (type != null) { + action("Type", type) + } + if (source != null) { + action("Source", source) + } + } + + companion object { + @JvmStatic + fun getStatusType(status: ParcelableStatus): String { + if (status.is_retweet) { + return "retweet" + } else if (status.is_quote) { + return "quote" + } + return "tweet" + } } }