Skip to content

Commit

Permalink
MBL-1278: Analytics on late pledge checkout screen (#2001)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkariang authored Apr 2, 2024
1 parent 0d18f6f commit 7c34d92
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/kickstarter/libs/AnalyticEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.kickstarter.libs.utils.EventContextValues.CtaContextName.COMMENT_POST
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.DISCOVER
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.DISCOVER_FILTER
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.DISCOVER_SORT
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LATE_PLEDGE
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LOGIN_INITIATE
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LOGIN_OR_SIGN_UP
import com.kickstarter.libs.utils.EventContextValues.CtaContextName.LOGIN_SUBMIT
Expand Down Expand Up @@ -412,6 +413,20 @@ class AnalyticEvents(trackingClients: List<TrackingClientType?>) {
client.track(CTA_CLICKED.eventName, props)
}

/**
* Sends data associated with the submit CTA click event to the client.
*
* @param checkoutData: The checkout data.
* @param pledgeData: The selected pledge data.
*/
fun trackLatePledgeSubmitCTA(checkoutData: CheckoutData, pledgeData: PledgeData) {
val props: HashMap<String, Any> = hashMapOf(CONTEXT_CTA.contextName to LATE_PLEDGE.contextName)
props[CONTEXT_TYPE.contextName] = CREDIT_CARD.contextName
props[CONTEXT_PAGE.contextName] = CHECKOUT.contextName
props.putAll(AnalyticEventsUtils.checkoutDataProperties(checkoutData, pledgeData, client.loggedInUser()))
client.track(CTA_CLICKED.eventName, props)
}

/**
* Sends data associated with page view event to the client.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class EventContextValues {
SIGN_UP_INITIATE("sign_up_initiate"),
COMMENT_POST("comment_post"),
PROJECT_SELECT("project_select"),
LATE_PLEDGE("late_pledge"),
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,21 @@ class ProjectPageActivity :
easing = FastOutSlowInEasing
)
)

if (currentPage == 3) {
latePledgeCheckoutViewModel.sendPageViewedEvent(
projectData,
addOns,
currentUserShippingRule,
shippingAmount,
totalAmount,
totalBonusSupportAmount
)
}
}
}

var selectedReward: Reward? = null

ProjectPledgeButtonAndFragmentContainer(
expanded = expanded,
onContinueClicked = { checkoutFlowViewModel.onBackThisProjectClicked() },
Expand Down Expand Up @@ -645,6 +655,7 @@ class ProjectPageActivity :
addOnsViewModel.userRewardSelection(reward)
rewardsSelectionViewModel.onUserRewardSelection(reward)
confirmDetailsViewModel.onUserSelectedReward(reward)
latePledgeCheckoutViewModel.userRewardSelection(reward)
},
onAddOnAddedOrRemoved = { updateAddOnRewardCount ->
selectedAddOnsMap[updateAddOnRewardCount.keys.first()] =
Expand Down Expand Up @@ -677,7 +688,10 @@ class ProjectPageActivity :
onBonusSupportPlusClicked = { confirmDetailsViewModel.incrementBonusSupport() },
selectedAddOnsMap = selectedAddOnsMap,
onPledgeCtaClicked = { selectedCard ->
latePledgeCheckoutViewModel.onPledgeButtonClicked(selectedCard = selectedCard, project = projectData.project(), totalAmount = totalAmount)
selectedCard?.apply {
latePledgeCheckoutViewModel.sendSubmitCTAEvent(projectData, addOns, currentUserShippingRule, shippingAmount, totalAmount, totalBonusSupportAmount)
latePledgeCheckoutViewModel.onPledgeButtonClicked(selectedCard = selectedCard, project = projectData.project(), totalAmount = totalAmount)
}
},
onAddPaymentMethodClicked = {
latePledgeCheckoutViewModel.onAddNewCardClicked(project = projectData.project(), totalAmount = totalAmount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.kickstarter.libs.Environment
import com.kickstarter.libs.utils.extensions.isNotNull
import com.kickstarter.models.Checkout
import com.kickstarter.models.CreatePaymentIntentInput
import com.kickstarter.models.Project
import com.kickstarter.models.Reward
import com.kickstarter.models.ShippingRule
import com.kickstarter.models.StoredCard
import com.kickstarter.ui.data.CheckoutData
import com.kickstarter.ui.data.PledgeData
import com.kickstarter.ui.data.ProjectData
import com.stripe.android.ApiResultCallback
import com.stripe.android.Stripe
import com.stripe.android.confirmPaymentIntent
Expand All @@ -29,6 +35,7 @@ import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.asFlow
import type.CreditCardPaymentType

data class LatePledgeCheckoutUIState(
val storeCards: List<StoredCard> = listOf(),
Expand All @@ -39,6 +46,7 @@ data class LatePledgeCheckoutUIState(
class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {

private val apolloClient = requireNotNull(environment.apolloClientV2())
private val analytics = requireNotNull(environment.analytics())

private var storedCards: List<StoredCard> = listOf()
private var userEmail: String = ""
Expand All @@ -50,6 +58,7 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
private var newStoredCard: StoredCard? = null
private var errorAction: (message: String?) -> Unit = {}

private var selectedReward: Reward? = null
private var mutableLatePledgeCheckoutUIState = MutableStateFlow(LatePledgeCheckoutUIState())
val latePledgeCheckoutUIState: StateFlow<LatePledgeCheckoutUIState>
get() = mutableLatePledgeCheckoutUIState
Expand Down Expand Up @@ -263,6 +272,25 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
}.collect()
}

private fun createCheckoutData(shippingAmount: Double, total: Double, bonusAmount: Double?, checkout: Checkout? = null): CheckoutData {
return CheckoutData.builder()
.amount(total)
.id(checkout?.id())
.paymentType(CreditCardPaymentType.CREDIT_CARD)
.bonusAmount(bonusAmount)
.shippingAmount(shippingAmount)
.build()
}

private fun createPledgeData(projectData: ProjectData, addOns: List<Reward>, shippingRule: ShippingRule, reward: Reward): PledgeData {
return PledgeData.builder()
.projectData(projectData)
.addOns(addOns)
.shippingRule(shippingRule)
.reward(reward)
.build()
}

private suspend fun emitCurrentState(isLoading: Boolean = false) {
mutableLatePledgeCheckoutUIState.emit(
LatePledgeCheckoutUIState(
Expand All @@ -273,6 +301,47 @@ class LatePledgeCheckoutViewModel(val environment: Environment) : ViewModel() {
)
}

fun sendPageViewedEvent(
projectData: ProjectData,
addOns: List<Reward>,
currentUserShippingRule: ShippingRule,
shippingAmount: Double,
totalAmount: Double,
totalBonusSupportAmount: Double
) {
this.selectedReward?.let {
val pledgeData = createPledgeData(projectData, addOns, currentUserShippingRule, it)
val checkOutData =
createCheckoutData(shippingAmount, totalAmount, totalBonusSupportAmount)
this@LatePledgeCheckoutViewModel.analytics.trackCheckoutScreenViewed(
checkoutData = checkOutData,
pledgeData = pledgeData
)
}
}
fun sendSubmitCTAEvent(
projectData: ProjectData,
addOns: List<Reward>,
currentUserShippingRule: ShippingRule,
shippingAmount: Double,
totalAmount: Double,
totalBonusSupportAmount: Double
) {
this.selectedReward?.let {
val pledgeData = createPledgeData(projectData, addOns, currentUserShippingRule, it)
val checkOutData =
createCheckoutData(shippingAmount, totalAmount, totalBonusSupportAmount)
this@LatePledgeCheckoutViewModel.analytics.trackLatePledgeSubmitCTA(
checkoutData = checkOutData,
pledgeData = pledgeData
)
}
}

fun userRewardSelection(reward: Reward) {
this.selectedReward = reward
}

class Factory(private val environment: Environment) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.kickstarter.viewmodels

import com.kickstarter.KSRobolectricTestCase
import com.kickstarter.libs.Environment
import com.kickstarter.libs.utils.EventName
import com.kickstarter.mock.factories.ProjectDataFactory
import com.kickstarter.mock.factories.ProjectFactory
import com.kickstarter.mock.factories.RewardFactory
import com.kickstarter.mock.factories.ShippingRuleFactory
import com.kickstarter.viewmodels.projectpage.LatePledgeCheckoutViewModel
import org.junit.Test

class LatePledgeCheckoutViewModelTest : KSRobolectricTestCase() {

private lateinit var viewModel: LatePledgeCheckoutViewModel

private fun setUpEnvironment(environment: Environment) {
viewModel = LatePledgeCheckoutViewModel.Factory(environment).create(LatePledgeCheckoutViewModel::class.java)
}

@Test
fun `test send PageViewed event`() {
setUpEnvironment(environment())

val rw = RewardFactory.rewardWithShipping()
val project = ProjectFactory.project().toBuilder().rewards(listOf(rw)).build()
val addOns = listOf(rw, rw, rw)
val rule = ShippingRuleFactory.germanyShippingRule()
val shipAmount = 3.0
val totalAmount = 300.0
val bonusAmount = 5.0

val projectData = ProjectDataFactory.project(project = project)

viewModel.userRewardSelection(rw)
viewModel.sendPageViewedEvent(
projectData,
addOns,
rule,
shipAmount,
totalAmount,
bonusAmount
)

this.segmentTrack.assertValue(EventName.PAGE_VIEWED.eventName)
}

@Test
fun `test send CTAClicked event`() {
setUpEnvironment(environment())

val rw = RewardFactory.rewardWithShipping()
val project = ProjectFactory.project().toBuilder().rewards(listOf(rw)).build()
val addOns = listOf(rw, rw, rw)
val rule = ShippingRuleFactory.germanyShippingRule()
val shipAmount = 3.0
val totalAmount = 300.0
val bonusAmount = 5.0

val projectData = ProjectDataFactory.project(project = project)
viewModel.userRewardSelection(rw)
viewModel.sendSubmitCTAEvent(
projectData,
addOns,
rule,
shipAmount,
totalAmount,
bonusAmount
)

this.segmentTrack.assertValue(EventName.CTA_CLICKED.eventName)
}
}

0 comments on commit 7c34d92

Please sign in to comment.