Skip to content

Commit

Permalink
MBL-1278: Analytic events for rewards (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkariang authored Apr 1, 2024
1 parent bfa7a49 commit 400bba3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ProjectPageActivity :
private lateinit var checkoutViewModelFactory: CheckoutFlowViewModel.Factory
private val checkoutFlowViewModel: CheckoutFlowViewModel by viewModels { checkoutViewModelFactory }

private val rewardsSelectionViewModelFactory = RewardsSelectionViewModel.Factory()
private lateinit var rewardsSelectionViewModelFactory: RewardsSelectionViewModel.Factory
private val rewardsSelectionViewModel: RewardsSelectionViewModel by viewModels { rewardsSelectionViewModelFactory }

private lateinit var confirmDetailsViewModelFactory: ConfirmDetailsViewModel.Factory
Expand Down Expand Up @@ -167,6 +167,7 @@ class ProjectPageActivity :
val environment = this.getEnvironment()?.let { env ->
viewModelFactory = ProjectPageViewModel.Factory(env)
checkoutViewModelFactory = CheckoutFlowViewModel.Factory(env)
rewardsSelectionViewModelFactory = RewardsSelectionViewModel.Factory(env)
confirmDetailsViewModelFactory = ConfirmDetailsViewModel.Factory(env)
addOnsViewModelFactory = AddOnsViewModel.Factory(env)
latePledgeCheckoutViewModelFactory = LatePledgeCheckoutViewModel.Factory(env)
Expand Down Expand Up @@ -498,11 +499,11 @@ class ProjectPageActivity :
val currentPage = flowUIState.currentPage

val rewardSelectionUIState by rewardsSelectionViewModel.rewardSelectionUIState.collectAsStateWithLifecycle()

val projectData = rewardSelectionUIState.project
val indexOfBackedReward = rewardSelectionUIState.initialRewardIndex
val rewardsList = rewardSelectionUIState.rewardList
val showRewardCarouselAlertDialog = rewardSelectionUIState.showAlertDialog
rewardsSelectionViewModel.sendEvent(expanded, currentPage, projectData)

LaunchedEffect(Unit) {
rewardsSelectionViewModel.flowUIRequest.collect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.Pair
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.kickstarter.libs.Environment
import com.kickstarter.libs.utils.extensions.isBacked
import com.kickstarter.mock.factories.RewardFactory
import com.kickstarter.models.Backing
Expand Down Expand Up @@ -31,8 +32,9 @@ data class RewardSelectionUIState(
val showAlertDialog: Boolean = false
)

class RewardsSelectionViewModel : ViewModel() {
class RewardsSelectionViewModel(environment: Environment) : ViewModel() {

private val analytics = requireNotNull(environment.analytics())
private lateinit var currentProjectData: ProjectData
private var previousUserBacking: Backing? = null
private var previouslyBackedReward: Reward? = null
Expand Down Expand Up @@ -69,6 +71,8 @@ class RewardsSelectionViewModel : ViewModel() {
val pledgeDataAndReason = pledgeDataAndPledgeReason(currentProjectData, reward)
newUserReward = pledgeDataAndReason.first.reward()

analytics.trackSelectRewardCTA(pledgeDataAndReason.first)

when (pledgeDataAndReason.second) {
PledgeReason.UPDATE_REWARD -> {
if (previouslyBackedReward?.hasAddons() == true && !newUserReward.hasAddons())
Expand Down Expand Up @@ -163,6 +167,11 @@ class RewardsSelectionViewModel : ViewModel() {
return 0
}

fun sendEvent(expanded: Boolean, currentPage: Int, projectData: ProjectData) {
if (expanded && currentPage == 0) {
analytics.trackRewardsCarouselViewed(projectData = projectData)
}
}
private suspend fun emitCurrentState(
showAlertDialog: Boolean = false
) {
Expand All @@ -176,10 +185,10 @@ class RewardsSelectionViewModel : ViewModel() {
)
}

class Factory :
class Factory(private val environment: Environment) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return RewardsSelectionViewModel() as T
return RewardsSelectionViewModel(environment = environment) as T
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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.models.Backing
import com.kickstarter.models.Project
import com.kickstarter.models.Reward
Expand All @@ -19,9 +23,9 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {

private lateinit var viewModel: RewardsSelectionViewModel

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -97,6 +101,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 1, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -141,6 +147,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 2, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -189,6 +197,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 1, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -237,6 +247,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 1, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -290,6 +302,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
showAlertDialog = true
)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -338,6 +352,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 2, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -394,6 +410,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
showAlertDialog = true
)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -458,6 +476,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 1, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -522,6 +542,8 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
flowState.last(),
FlowUIState(currentPage = 2, expanded = true)
)

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

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -591,5 +613,30 @@ class RewardsSelectionViewModelTest : KSRobolectricTestCase() {
showAlertDialog = false
)
)

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

@Test
fun `test send analytic event trackRewardsCarouselViewed() when the currentPage is rewards and is expanded mode`() {
createViewModel()

val projectData = ProjectDataFactory.project(ProjectFactory.project())

viewModel.sendEvent(expanded = true, currentPage = 0, projectData)
this@RewardsSelectionViewModelTest.segmentTrack.assertValue(EventName.PAGE_VIEWED.eventName)
}

@Test
fun `test analytic event trackRewardsCarouselViewed() not sent when the currentPage is not rewards or not expanded`() {
createViewModel()

val projectData = ProjectDataFactory.project(ProjectFactory.project())

viewModel.sendEvent(expanded = true, currentPage = 1, projectData)
this@RewardsSelectionViewModelTest.segmentTrack.assertNoValues()

viewModel.sendEvent(expanded = false, currentPage = 0, projectData)
this@RewardsSelectionViewModelTest.segmentTrack.assertNoValues()
}
}

0 comments on commit 400bba3

Please sign in to comment.