Skip to content

Commit cdf842a

Browse files
committed
Attributed metrics module and skeleton
1 parent dfb371d commit cdf842a

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2024 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
id 'java-library'
19+
id 'kotlin'
20+
}
21+
22+
apply from: "$rootProject.projectDir/code-formatting.gradle"
23+
24+
java {
25+
sourceCompatibility = JavaVersion.VERSION_17
26+
targetCompatibility = JavaVersion.VERSION_17
27+
}
28+
29+
kotlin {
30+
jvmToolchain(17)
31+
}
32+
33+
dependencies {
34+
implementation Kotlin.stdlib.jdk7
35+
implementation KotlinX.coroutines.core
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.attributed.metrics.api
18+
19+
// owns storing events, providing stats and emitting metrics
20+
// owns collection and monitoring windows (6mo monitoring)
21+
// owns new user / returning user logic
22+
// owns adding common params to all metrics (e.g. origin, removing default params)
23+
interface AttributedMetricClient {
24+
// will store an event in the data base, keep the counter per day
25+
fun collectEvent(eventName: String)
26+
27+
// return events stored in the last days, and precalculated stats
28+
suspend fun getEventStats(eventName: String, days: Int): EventStats
29+
30+
// if in monitoring window will emit the metric
31+
// this part owns adding common params to all metrics (e.g. origin, removing default params)
32+
fun emitMetric(metric: AttributedMetric)
33+
}
34+
35+
// stats about events collected
36+
data class EventStats(
37+
// number of days with at least one event
38+
val daysWithEvents: Int,
39+
// rolling average of events based on days timeframe
40+
val rollingAverage: Double,
41+
// total number of events in the timeframe
42+
val totalEvents: Int
43+
)
44+
45+
// interface for each metric
46+
interface AttributedMetric {
47+
// Metric owns the pixel name value
48+
fun getPixelName(): String
49+
// Metric owns adding metric specific parameters
50+
suspend fun getMetricParameters(): Map<String, String>
51+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2024 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
id 'com.android.library'
19+
id 'kotlin-android'
20+
id 'com.google.devtools.ksp'
21+
id 'com.squareup.anvil'
22+
}
23+
24+
apply from: "$rootProject.projectDir/gradle/android-library.gradle"
25+
26+
dependencies {
27+
anvil project(path: ':anvil-compiler')
28+
29+
implementation project(path: ':anvil-annotations')
30+
implementation project(path: ':attributed-metrics-api')
31+
implementation project(path: ':common-utils')
32+
implementation project(path: ':di')
33+
implementation project(path: ':app-build-config-api')
34+
implementation project(path: ':statistics-api')
35+
36+
implementation KotlinX.coroutines.core
37+
implementation KotlinX.coroutines.android
38+
39+
implementation "io.reactivex.rxjava2:rxjava:_"
40+
implementation "io.reactivex.rxjava2:rxandroid:_"
41+
42+
implementation Square.retrofit2.retrofit
43+
implementation Square.retrofit2.converter.moshi
44+
implementation Square.retrofit2.adapter.rxJava2
45+
implementation Square.retrofit2.converter.scalars
46+
47+
implementation Google.dagger
48+
49+
// Room
50+
implementation AndroidX.room.ktx
51+
ksp AndroidX.room.compiler
52+
53+
// WorkManager
54+
implementation AndroidX.work.runtimeKtx
55+
androidTestImplementation AndroidX.work.testing
56+
implementation AndroidX.work.rxJava2
57+
58+
implementation "com.squareup.logcat:logcat:_"
59+
60+
implementation AndroidX.core.ktx
61+
62+
testImplementation Testing.junit4
63+
testImplementation "org.mockito.kotlin:mockito-kotlin:_"
64+
testImplementation "androidx.lifecycle:lifecycle-runtime-testing:_"
65+
testImplementation project(path: ':common-test')
66+
testImplementation project(':data-store-test')
67+
testImplementation CashApp.turbine
68+
testImplementation Testing.robolectric
69+
testImplementation(KotlinX.coroutines.test) {
70+
// https://github.com/Kotlin/kotlinx.coroutines/issues/2023
71+
// conflicts with mockito due to direct inclusion of byte buddy
72+
exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"
73+
}
74+
testImplementation AndroidX.test.ext.junit
75+
testImplementation AndroidX.archCore.testing
76+
testImplementation AndroidX.room.testing
77+
testImplementation AndroidX.room.rxJava2
78+
79+
androidTestImplementation AndroidX.test.runner
80+
androidTestImplementation AndroidX.test.rules
81+
82+
coreLibraryDesugaring Android.tools.desugarJdkLibs
83+
}
84+
85+
android {
86+
anvil {
87+
generateDaggerFactories = true // default is false
88+
}
89+
lintOptions {
90+
baseline file("lint-baseline.xml")
91+
abortOnError = !project.hasProperty("abortOnError") || project.property("abortOnError") != "false"
92+
}
93+
namespace 'com.duckduckgo.app.attributed.metrics'
94+
compileOptions {
95+
coreLibraryDesugaringEnabled = true
96+
}
97+
buildFeatures {
98+
buildConfig = true
99+
}
100+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.attributed.metrics.impl
18+
19+
import com.duckduckgo.app.attributed.metrics.api.AttributedMetric
20+
import com.duckduckgo.app.attributed.metrics.api.AttributedMetricClient
21+
import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin
22+
import com.duckduckgo.app.attributed.metrics.api.EventStats
23+
import com.duckduckgo.app.di.AppCoroutineScope
24+
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
25+
import com.duckduckgo.di.scopes.AppScope
26+
import com.squareup.anvil.annotations.ContributesBinding
27+
import com.squareup.anvil.annotations.ContributesMultibinding
28+
import dagger.SingleInstanceIn
29+
import kotlinx.coroutines.CoroutineScope
30+
import kotlinx.coroutines.launch
31+
import javax.inject.Inject
32+
33+
@ContributesMultibinding(AppScope::class, AtbLifecyclePlugin::class)
34+
@ContributesBinding(AppScope::class, AttributedMetricClient::class)
35+
@SingleInstanceIn(AppScope::class)
36+
class RealAttributedMetricClient @Inject constructor(
37+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
38+
private val appBuildConfig: AppBuildConfig,
39+
): AttributedMetricClient, AtbLifecyclePlugin {
40+
41+
override fun onAppAtbInitialized() {
42+
appCoroutineScope.launch {
43+
if (appBuildConfig.isAppReinstall()) {
44+
// Do not start metrics for returning users
45+
return@launch
46+
} else {
47+
// enable collecting events and emitting metrics
48+
}
49+
}
50+
}
51+
52+
override fun collectEvent(eventName: String) {
53+
TODO("Not yet implemented")
54+
}
55+
56+
override suspend fun getEventStats(
57+
eventName: String,
58+
days: Int
59+
): EventStats {
60+
TODO("Not yet implemented")
61+
}
62+
63+
override fun emitMetric(metric: AttributedMetric) {
64+
TODO("Not yet implemented")
65+
}
66+
}

attributed-metrics/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Attributed Metrics
2+
This module contains utility code for collecting attributed metrics.
3+
4+
## Who can help you better understand this feature?
5+
- Cristian Monforte
6+
7+
## More information
8+
N/A

0 commit comments

Comments
 (0)