Commit 3c539e62 authored by Matthew Finkel's avatar Matthew Finkel Committed by Pier Angelo Vendrame
Browse files

TB 40019 [android]: Disable Adjust metrics

Originally, fenix#40019.
parent 58c90df6
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -638,7 +638,6 @@ dependencies {
    implementation ComponentsDependencies.mozilla_appservices_merino

    implementation libs.accompanist.drawablepainter
    implementation libs.adjust
    implementation libs.androidx.activity
    implementation libs.androidx.annotation
    implementation libs.androidx.appcompat
+8 −162
Original line number Diff line number Diff line
@@ -6,11 +6,6 @@ package org.mozilla.fenix.components.metrics

import android.app.Application
import androidx.annotation.VisibleForTesting
import com.adjust.sdk.Adjust
import com.adjust.sdk.AdjustConfig
import com.adjust.sdk.AdjustEvent
import com.adjust.sdk.Constants.ADJUST_PREINSTALL_SYSTEM_PROPERTY_PATH
import com.adjust.sdk.LogLevel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -39,120 +34,16 @@ class AdjustMetricsService(

    @Suppress("CognitiveComplexMethod")
    override fun start() {
        logger.info("Started")

        CoroutineScope(Dispatchers.IO).launch {
            val settings = application.components.settings

            if ((BuildConfig.ADJUST_TOKEN.isNullOrBlank())) {
                logger.info("No adjust token defined")

                if (Config.channel.isReleased) {
                    throw IllegalStateException("No adjust token defined for release build")
                }

                return@launch
            }

            System.setProperty(ADJUST_PREINSTALL_SYSTEM_PROPERTY_PATH, "/preload/etc/adjust.preinstall")

            val config = AdjustConfig(
                application,
                BuildConfig.ADJUST_TOKEN,
                AdjustConfig.ENVIRONMENT_PRODUCTION,
                true,
            )
            config.enablePreinstallTracking()

            val distributionIdManager = application.components.distributionIdManager

            // If we skipped the marketing consent screen, enable COPPA compliance to prevent
            // personal identifiers from being shared with Adjust.
            when (distributionIdManager.getDistributionAdjustStartupStrategy()) {
                DistributionAdjustStartupStrategy.IMMEDIATE_WITH_COPPA ->
                    config.enableCoppaCompliance()

                DistributionAdjustStartupStrategy.IMMEDIATE_WITH_PLAY_STORE_KIDS ->
                    config.enablePlayStoreKidsCompliance()

                else -> {}
            }

            if (!alreadyKnown(settings)) {
                val timerId = AdjustAttribution.adjustAttributionTime.start()

                config.setOnAttributionChangedListener {
                    AdjustAttribution.adjustAttributionTime.stopAndAccumulate(timerId)

                    if (!it.network.isNullOrEmpty()) {
                        settings.adjustNetwork = it.network
                        AdjustAttribution.network.set(it.network)
                    }
                    if (!it.adgroup.isNullOrEmpty()) {
                        settings.adjustAdGroup = it.adgroup
                        AdjustAttribution.adgroup.set(it.adgroup)
                    }
                    if (!it.creative.isNullOrEmpty()) {
                        settings.adjustCreative = it.creative
                        AdjustAttribution.creative.set(it.creative)
                    }
                    if (!it.campaign.isNullOrEmpty()) {
                        settings.adjustCampaignId = it.campaign
                        AdjustAttribution.campaign.set(it.campaign)
                    }

                    triggerPing()
                    logger.info("Trigger ping")
                }
            }

            config.setLogLevel(LogLevel.SUPPRESS)

            config.disableFbIdReading()
            applyThirdPartySharingSettings(
                distribution = distributionIdManager.getDistribution(),
                isUserMetaAttributed = settings.isUserMetaAttributed,
            )

            // All configuration have to be done before this.
            Adjust.initSdk(config)
            Adjust.enable()
            logger.info("Adjust SDK enabled")
        }
        /* noop */
    }

    override fun stop() {
        logger.info("Stopped")

        Adjust.disable()
        Adjust.gdprForgetMe(application.applicationContext)
        /* noop */
    }

    @Suppress("TooGenericExceptionCaught")
    override fun track(event: Event) {
        logger.info("Track")

        CoroutineScope(dispatcher).launch {
            try {
                val tokenName = when (event) {
                    is Event.GrowthData -> event.tokenName
                    is Event.FirstWeekPostInstall -> event.tokenName
                }

                if (
                    (event is Event.GrowthData || event is Event.FirstWeekPostInstall) &&
                    storage.shouldTrack(event)
                ) {
                    Adjust.trackEvent(AdjustEvent(tokenName))
                    storage.updateSentState(event)
                    sendGleanEventAndPing(event)
                    logger.info("Update sent state $event")
                }
            } catch (e: Exception) {
                crashReporter.submitCaughtException(e)
                logger.info("Track threw an exception for $event")
            }
        }
        /* noop */
    }

    override fun shouldTrack(event: Event): Boolean =
@@ -178,28 +69,7 @@ class AdjustMetricsService(
            event: Event,
            conversionEventRecorder: ConversionEventRecorder = GleanConversionEventRecorder(),
        ) {
            when (event) {
                is Event.GrowthData.ConversionEvent1 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_1)
                is Event.GrowthData.ConversionEvent2 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_2)
                is Event.GrowthData.ConversionEvent3 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_3)
                is Event.GrowthData.ConversionEvent4 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_4)
                is Event.GrowthData.ConversionEvent5 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_5)
                is Event.GrowthData.ConversionEvent6 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_6)
                is Event.GrowthData.ConversionEvent7 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_7)
                is Event.FirstWeekPostInstall.ConversionEvent8 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_8)
                is Event.FirstWeekPostInstall.ConversionEvent9 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_9)
                is Event.FirstWeekPostInstall.ConversionEvent10 ->
                    conversionEventRecorder.recordConversionEvent(CONVERSION_EVENT_10)
            }
            /* noop */
        }

        /**
@@ -211,41 +81,17 @@ class AdjustMetricsService(
            isUserMetaAttributed: Boolean,
            controller: ThirdPartySharingController = AdjustThirdPartySharingController(),
        ) {
            when (distribution) {
                DistributionIdManager.Distribution.DEFAULT -> {
                    if (isUserMetaAttributed) {
                        controller.enableThirdPartySharingForPartner(META_PARTNER_ID)
                    } else {
                        controller.disableMetaThirdPartySharing()
                    }
                }

                DistributionIdManager.Distribution.AURA_001 -> {
                    controller.enableThirdPartySharingForPartner(AURA_PARTNER_ID)
                }

                DistributionIdManager.Distribution.VIVO_001,
                DistributionIdManager.Distribution.DT_001,
                DistributionIdManager.Distribution.DT_002,
                DistributionIdManager.Distribution.DT_003,
                DistributionIdManager.Distribution.XIAOMI_001,
                    -> {
                    controller.disableAllThirdPartySharing()
                }
                // Do not add an else branch here. All distributions should be handled deliberately.
            }
            /* noop */
        }

        @VisibleForTesting
        internal fun alreadyKnown(settings: Settings): Boolean {
            return settings.adjustCampaignId.isNotEmpty() || settings.adjustNetwork.isNotEmpty() ||
                settings.adjustCreative.isNotEmpty() || settings.adjustAdGroup.isNotEmpty()
            /* noop */
            return false
        }

        private fun triggerPing() {
            CoroutineScope(Dispatchers.IO).launch {
                Pings.adjustAttribution.submit()
            }
            /* noop */
        }
    }
}