-
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.
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
127 changes: 127 additions & 0 deletions
127
app/src/main/java/com/duckduckgo/app/browser/search/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,127 @@ | ||
/* | ||
* 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.browser.search | ||
|
||
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.di.AppCoroutineScope | ||
import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin | ||
import com.duckduckgo.browser.api.UserBrowserProperties | ||
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 | ||
|
||
@ContributesMultibinding(AppScope::class, AtbLifecyclePlugin::class) | ||
@ContributesMultibinding(AppScope::class, AttributedMetric::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class RealSearchAttributedMetric @Inject constructor( | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val attributedMetricClient: AttributedMetricClient, | ||
private val userBrowserProperties: UserBrowserProperties, | ||
) : 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 (userBrowserProperties.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"] = userBrowserProperties.daysSinceInstalled().toString() | ||
} | ||
return params | ||
} | ||
|
||
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 (userBrowserProperties.daysSinceInstalled() == 0L) { | ||
// 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, userBrowserProperties.daysSinceInstalled().toInt()) | ||
} | ||
|
||
return stats | ||
} | ||
|
||
private fun hasCompleteDataWindow(): Boolean { | ||
val daysSinceInstalled = userBrowserProperties.daysSinceInstalled().toInt() | ||
return daysSinceInstalled >= DAYS_WINDOW | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
app/src/main/java/com/duckduckgo/app/browser/search/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,107 @@ | ||
/* | ||
* 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.browser.search | ||
|
||
import com.duckduckgo.app.attributed.metrics.api.AttributedMetric | ||
import com.duckduckgo.app.attributed.metrics.api.AttributedMetricClient | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin | ||
import com.duckduckgo.browser.api.UserBrowserProperties | ||
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 | ||
|
||
@ContributesMultibinding(AppScope::class, AtbLifecyclePlugin::class) | ||
@ContributesMultibinding(AppScope::class, AttributedMetric::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class RealSearchDaysAttributedMetric @Inject constructor( | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val attributedMetricClient: AttributedMetricClient, | ||
private val userBrowserProperties: UserBrowserProperties, | ||
) : 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 onSearchRetentionAtbRefreshed( | ||
oldAtb: String, | ||
newAtb: String, | ||
) { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
attributedMetricClient.collectEvent(EVENT_NAME) | ||
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 getPixelName(): String = PIXEL_NAME | ||
|
||
override suspend fun getMetricParameters(): Map<String, String> { | ||
val daysSinceInstalled = userBrowserProperties.daysSinceInstalled().toInt() | ||
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 | ||
} | ||
|
||
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 (userBrowserProperties.daysSinceInstalled() == 0L) { | ||
// 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 | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal screen bug fix: before making a search, there's no search retention atb, and override was not working