-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adding search related attributed metrics #6899
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
Open
cmonfortep
wants to merge
1
commit into
feature/cristian/attributed_metrics_internal_dev_settings
Choose a base branch
from
feature/cristian/search_attributed_metric
base: feature/cristian/attributed_metrics_internal_dev_settings
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+904
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -18,9 +18,11 @@ package com.duckduckgo.app.global.install | |
|
||
import com.duckduckgo.browser.api.install.AppInstall | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
|
||
@ContributesBinding(AppScope::class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed this bit on a different PR. "Unrelated" |
||
@SingleInstanceIn(AppScope::class) | ||
class AppInstallRepository @Inject constructor( | ||
private val appInstallStore: AppInstallStore, | ||
|
This file contains hidden or 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
155 changes: 155 additions & 0 deletions
155
...etrics-impl/src/main/java/com/duckduckgo/app/attributed/metrics/SearchAttributedMetric.kt
This file contains hidden or 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,155 @@ | ||
/* | ||
* Copyright (c) 2025 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.attributed.metrics | ||
|
||
import com.duckduckgo.app.attributed.metrics.api.AttributedMetric | ||
import com.duckduckgo.app.attributed.metrics.api.AttributedMetricClient | ||
import com.duckduckgo.app.attributed.metrics.api.EventStats | ||
import com.duckduckgo.app.attributed.metrics.store.AttributedMetricsDateUtils | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin | ||
import com.duckduckgo.app.statistics.store.StatisticsDataStore | ||
import com.duckduckgo.browser.api.install.AppInstall | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import dagger.SingleInstanceIn | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import logcat.logcat | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Search Count 7d avg Attributed Metric | ||
* Trigger: on first search of day | ||
* Type: Daily pixel | ||
* Report: 7d rolling average of searches (bucketed value). Not sent if count is 0. | ||
* Specs: https://app.asana.com/1/137249556945/project/1206716555947156/task/1211313432282643?focus=true | ||
*/ | ||
@ContributesMultibinding(AppScope::class, AtbLifecyclePlugin::class) | ||
@ContributesMultibinding(AppScope::class, AttributedMetric::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class RealSearchAttributedMetric @Inject constructor( | ||
marcosholgado marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val attributedMetricClient: AttributedMetricClient, | ||
private val appInstall: AppInstall, | ||
private val statisticsDataStore: StatisticsDataStore, | ||
private val dateUtils: AttributedMetricsDateUtils, | ||
) : AttributedMetric, AtbLifecyclePlugin { | ||
|
||
companion object { | ||
private const val EVENT_NAME = "ddg_search" | ||
private const val FIRST_MONTH_PIXEL = "user_average_searches_past_week_first_month" | ||
private const val PAST_WEEK_PIXEL_NAME = "user_average_searches_past_week" | ||
private const val DAYS_WINDOW = 7 | ||
private const val FIRST_MONTH_DAY_THRESHOLD = 28 // we consider 1 month after 4 weeks | ||
private val SEARCH_BUCKETS = arrayOf( | ||
5, | ||
9, | ||
) // TODO: default bucket, remote bucket implementation will happen in future PRs | ||
} | ||
|
||
override fun onSearchRetentionAtbRefreshed( | ||
oldAtb: String, | ||
newAtb: String, | ||
) { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
attributedMetricClient.collectEvent(EVENT_NAME) | ||
|
||
if (oldAtb == newAtb) { | ||
logcat(tag = "AttributedMetrics") { | ||
"SearchCount7d: Skip emitting, atb not changed" | ||
} | ||
return@launch | ||
} | ||
if (shouldSendPixel().not()) { | ||
logcat(tag = "AttributedMetrics") { | ||
"SearchCount7d: Skip emitting, not enough data or no events" | ||
} | ||
return@launch | ||
} | ||
attributedMetricClient.emitMetric(this@RealSearchAttributedMetric) | ||
} | ||
} | ||
|
||
override fun getPixelName(): String = when (daysSinceInstalled()) { | ||
in 0..FIRST_MONTH_DAY_THRESHOLD -> FIRST_MONTH_PIXEL | ||
else -> PAST_WEEK_PIXEL_NAME | ||
} | ||
|
||
override suspend fun getMetricParameters(): Map<String, String> { | ||
val stats = getEventStats() | ||
val params = mutableMapOf( | ||
"count" to getBucketValue(stats.rollingAverage.toInt()).toString(), | ||
) | ||
if (!hasCompleteDataWindow()) { | ||
params["dayAverage"] = daysSinceInstalled().toString() | ||
} | ||
return params | ||
} | ||
|
||
override suspend fun getTag(): String { | ||
// Daily metric, on first search of day | ||
// rely on searchRetentionAtb as mirrors the metric trigger event | ||
return statisticsDataStore.searchRetentionAtb | ||
?: "no-atb" // should not happen, but just in case | ||
} | ||
|
||
private fun getBucketValue(searches: Int): Int { | ||
return SEARCH_BUCKETS.indexOfFirst { bucket -> searches <= bucket }.let { index -> | ||
if (index == -1) SEARCH_BUCKETS.size else index | ||
} | ||
} | ||
|
||
private suspend fun shouldSendPixel(): Boolean { | ||
if (daysSinceInstalled() <= 0) { | ||
// installation day, we don't emit | ||
return false | ||
} | ||
|
||
val eventStats = getEventStats() | ||
if (eventStats.daysWithEvents == 0 || eventStats.rollingAverage == 0.0) { | ||
// no events, nothing to emit | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
private suspend fun getEventStats(): EventStats { | ||
val stats = if (hasCompleteDataWindow()) { | ||
attributedMetricClient.getEventStats(EVENT_NAME, DAYS_WINDOW) | ||
} else { | ||
attributedMetricClient.getEventStats( | ||
EVENT_NAME, | ||
daysSinceInstalled(), | ||
) | ||
} | ||
|
||
return stats | ||
} | ||
|
||
private fun hasCompleteDataWindow(): Boolean { | ||
val daysSinceInstalled = daysSinceInstalled() | ||
return daysSinceInstalled >= DAYS_WINDOW | ||
} | ||
|
||
private fun daysSinceInstalled(): Int { | ||
return dateUtils.daysSince(appInstall.getInstallationTimestamp()) | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...cs-impl/src/main/java/com/duckduckgo/app/attributed/metrics/SearchDaysAttributedMetric.kt
This file contains hidden or 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,139 @@ | ||
/* | ||
* Copyright (c) 2025 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.attributed.metrics | ||
|
||
import com.duckduckgo.app.attributed.metrics.api.AttributedMetric | ||
import com.duckduckgo.app.attributed.metrics.api.AttributedMetricClient | ||
import com.duckduckgo.app.attributed.metrics.store.AttributedMetricsDateUtils | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin | ||
import com.duckduckgo.app.statistics.store.StatisticsDataStore | ||
import com.duckduckgo.browser.api.install.AppInstall | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import dagger.SingleInstanceIn | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import logcat.logcat | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Search Days Attributed Metric | ||
* Trigger: on app start | ||
* Type: Daily pixel | ||
* Report: Bucketed value, how many days user searched last 7d. Not sent if count is 0. | ||
* Specs: https://app.asana.com/1/137249556945/project/1206716555947156/task/1211301604929609?focus=true | ||
*/ | ||
@ContributesMultibinding(AppScope::class, AtbLifecyclePlugin::class) | ||
@ContributesMultibinding(AppScope::class, AttributedMetric::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class RealSearchDaysAttributedMetric @Inject constructor( | ||
marcosholgado marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val attributedMetricClient: AttributedMetricClient, | ||
private val appInstall: AppInstall, | ||
private val statisticsDataStore: StatisticsDataStore, | ||
private val dateUtils: AttributedMetricsDateUtils, | ||
) : AttributedMetric, AtbLifecyclePlugin { | ||
|
||
companion object { | ||
private const val EVENT_NAME = "ddg_search_days" | ||
private const val PIXEL_NAME = "user_active_past_week" | ||
private const val DAYS_WINDOW = 7 | ||
private val DAYS_BUCKETS = arrayOf( | ||
2, | ||
4, | ||
) // TODO: default bucket, remote bucket implementation will happen in future PRs | ||
} | ||
|
||
override fun onAppRetentionAtbRefreshed( | ||
oldAtb: String, | ||
newAtb: String, | ||
) { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
if (oldAtb == newAtb) { | ||
logcat(tag = "AttributedMetrics") { | ||
"SearchDays: Skip emitting atb not changed" | ||
} | ||
return@launch | ||
} | ||
if (shouldSendPixel().not()) { | ||
logcat(tag = "AttributedMetrics") { | ||
"SearchDays: Skip emitting, not enough data or no events" | ||
} | ||
return@launch | ||
} | ||
attributedMetricClient.emitMetric(this@RealSearchDaysAttributedMetric) | ||
} | ||
} | ||
|
||
override fun onSearchRetentionAtbRefreshed( | ||
oldAtb: String, | ||
newAtb: String, | ||
) { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
attributedMetricClient.collectEvent(EVENT_NAME) | ||
} | ||
} | ||
|
||
override fun getPixelName(): String = PIXEL_NAME | ||
|
||
override suspend fun getMetricParameters(): Map<String, String> { | ||
val daysSinceInstalled = daysSinceInstalled() | ||
val hasCompleteDataWindow = daysSinceInstalled >= DAYS_WINDOW | ||
val stats = attributedMetricClient.getEventStats(EVENT_NAME, DAYS_WINDOW) | ||
val params = mutableMapOf( | ||
"days" to getBucketValue(stats.daysWithEvents).toString(), | ||
) | ||
if (!hasCompleteDataWindow) { | ||
params["daysSinceInstalled"] = daysSinceInstalled.toString() | ||
} | ||
return params | ||
} | ||
|
||
override suspend fun getTag(): String { | ||
// Daily metric, on App start | ||
// rely on appRetentionAtb as mirrors the metric trigger event | ||
return statisticsDataStore.appRetentionAtb ?: "no-atb" // should not happen, but just in case | ||
} | ||
|
||
private fun getBucketValue(days: Int): Int { | ||
return DAYS_BUCKETS.indexOfFirst { bucket -> days <= bucket }.let { index -> | ||
if (index == -1) DAYS_BUCKETS.size else index | ||
} | ||
} | ||
|
||
private suspend fun shouldSendPixel(): Boolean { | ||
if (daysSinceInstalled() <= 0) { | ||
// installation day, we don't emit | ||
return false | ||
} | ||
|
||
val eventStats = attributedMetricClient.getEventStats(EVENT_NAME, DAYS_WINDOW) | ||
if (eventStats.daysWithEvents == 0) { | ||
// no events, nothing to emit | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
private fun daysSinceInstalled(): Int { | ||
return dateUtils.daysSince(appInstall.getInstallationTimestamp()) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.