-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update origin handling logic (Attributed Metrics) #7156
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
Merged
cmonfortep
merged 1 commit into
develop
from
feature/cristian/attributed_metrics/remote_origin_handling
Nov 20, 2025
Merged
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
76 changes: 76 additions & 0 deletions
76
...trics-impl/src/main/java/com/duckduckgo/app/attributed/metrics/impl/OriginParamManager.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,76 @@ | ||
| /* | ||
| * 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.impl | ||
|
|
||
| import com.duckduckgo.app.attributed.metrics.AttributedMetricsConfigFeature | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import com.squareup.moshi.JsonAdapter | ||
| import com.squareup.moshi.Moshi | ||
| import dagger.SingleInstanceIn | ||
| import javax.inject.Inject | ||
|
|
||
| interface OriginParamManager { | ||
| /** | ||
| * Determines whether the origin parameter should be included to metric based on the remote config. | ||
| * | ||
| * @return true if the origin should be included in the metric, false if not | ||
| */ | ||
| fun shouldSendOrigin(origin: String?): Boolean | ||
| } | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| @SingleInstanceIn(AppScope::class) | ||
| class RealOriginParamManager @Inject constructor( | ||
| private val attributedMetricsConfigFeature: AttributedMetricsConfigFeature, | ||
| private val moshi: Moshi, | ||
| ) : OriginParamManager { | ||
| private val sendOriginParamAdapter: JsonAdapter<SendOriginParamSettings> by lazy { | ||
cmonfortep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| moshi.adapter(SendOriginParamSettings::class.java) | ||
| } | ||
|
|
||
| // Cache parsed substrings. It's expected this to be a short list and not change frequently. | ||
| private val cachedSubstrings: List<String> by lazy { | ||
| kotlin.runCatching { | ||
| attributedMetricsConfigFeature.sendOriginParam().getSettings() | ||
| ?.let { sendOriginParamAdapter.fromJson(it) } | ||
| ?.originCampaignSubstrings | ||
| }.getOrNull() ?: emptyList() | ||
| } | ||
|
|
||
| override fun shouldSendOrigin(origin: String?): Boolean { | ||
| // If toggle is disabled, don't send origin | ||
| if (!attributedMetricsConfigFeature.sendOriginParam().isEnabled()) { | ||
| return false | ||
| } | ||
|
|
||
| // If origin is null or blank, can't send it | ||
| if (origin.isNullOrBlank()) { | ||
| return false | ||
| } | ||
|
|
||
| // If no substrings configured, don't send origin | ||
| if (cachedSubstrings.isEmpty()) { | ||
| return false | ||
| } | ||
|
|
||
| // Check if origin matches any of the configured substrings (case-insensitive) | ||
| return cachedSubstrings.any { substring -> | ||
| origin.contains(substring, ignoreCase = true) | ||
| } | ||
| } | ||
| } | ||
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
23 changes: 23 additions & 0 deletions
23
...-impl/src/main/java/com/duckduckgo/app/attributed/metrics/impl/SendOriginParamSettings.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,23 @@ | ||
| /* | ||
| * 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.impl | ||
|
|
||
| import com.squareup.moshi.Json | ||
|
|
||
| data class SendOriginParamSettings( | ||
| @Json(name = "originCampaignSubstrings") val originCampaignSubstrings: List<String> = emptyList(), | ||
| ) |
167 changes: 167 additions & 0 deletions
167
...s-impl/src/test/java/com/duckduckgo/app/attributed/metrics/impl/OriginParamManagerTest.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,167 @@ | ||
| /* | ||
| * 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.impl | ||
|
|
||
| import com.duckduckgo.app.attributed.metrics.AttributedMetricsConfigFeature | ||
| import com.duckduckgo.feature.toggles.api.Toggle | ||
| import com.squareup.moshi.Moshi | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.whenever | ||
|
|
||
| class OriginParamManagerTest { | ||
|
|
||
| private val mockAttributedMetricsConfigFeature: AttributedMetricsConfigFeature = mock() | ||
| private val mockSendOriginParamToggle: Toggle = mock() | ||
| private val moshi = Moshi.Builder().build() | ||
|
|
||
| private lateinit var testee: RealOriginParamManager | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| whenever(mockAttributedMetricsConfigFeature.sendOriginParam()).thenReturn(mockSendOriginParamToggle) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenToggleDisabledThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(false) | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_paid_test") | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOriginIsNullThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin(null) | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOriginIsBlankThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin(" ") | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenSubstringListIsEmptyThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":[]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_paid_test") | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOriginMatchesSubstringThenReturnsTrue() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_paid_test") | ||
|
|
||
| assertTrue(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOriginDoesNotMatchSubstringThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_organic_test") | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOriginMatchesAnyOfMultipleSubstringsThenReturnsTrue() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid","sponsored","affiliate"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_sponsored_search") | ||
|
|
||
| assertTrue(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenMatchingIsCaseInsensitiveThenReturnsTrue() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val resultUpperCase = testee.shouldSendOrigin("campaign_PAID_test") | ||
| val resultMixedCase = testee.shouldSendOrigin("campaign_PaId_test") | ||
|
|
||
| assertTrue(resultUpperCase) | ||
| assertTrue(resultMixedCase) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenSettingsParsingFailsThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("invalid json") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_paid_test") | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenSettingsIsNullThenReturnsFalse() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn(null) | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val result = testee.shouldSendOrigin("campaign_paid_test") | ||
|
|
||
| assertFalse(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOriginContainsSubstringWithinWordThenReturnsTrue() { | ||
| whenever(mockSendOriginParamToggle.isEnabled()).thenReturn(true) | ||
| whenever(mockSendOriginParamToggle.getSettings()).thenReturn("""{"originCampaignSubstrings":["paid"]}""") | ||
| testee = RealOriginParamManager(mockAttributedMetricsConfigFeature, moshi) | ||
|
|
||
| val resultHipaid = testee.shouldSendOrigin("funnel_hipaid_us") | ||
| val resultPaidctv = testee.shouldSendOrigin("funnel_paidctv_us") | ||
| val resultPaid = testee.shouldSendOrigin("funnel_paid_us") | ||
|
|
||
| assertTrue(resultHipaid) | ||
| assertTrue(resultPaidctv) | ||
| assertTrue(resultPaid) | ||
| } | ||
| } |
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
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.