-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Send wide events to a dedicated POST endpoint #7422
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
lmac012
wants to merge
7
commits into
develop
Choose a base branch
from
feature/lukasz/wide-event-post
base: develop
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.
+1,052
−141
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
162d699
Extract PixelWideEventSender to a separate file
lmac012 949e5d6
Moving stuff around
lmac012 6c404a8
Use WorkManager to retry sending wide events after encountering an error
lmac012 f914eab
Refactor CompletedWideEventsProcessor to remove processCompletedWideE…
lmac012 0622c73
Add unit tests for CompletedWideEventProcessor
lmac012 bbae3f2
Send wide events to a dedicated POST endpoint
lmac012 8627e21
Add unit tests for ApiWideEventSender
lmac012 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
108 changes: 108 additions & 0 deletions
108
...tistics-impl/src/main/java/com/duckduckgo/app/statistics/wideevents/ApiWideEventSender.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,108 @@ | ||
| /* | ||
| * 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.statistics.wideevents | ||
|
|
||
| import com.duckduckgo.app.statistics.wideevents.api.AppSection | ||
| import com.duckduckgo.app.statistics.wideevents.api.ContextSection | ||
| import com.duckduckgo.app.statistics.wideevents.api.FeatureData | ||
| import com.duckduckgo.app.statistics.wideevents.api.FeatureSection | ||
| import com.duckduckgo.app.statistics.wideevents.api.GlobalSection | ||
| import com.duckduckgo.app.statistics.wideevents.api.WideEventRequest | ||
| import com.duckduckgo.app.statistics.wideevents.api.WideEventService | ||
| import com.duckduckgo.app.statistics.wideevents.db.WideEventRepository | ||
| import com.duckduckgo.app.statistics.wideevents.db.WideEventRepository.WideEventStatus.CANCELLED | ||
| import com.duckduckgo.app.statistics.wideevents.db.WideEventRepository.WideEventStatus.FAILURE | ||
| import com.duckduckgo.app.statistics.wideevents.db.WideEventRepository.WideEventStatus.SUCCESS | ||
| import com.duckduckgo.app.statistics.wideevents.db.WideEventRepository.WideEventStatus.UNKNOWN | ||
| import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
| import com.duckduckgo.common.utils.device.DeviceInfo | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import javax.inject.Inject | ||
| import javax.inject.Named | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| @Named("api") | ||
| class ApiWideEventSender @Inject constructor( | ||
| private val wideEventService: WideEventService, | ||
| private val appBuildConfig: AppBuildConfig, | ||
| private val deviceInfo: DeviceInfo, | ||
| ) : WideEventSender { | ||
|
|
||
| override suspend fun sendWideEvent(event: WideEventRepository.WideEvent) { | ||
| requireNotNull(event.status) { "Attempting to send wide event with null status" } | ||
|
|
||
| val request = WideEventRequest( | ||
| global = GlobalSection( | ||
| platform = PLATFORM, | ||
| type = TYPE, | ||
| sampleRate = SAMPLE_RATE, | ||
| ), | ||
| app = AppSection( | ||
| name = APP_NAME, | ||
| version = appBuildConfig.versionName, | ||
| formFactor = deviceInfo.formFactor().description, | ||
| devMode = appBuildConfig.isDebug.toString(), | ||
| ), | ||
| feature = FeatureSection( | ||
| name = event.name, | ||
| status = event.status.toParamValue(), | ||
| data = buildFeatureData(event), | ||
| ), | ||
| context = event.flowEntryPoint?.let { ContextSection(name = it) }, | ||
| ) | ||
|
|
||
| wideEventService.sendWideEvent(request) | ||
| } | ||
|
|
||
| private fun buildFeatureData(event: WideEventRepository.WideEvent): FeatureData? { | ||
| val extData = mutableMapOf<String, String>() | ||
|
|
||
| // Add metadata as ext data | ||
| event.metadata | ||
| .filterValues { it != null } | ||
| .forEach { (key, value) -> | ||
| extData[key] = value!! | ||
| } | ||
|
|
||
| // Add steps as ext data with "step." prefix | ||
| event.steps.forEach { (name, success) -> | ||
| extData["step.$name"] = success.toString() | ||
| } | ||
|
|
||
| return if (extData.isNotEmpty()) { | ||
| FeatureData(ext = extData) | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| private companion object { | ||
| const val PLATFORM = "Android" | ||
| const val TYPE = "app" | ||
| const val SAMPLE_RATE = 1 | ||
| const val APP_NAME = "DuckDuckGo Android" | ||
| } | ||
| } | ||
|
|
||
| private fun WideEventRepository.WideEventStatus.toParamValue(): String = | ||
| when (this) { | ||
| SUCCESS -> "SUCCESS" | ||
| FAILURE -> "FAILURE" | ||
| CANCELLED -> "CANCELLED" | ||
| UNKNOWN -> "UNKNOWN" | ||
| } |
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
51 changes: 51 additions & 0 deletions
51
...-impl/src/main/java/com/duckduckgo/app/statistics/wideevents/DelegatingWideEventSender.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,51 @@ | ||
| /* | ||
| * 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.statistics.wideevents | ||
|
|
||
| import com.duckduckgo.app.statistics.wideevents.db.WideEventRepository | ||
| import com.duckduckgo.common.utils.DispatcherProvider | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import kotlinx.coroutines.withContext | ||
| import javax.inject.Inject | ||
| import javax.inject.Named | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| class DelegatingWideEventSender @Inject constructor( | ||
| @Named("pixel") private val pixelWideEventSender: WideEventSender, | ||
| @Named("api") private val apiWideEventSender: WideEventSender, | ||
| private val wideEventFeature: WideEventFeature, | ||
| private val dispatcherProvider: DispatcherProvider, | ||
| ) : WideEventSender { | ||
|
|
||
| override suspend fun sendWideEvent(event: WideEventRepository.WideEvent) { | ||
| if (shouldUseApiSender()) { | ||
| apiWideEventSender.sendWideEvent(event) | ||
| } | ||
| if (shouldUsePixelSender()) { | ||
| pixelWideEventSender.sendWideEvent(event) | ||
| } | ||
| } | ||
lmac012 marked this conversation as resolved.
Show resolved
Hide resolved
lmac012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private suspend fun shouldUsePixelSender(): Boolean = withContext(dispatcherProvider.io()) { | ||
| wideEventFeature.sendWideEventsViaPixels().isEnabled() | ||
| } | ||
|
|
||
| private suspend fun shouldUseApiSender(): Boolean = withContext(dispatcherProvider.io()) { | ||
| wideEventFeature.sendWideEventsViaPost().isEnabled() | ||
| } | ||
| } | ||
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.