Skip to content

Handle experiment metadata from subscriptionSelected message #5930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ interface SubscriptionsManager {
suspend fun getSubscriptionOffer(): List<SubscriptionOffer>

/**
* Launches the purchase flow for a given combination of plan id and offer id
* Launches the purchase flow for a given combination of plan id, offer id and front-end experiment details
*/
suspend fun purchase(
activity: Activity,
planId: String,
offerId: String?,
experimentName: String?,
experimentCohort: String?,
)

/**
Expand Down Expand Up @@ -271,6 +273,9 @@ class RealSubscriptionsManager @Inject constructor(
private var removeExpiredSubscriptionOnCancelledPurchase: Boolean = false
private var purchaseFlowStartedUsingRestoredAccount: Boolean = false

// Indicates whether the user is part of any FE experiment at the time of purchase
private var experimentAssigned: Experiment? = null

override suspend fun isSignedIn(): Boolean {
return isSignedInV1() || isSignedInV2()
}
Expand Down Expand Up @@ -424,9 +429,14 @@ class RealSubscriptionsManager @Inject constructor(
purchaseToken: String,
): Boolean {
var experimentName: String? = null
val cohort: String? = privacyProFeature.get().privacyProFreeTrialJan25().getCohort()?.name
if (cohort != null) {
var cohort: String? = privacyProFeature.get().privacyProFreeTrialJan25().getCohort()?.name
if (cohort != null) { // Android experiment
experimentName = "privacyProFreeTrialJan25"
} else {
experimentAssigned?.let { // FE experiment details
cohort = it.cohort
experimentName = it.name
}
}
return try {
val confirmationResponse = subscriptionsService.confirm(
Expand Down Expand Up @@ -807,6 +817,8 @@ class RealSubscriptionsManager @Inject constructor(
activity: Activity,
planId: String,
offerId: String?,
experimentName: String?,
experimentCohort: String?,
) {
try {
_currentPurchaseState.emit(CurrentPurchase.PreFlowInProgress)
Expand Down Expand Up @@ -860,6 +872,12 @@ class RealSubscriptionsManager @Inject constructor(
}
}

experimentAssigned = if (experimentCohort.isNullOrEmpty() || experimentName.isNullOrEmpty()) {
null
} else {
Experiment(experimentName, experimentCohort)
}

purchaseFlowStartedUsingRestoredAccount = restoredAccount

logcat(LogPriority.DEBUG) { "Subs: external id is ${authRepository.getAccount()!!.externalId}" }
Expand Down Expand Up @@ -1120,3 +1138,8 @@ data class ValidatedTokenPair(
val refreshToken: String,
val refreshTokenClaims: RefreshTokenClaims,
)

data class Experiment(
val name: String,
val cohort: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,26 @@ class SubscriptionWebViewViewModel @Inject constructor(
viewModelScope.launch(dispatcherProvider.io()) {
val id = runCatching { data?.getString("id") }.getOrNull()
val offerId = runCatching { data?.getString("offerId") }.getOrNull()
val experimentName = runCatching { data?.getJSONObject("experiment")?.getString("name") }.getOrNull()
val experimentCohort = runCatching { data?.getJSONObject("experiment")?.getString("cohort") }.getOrNull()
if (id.isNullOrBlank()) {
pixelSender.reportPurchaseFailureOther()
_currentPurchaseViewState.emit(currentPurchaseViewState.value.copy(purchaseState = Failure))
} else {
command.send(SubscriptionSelected(id, offerId))
command.send(SubscriptionSelected(id, offerId, experimentName, experimentCohort))
}
}
}

fun purchaseSubscription(activity: Activity, planId: String, offerId: String?) {
fun purchaseSubscription(
activity: Activity,
planId: String,
offerId: String?,
experimentName: String?,
experimentCohort: String?,
) {
viewModelScope.launch(dispatcherProvider.io()) {
subscriptionsManager.purchase(activity, planId, offerId)
subscriptionsManager.purchase(activity, planId, offerId, experimentName, experimentCohort)
}
}

Expand Down Expand Up @@ -411,6 +419,8 @@ class SubscriptionWebViewViewModel @Inject constructor(
data class SubscriptionSelected(
val id: String,
val offerId: String?,
val experimentName: String?,
val experimentCohort: String?,
) : Command()
data object RestoreSubscription : Command()
data object GoToITR : Command()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class SubscriptionsWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
is BackToSettings, BackToSettingsActivateSuccess -> backToSettings()
is SendJsEvent -> sendJsEvent(command.event)
is SendResponseToJs -> sendResponseToJs(command.data)
is SubscriptionSelected -> selectSubscription(command.id, command.offerId)
is SubscriptionSelected -> selectSubscription(command.id, command.offerId, command.experimentName, command.experimentCohort)
is RestoreSubscription -> restoreSubscription()
is GoToITR -> goToITR()
is GoToPIR -> goToPIR()
Expand Down Expand Up @@ -517,8 +517,13 @@ class SubscriptionsWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
.show()
}

private fun selectSubscription(id: String, offerId: String?) {
viewModel.purchaseSubscription(this, id, offerId)
private fun selectSubscription(
id: String,
offerId: String?,
experimentName: String?,
experimentCohort: String?,
) {
viewModel.purchaseSubscription(this, id, offerId, experimentName, experimentCohort)
}

private fun sendResponseToJs(data: JsCallbackData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenCreateAccountSucceeds()
val accountExternalId = authDataStore.externalId

subscriptionsManager.purchase(activity = mock(), planId = "", offerId = null)
purchase()

if (authApiV2Enabled) {
verify(authClient).authorize(any())
Expand All @@ -337,7 +337,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenUserIsNotSignedIn()
givenCreateAccountSucceeds()

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

if (authApiV2Enabled) {
verify(authClient).authorize(any())
Expand All @@ -355,7 +355,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
whenever(emailManager.getToken()).thenReturn("emailToken")
givenUserIsNotSignedIn()

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

verify(authService).createAccount("Bearer emailToken")
}
Expand All @@ -366,7 +366,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenCreateAccountFails()

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
assertTrue(awaitItem() is CurrentPurchase.Failure)
cancelAndConsumeRemainingEvents()
Expand All @@ -380,7 +380,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenValidateTokenSucceedsNoEntitlements()
givenAccessTokenSucceeds()

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

verify(playBillingManager).launchBillingFlow(any(), any(), externalId = eq("1234"), isNull())
}
Expand All @@ -393,7 +393,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenSubscriptionSucceedsWithoutEntitlements(status = "Expired")
givenAccessTokenSucceeds()

subscriptionsManager.purchase(mock(), "", null)
purchase()

verify(playBillingManager).launchBillingFlow(any(), any(), externalId = eq("1234"), isNull())
}
Expand All @@ -407,7 +407,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenAccessTokenSucceeds()

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
verify(playBillingManager, never()).launchBillingFlow(any(), any(), any(), isNull())
assertTrue(awaitItem() is CurrentPurchase.Recovered)
Expand All @@ -423,7 +423,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenStoreLoginFails()

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
assertTrue(awaitItem() is CurrentPurchase.Failure)
cancelAndConsumeRemainingEvents()
Expand All @@ -436,7 +436,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {

givenUserIsSignedIn()

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

verify(authService).validateToken(any())
}
Expand All @@ -447,7 +447,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenSubscriptionSucceedsWithoutEntitlements(status = "Expired")

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
verify(playBillingManager).launchBillingFlow(any(), any(), externalId = eq("1234"), isNull())
assertTrue(awaitItem() is CurrentPurchase.PreFlowFinished)
Expand All @@ -461,7 +461,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenCreateAccountFails()

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
assertTrue(awaitItem() is CurrentPurchase.Failure)
cancelAndConsumeRemainingEvents()
Expand All @@ -473,7 +473,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenUserIsSignedIn()
givenValidateTokenFails("failure")

subscriptionsManager.purchase(mock(), "", null)
purchase()

verify(authService, never()).createAccount(any())
}
Expand All @@ -485,7 +485,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenSubscriptionSucceedsWithoutEntitlements()
givenAccessTokenSucceeds()

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
if (authApiV2Enabled) {
assertEquals(FAKE_ACCESS_TOKEN_V2, authDataStore.accessTokenV2)
assertEquals(FAKE_REFRESH_TOKEN_V2, authDataStore.refreshTokenV2)
Expand All @@ -507,7 +507,8 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenSubscriptionSucceedsWithoutEntitlements()
givenAccessTokenSucceeds()

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

subscriptionsManager.isSignedIn.test {
assertTrue(awaitItem())
if (authApiV2Enabled) {
Expand All @@ -530,7 +531,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenUserIsSignedIn()
givenSubscriptionFails(httpResponseCode = 400)

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

verify(playBillingManager).launchBillingFlow(any(), any(), any(), isNull())
}
Expand All @@ -540,7 +541,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenUserIsSignedIn()
givenSubscriptionFails(httpResponseCode = 404)

subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()

verify(playBillingManager).launchBillingFlow(any(), any(), any(), isNull())
}
Expand Down Expand Up @@ -1171,7 +1172,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenAccessTokenSucceeds()

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
assertTrue(awaitItem() is CurrentPurchase.Recovered)

Expand All @@ -1197,7 +1198,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
whenever(playBillingManager.purchaseState).thenReturn(purchaseState)

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
assertTrue(awaitItem() is CurrentPurchase.PreFlowFinished)

Expand Down Expand Up @@ -1241,7 +1242,7 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
givenCreateAccountFails()

subscriptionsManager.currentPurchaseState.test {
subscriptionsManager.purchase(mock(), planId = "", offerId = null)
purchase()
assertTrue(awaitItem() is CurrentPurchase.PreFlowInProgress)
assertTrue(awaitItem() is CurrentPurchase.Failure)

Expand Down Expand Up @@ -1758,6 +1759,21 @@ class RealSubscriptionsManagerTest(private val authApiV2Enabled: Boolean) {
whenever(playBillingManager.products).thenReturn(listOf(productDetails))
}

private suspend fun purchase(
planId: String = "",
offerId: String? = null,
experimentName: String? = null,
experimentCohort: String? = null,
) {
subscriptionsManager.purchase(
mock(),
planId = planId,
offerId = offerId,
experimentCohort = experimentCohort,
experimentName = experimentName,
)
}

@SuppressLint("DenyListedApi")
private fun givenIsLaunchedRow(value: Boolean) {
privacyProFeature.isLaunchedROW().setRawStoredState(State(remoteEnableState = value))
Expand Down
Loading