-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send app version on search pixel (#5287)
Task/Issue URL: https://app.asana.com/0/1174433894299346/1208776505075744/f ### Description We want to add a new temporary triggered on searches where we’ll send the app version. There are few conditions, please refer to the task for more info. Testing scenarios -> https://app.asana.com/0/1174433894299346/1208801300909712/f
- Loading branch information
Showing
11 changed files
with
266 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/duckduckgo/app/browser/trafficquality/AndroidAppVersionPixelSender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.app.browser.trafficquality | ||
|
||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.pixels.AppPixelName | ||
import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin | ||
import com.duckduckgo.app.statistics.pixels.Pixel | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
@ContributesMultibinding(AppScope::class) | ||
class AndroidAppVersionPixelSender @Inject constructor( | ||
private val appVersionProvider: QualityAppVersionProvider, | ||
private val pixel: Pixel, | ||
@AppCoroutineScope private val coroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
) : AtbLifecyclePlugin { | ||
|
||
override fun onSearchRetentionAtbRefreshed(oldAtb: String, newAtb: String) { | ||
coroutineScope.launch(dispatcherProvider.io()) { | ||
val params = mutableMapOf<String, String>() | ||
params[PARAM_APP_VERSION] = appVersionProvider.provide() | ||
pixel.fire(AppPixelName.APP_VERSION_AT_SEARCH_TIME, params) | ||
} | ||
} | ||
|
||
companion object { | ||
internal const val PARAM_APP_VERSION = "app_version" | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/duckduckgo/app/browser/trafficquality/QualityAppVersionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.app.browser.trafficquality | ||
|
||
import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
import java.time.temporal.ChronoUnit | ||
import javax.inject.Inject | ||
|
||
interface QualityAppVersionProvider { | ||
fun provide(): String | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealQualityAppVersionProvider @Inject constructor(private val appBuildConfig: AppBuildConfig) : QualityAppVersionProvider { | ||
override fun provide(): String { | ||
val appBuildDateMillis = appBuildConfig.buildDateTimeMillis | ||
|
||
if (appBuildDateMillis == 0L) { | ||
return APP_VERSION_QUALITY_DEFAULT_VALUE | ||
} | ||
|
||
val appBuildDate = LocalDateTime.ofEpochSecond(appBuildDateMillis / 1000, 0, ZoneOffset.UTC) | ||
val now = LocalDateTime.now(ZoneOffset.UTC) | ||
val daysSinceBuild = ChronoUnit.DAYS.between(appBuildDate, now) | ||
|
||
if (daysSinceBuild < DAYS_AFTER_APP_BUILD_WITH_DEFAULT_VALUE) { | ||
return APP_VERSION_QUALITY_DEFAULT_VALUE | ||
} | ||
|
||
if (daysSinceBuild > DAYS_FOR_APP_VERSION_LOGGING) { | ||
return APP_VERSION_QUALITY_DEFAULT_VALUE | ||
} | ||
|
||
return appBuildConfig.versionName | ||
} | ||
|
||
companion object { | ||
const val APP_VERSION_QUALITY_DEFAULT_VALUE = "other_versions" | ||
const val DAYS_AFTER_APP_BUILD_WITH_DEFAULT_VALUE = 6 | ||
const val DAYS_FOR_APP_VERSION_LOGGING = DAYS_AFTER_APP_BUILD_WITH_DEFAULT_VALUE + 10 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...c/test/java/com/duckduckgo/app/browser/trafficquality/AndroidAppVersionPixelSenderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.duckduckgo.app.browser.trafficquality | ||
|
||
import com.duckduckgo.app.browser.trafficquality.RealQualityAppVersionProvider.Companion.APP_VERSION_QUALITY_DEFAULT_VALUE | ||
import com.duckduckgo.app.pixels.AppPixelName | ||
import com.duckduckgo.app.statistics.pixels.Pixel | ||
import com.duckduckgo.common.test.CoroutineTestRule | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.verifyNoMoreInteractions | ||
import org.mockito.kotlin.whenever | ||
|
||
class AndroidAppVersionPixelSenderTest { | ||
@get:Rule | ||
var coroutineRule = CoroutineTestRule() | ||
|
||
private val mockAppVersionProvider = mock<QualityAppVersionProvider>() | ||
private val mockPixel = mock<Pixel>() | ||
|
||
private lateinit var pixelSender: AndroidAppVersionPixelSender | ||
|
||
@Before | ||
fun setup() { | ||
pixelSender = AndroidAppVersionPixelSender( | ||
mockAppVersionProvider, | ||
mockPixel, | ||
coroutineRule.testScope, | ||
coroutineRule.testDispatcherProvider, | ||
) | ||
} | ||
|
||
@Test | ||
fun reportFeaturesEnabledOrDisabledWhenEnabledOrDisabled() = runTest { | ||
whenever(mockAppVersionProvider.provide()).thenReturn(APP_VERSION_QUALITY_DEFAULT_VALUE) | ||
|
||
pixelSender.onSearchRetentionAtbRefreshed("v123-1", "v123-2") | ||
|
||
verify(mockPixel).fire( | ||
AppPixelName.APP_VERSION_AT_SEARCH_TIME, | ||
mapOf( | ||
AndroidAppVersionPixelSender.PARAM_APP_VERSION to APP_VERSION_QUALITY_DEFAULT_VALUE, | ||
), | ||
) | ||
verifyNoMoreInteractions(mockPixel) | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
.../test/java/com/duckduckgo/app/browser/trafficquality/RealQualityAppVersionProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.duckduckgo.app.browser.trafficquality | ||
|
||
import com.duckduckgo.app.browser.trafficquality.RealQualityAppVersionProvider.Companion.APP_VERSION_QUALITY_DEFAULT_VALUE | ||
import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
import com.duckduckgo.appbuildconfig.api.BuildFlavor | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
class RealQualityAppVersionProviderTest { | ||
|
||
private val appBuildConfig = mock<AppBuildConfig>().apply { | ||
whenever(this.flavor).thenReturn(BuildFlavor.PLAY) | ||
} | ||
|
||
private lateinit var testee: RealQualityAppVersionProvider | ||
|
||
@Before | ||
fun setup() { | ||
testee = RealQualityAppVersionProvider(appBuildConfig) | ||
} | ||
|
||
@Test | ||
fun whenBuildDateEmptyThenReturnDefault() { | ||
whenever(appBuildConfig.buildDateTimeMillis).thenReturn(0L) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == APP_VERSION_QUALITY_DEFAULT_VALUE) | ||
} | ||
|
||
@Test | ||
fun whenBuildDateTodayThenReturnDefault() { | ||
givenBuildDateDaysAgo(0) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == APP_VERSION_QUALITY_DEFAULT_VALUE) | ||
} | ||
|
||
@Test | ||
fun whenNotYetTimeToLogThenReturnDefault() { | ||
givenBuildDateDaysAgo(2) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == APP_VERSION_QUALITY_DEFAULT_VALUE) | ||
} | ||
|
||
@Test | ||
fun whenTimeToLogThenReturnAppVersion() { | ||
givenBuildDateDaysAgo(6) | ||
val versionName = "5.212.0" | ||
givenVersionName(versionName) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == versionName) | ||
} | ||
|
||
@Test | ||
fun whenTimeToLogAndNotOverLoggingPeriodThenReturnAppVersion() { | ||
val versionName = "5.212.0" | ||
givenVersionName(versionName) | ||
givenBuildDateDaysAgo(8) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == versionName) | ||
} | ||
|
||
@Test | ||
fun whenTimeToLogAndOverLoggingPeriodThenReturnDefault() { | ||
val versionName = "5.212.0" | ||
givenVersionName(versionName) | ||
givenBuildDateDaysAgo(20) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == APP_VERSION_QUALITY_DEFAULT_VALUE) | ||
} | ||
|
||
@Test | ||
fun whenTimeToLogAndJustOnLoggingPeriodThenReturnVersionName() { | ||
val versionName = "5.212.0" | ||
givenVersionName(versionName) | ||
givenBuildDateDaysAgo(16) | ||
val appVersion = testee.provide() | ||
assertTrue(appVersion == versionName) | ||
} | ||
|
||
private fun givenBuildDateDaysAgo(days: Long) { | ||
val daysAgo = LocalDateTime.now().minusDays(days).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() | ||
whenever(appBuildConfig.buildDateTimeMillis).thenReturn(daysAgo) | ||
} | ||
|
||
private fun givenVersionName(versionName: String) { | ||
whenever(appBuildConfig.versionName).thenReturn(versionName) | ||
} | ||
} |