Skip to content
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

Privacy Pro Free Trials Experiment #5686

Merged
merged 16 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from 15 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 @@ -94,3 +94,7 @@ enum class SubscriptionStatus(val statusName: String) {
UNKNOWN("Unknown"),
WAITING("Waiting"),
}

enum class ActiveOfferType {
TRIAL, UNKNOWN
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.duckduckgo.feature.toggles.api.RemoteFeatureStoreNamed
import com.duckduckgo.feature.toggles.api.Toggle
import com.duckduckgo.feature.toggles.api.Toggle.InternalAlwaysEnabled
import com.duckduckgo.feature.toggles.api.Toggle.State
import com.duckduckgo.feature.toggles.api.Toggle.State.CohortName
import com.duckduckgo.navigation.api.GlobalActivityStarter
import com.duckduckgo.subscriptions.api.Product
import com.duckduckgo.subscriptions.api.SubscriptionStatus
Expand Down Expand Up @@ -160,6 +161,14 @@ interface PrivacyProFeature {
// Kill switch
@Toggle.DefaultValue(true)
fun featuresApi(): Toggle

@Toggle.DefaultValue(false)
fun privacyProFreeTrialJan25(): Toggle

enum class Cohorts(override val cohortName: String) : CohortName {
CONTROL("control"),
TREATMENT("treatment"),
}
}

@ContributesBinding(AppScope::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object SubscriptionsConstants {
const val MONTHLY_PLAN_ROW = "ddg-privacy-pro-monthly-renews-row"

// List of offers
const val MONTHLY_FREE_TRIAL_OFFER_US = "ddg-privacy-pro-sandbox-freetrial-monthly-renews-us"
const val YEARLY_FREE_TRIAL_OFFER_US = "ddg-privacy-pro-sandbox-freetrial-yearly-renews-us"
const val MONTHLY_FREE_TRIAL_OFFER_US = "ddg-privacy-pro-freetrial-monthly-renews-us"
const val YEARLY_FREE_TRIAL_OFFER_US = "ddg-privacy-pro-freetrial-yearly-renews-us"

// List of features
const val LEGACY_FE_NETP = "vpn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.duckduckgo.autofill.api.email.EmailManager
import com.duckduckgo.common.utils.CurrentTimeProvider
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.subscriptions.api.ActiveOfferType
import com.duckduckgo.subscriptions.api.Product
import com.duckduckgo.subscriptions.api.SubscriptionStatus
import com.duckduckgo.subscriptions.api.SubscriptionStatus.AUTO_RENEWABLE
Expand Down Expand Up @@ -110,7 +111,7 @@ interface SubscriptionsManager {
suspend fun purchase(
activity: Activity,
planId: String,
offerId: String? = null,
offerId: String?,
)

/**
Expand Down Expand Up @@ -214,6 +215,13 @@ interface SubscriptionsManager {
suspend fun getPortalUrl(): String?

suspend fun canSupportEncryption(): Boolean

/**
* Checks whether the user has previously used a trial.
*
* @return [Boolean] indicating if the user has had a trial before.
*/
suspend fun hadTrial(): Boolean
}

@SingleInstanceIn(AppScope::class)
Expand Down Expand Up @@ -327,6 +335,14 @@ class RealSubscriptionsManager @Inject constructor(

override suspend fun canSupportEncryption(): Boolean = authRepository.canSupportEncryption()

override suspend fun hadTrial(): Boolean {
return try {
return subscriptionsService.offerStatus().hadTrial
} catch (e: Exception) {
false
}
}

override suspend fun getAccount(): Account? = authRepository.getAccount()

override suspend fun getPortalUrl(): String? {
Expand Down Expand Up @@ -406,11 +422,18 @@ class RealSubscriptionsManager @Inject constructor(
packageName: String,
purchaseToken: String,
): Boolean {
var experimentName: String? = null
val cohort: String? = privacyProFeature.get().privacyProFreeTrialJan25().getCohort()?.name
if (cohort != null) {
experimentName = "privacyProFreeTrialJan25"
}
return try {
val confirmationResponse = subscriptionsService.confirm(
ConfirmationBody(
packageName = packageName,
purchaseToken = purchaseToken,
experimentName = experimentName,
experimentCohort = cohort,
),
)

Expand All @@ -420,6 +443,7 @@ class RealSubscriptionsManager @Inject constructor(
expiresOrRenewsAt = confirmationResponse.subscription.expiresOrRenewsAt,
status = confirmationResponse.subscription.status.toStatus(),
platform = confirmationResponse.subscription.platform,
activeOffers = confirmationResponse.subscription.activeOffers.map { it.type.toActiveOfferType() },
)

authRepository.setSubscription(subscription)
Expand All @@ -437,6 +461,12 @@ class RealSubscriptionsManager @Inject constructor(
}

if (subscription.isActive()) {
// Free Trial experiment metrics
if (confirmationResponse.subscription.productId.contains("monthly-renews-us")) {
pixelSender.reportFreeTrialOnSubscriptionStartedMonthly()
} else if (confirmationResponse.subscription.productId.contains("yearly-renews-us")) {
pixelSender.reportFreeTrialOnSubscriptionStartedYearly()
}
pixelSender.reportPurchaseSuccess()
pixelSender.reportSubscriptionActivated()
emitEntitlementsValues()
Expand Down Expand Up @@ -517,6 +547,7 @@ class RealSubscriptionsManager @Inject constructor(
expiresOrRenewsAt = subscription.expiresOrRenewsAt,
status = subscription.status.toStatus(),
platform = subscription.platform,
activeOffers = subscription.activeOffers.map { it.type.toActiveOfferType() },
),
)
authRepository.setEntitlements(accountData.entitlements.toEntitlements())
Expand Down Expand Up @@ -582,6 +613,7 @@ class RealSubscriptionsManager @Inject constructor(
expiresOrRenewsAt = subscription.expiresOrRenewsAt,
status = subscription.status.toStatus(),
platform = subscription.platform,
activeOffers = subscription.activeOffers.map { it.type.toActiveOfferType() },
),
)

Expand Down Expand Up @@ -814,6 +846,7 @@ class RealSubscriptionsManager @Inject constructor(
activity = activity,
planId = planId,
externalId = authRepository.getAccount()!!.externalId,
offerId = offerId,
)
} catch (e: Exception) {
val error = extractError(e)
Expand Down Expand Up @@ -1009,6 +1042,13 @@ fun String.toStatus(): SubscriptionStatus {
}
}

fun String.toActiveOfferType(): ActiveOfferType {
return when (this) {
"Trial" -> ActiveOfferType.TRIAL
else -> ActiveOfferType.UNKNOWN
}
}

sealed class CurrentPurchase {
data object PreFlowInProgress : CurrentPurchase()
data object PreFlowFinished : CurrentPurchase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ interface PlayBillingManager {
activity: Activity,
planId: String,
externalId: String,
offerId: String? = null,
offerId: String?,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.subscriptions.impl.freetrial

import android.content.SharedPreferences
import androidx.core.content.edit
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.data.store.api.SharedPreferencesProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.feature.toggles.api.PixelDefinition
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
import kotlinx.coroutines.withContext

interface FreeTrialExperimentDataStore {
/**
* @return number of times paywall has been displayed to user
*/
var paywallImpressions: Int

/**
* Increases the count of paywall impressions
*/
suspend fun increaseMetricForPaywallImpressions()

/**
* Returns the value [String] for the given pixel [definition]
*/
suspend fun getMetricForPixelDefinition(definition: PixelDefinition): String?

/**
* Increases the count of paywall impressions for the given [definition]
*/
suspend fun increaseMetricForPixelDefinition(
definition: PixelDefinition,
value: String,
): String?
}

@ContributesBinding(AppScope::class)
class FreeTrialExperimentDataStoreImpl @Inject constructor(
private val sharedPreferencesProvider: SharedPreferencesProvider,
private val dispatcherProvider: DispatcherProvider,
) : FreeTrialExperimentDataStore {

private val preferences: SharedPreferences by lazy {
sharedPreferencesProvider.getSharedPreferences(
FILENAME,
)
}

override var paywallImpressions: Int
get() = preferences.getInt(KEY_PAYWALL_IMPRESSIONS, 0)
set(impressions) = preferences.edit { putInt(KEY_PAYWALL_IMPRESSIONS, impressions) }

override suspend fun increaseMetricForPaywallImpressions() {
withContext(dispatcherProvider.io()) {
paywallImpressions += 1
}
}

override suspend fun getMetricForPixelDefinition(definition: PixelDefinition): String? {
val tag = "$definition"
return withContext(dispatcherProvider.io()) {
preferences.getString(tag, null)
}
}

override suspend fun increaseMetricForPixelDefinition(
definition: PixelDefinition,
value: String,
): String? =
withContext(dispatcherProvider.io()) {
val tag = "$definition"
preferences.edit {
putString(tag, value)
apply()
}
preferences.getString(tag, null)
}

companion object {
private const val FILENAME = "com.duckduckgo.subscriptions.freetrial.store"
private const val KEY_PAYWALL_IMPRESSIONS = "PAYWALL_IMPRESSIONS"
}
}
Loading
Loading