Skip to content

Commit

Permalink
feat: add anonymous analytics (WPB-8933) (#3076)
Browse files Browse the repository at this point in the history
Signed-off-by: alexandreferris <[email protected]>
  • Loading branch information
alexandreferris authored Jun 5, 2024
1 parent 4a8279a commit 5d18460
Show file tree
Hide file tree
Showing 40 changed files with 999 additions and 7 deletions.
23 changes: 23 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import customization.ConfigurationFileImporter
import customization.NormalizedFlavorSettings
import scripts.Variants_gradle

/*
Expand Down Expand Up @@ -49,6 +51,16 @@ fun isFossSourceSet(): Boolean {
.lowercase()
.contains("fdroid")
}

private fun getFlavorsSettings(): NormalizedFlavorSettings =
try {
val file = file("${project.rootDir}/default.json")
val configurationFileImporter = ConfigurationFileImporter()
configurationFileImporter.loadConfigsFromFile(file)
} catch (e: Exception) {
error(">> Error reading current flavors, exception: ${e.localizedMessage}")
}

android {
// Most of the configuration is done in the build-logic
// through the Wire Application convention plugin
Expand Down Expand Up @@ -193,6 +205,17 @@ dependencies {
}
implementation(libs.androidx.work)

// Anonymous Analytics
val flavors = getFlavorsSettings()
flavors.flavorMap.entries.forEach { (key, configs) ->
if (configs["analytics_enabled"] as? Boolean == true) {
println(">> Adding Anonymous Analytics dependency to [$key] flavor")
add("${key}Implementation",project(":core:analytics-enabled"))
} else {
add("${key}Implementation",project(":core:analytics-disabled"))
}
}

// commonMark
implementation(libs.commonmark.core)
implementation(libs.commonmark.strikethrough)
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/kotlin/com/wire/android/WireApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import com.wire.android.datastore.GlobalDataStore
import com.wire.android.di.ApplicationScope
import com.wire.android.di.KaliumCoreLogic
import com.wire.android.util.CurrentScreenManager
import com.wire.android.feature.analytics.AnonymousAnalyticsManagerImpl
import com.wire.android.feature.analytics.AnonymousAnalyticsRecorderImpl
import com.wire.android.feature.analytics.model.AnalyticsSettings
import com.wire.android.util.DataDogLogger
import com.wire.android.util.LogFileWriter
import com.wire.android.util.getGitBuildId
Expand Down Expand Up @@ -147,6 +150,25 @@ class WireApplication : Application(), Configuration.Provider {
// 4. Everything ready, now we can log device info
appLogger.i("Logger enabled")
logDeviceInformation()
// 5. Verify if we can initialize Anonymous Analytics
initializeAnonymousAnalytics()
}

private fun initializeAnonymousAnalytics() {
val anonymousAnalyticsRecorder = AnonymousAnalyticsRecorderImpl()
val analyticsSettings = AnalyticsSettings(
countlyAppKey = BuildConfig.ANALYTICS_APP_KEY,
countlyServerUrl = BuildConfig.ANALYTICS_SERVER_URL,
enableDebugLogging = BuildConfig.DEBUG
)

AnonymousAnalyticsManagerImpl.init(
context = this,
analyticsSettings = analyticsSettings,
isEnabledFlowProvider = globalDataStore.get()::isAnonymousUsageDataEnabled,
anonymousAnalyticsRecorder = anonymousAnalyticsRecorder,
dispatcher = Dispatchers.IO
)
}

private fun logDeviceInformation() {
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/kotlin/com/wire/android/ui/WireActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -58,6 +60,8 @@ import com.wire.android.config.CustomUiConfigurationProvider
import com.wire.android.config.LocalCustomUiConfigurationProvider
import com.wire.android.datastore.UserDataStore
import com.wire.android.feature.NavigationSwitchAccountActions
import com.wire.android.feature.analytics.globalAnalyticsManager
import com.wire.android.feature.analytics.model.AnalyticsEvent
import com.wire.android.navigation.BackStackMode
import com.wire.android.navigation.LocalNavigator
import com.wire.android.navigation.NavigationCommand
Expand Down Expand Up @@ -277,6 +281,25 @@ class WireActivity : AppCompatActivity() {
navController.removeOnDestinationChangedListener(currentScreenManager)
}
}

val lifecycleOwner = LocalLifecycleOwner.current
val activity = LocalContext.current as Activity
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
globalAnalyticsManager.onStart(activity = activity)
globalAnalyticsManager.sendEvent(AnalyticsEvent.AppOpen())
}
if (event == Lifecycle.Event.ON_STOP) {
globalAnalyticsManager.onStop()
}
}
lifecycleOwner.lifecycle.addObserver(observer)

onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
}

extensions.configure<LibraryExtension> {
namespace = "com.wire.android.feature.${target.name}"
namespace = "com.wire.android.feature.${target.name.replace("-", "_")}"

// TODO: Handle flavors. Currently implemented in `variants.gradle.kts` script
configureKotlinAndroid(this)
Expand Down
7 changes: 7 additions & 0 deletions buildSrc/src/main/kotlin/customization/FeatureConfigs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ enum class FeatureConfigs(val value: String, val configType: ConfigType) {

MAX_REMOTE_SEARCH_RESULT_COUNT("max_remote_search_result_count", ConfigType.INT),
LIMIT_TEAM_MEMBERS_FETCH_DURING_SLOW_SYNC("limit_team_members_fetch_during_slow_sync", ConfigType.INT),

/**
* Anonymous Analytics
*/
ANALYTICS_ENABLED("analytics_enabled", ConfigType.BOOLEAN),
ANALYTICS_APP_KEY("analytics_app_key", ConfigType.STRING),
ANALYTICS_SERVER_URL("analytics_server_url", ConfigType.STRING)
}
1 change: 1 addition & 0 deletions core/analytics-disabled/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
17 changes: 17 additions & 0 deletions core/analytics-disabled/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id(libs.plugins.wire.android.library.get().pluginId)
id(libs.plugins.wire.kover.get().pluginId)
}

dependencies {
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)

api(project(":core:analytics"))

val composeBom = platform(libs.compose.bom)
implementation(composeBom)
implementation(libs.compose.ui)

testImplementation(libs.junit4)
}
Empty file.
4 changes: 4 additions & 0 deletions core/analytics-disabled/lint-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 8.2.0" type="baseline" client="gradle" dependencies="false" name="AGP (8.2.0)" variant="all" version="8.2.0">

</issues>
21 changes: 21 additions & 0 deletions core/analytics-disabled/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
20 changes: 20 additions & 0 deletions core/analytics-disabled/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Wire
~ Copyright (C) 2024 Wire Swiss GmbH
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see http://www.gnu.org/licenses/.
-->
<manifest>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.feature.analytics

object AnonymousAnalyticsManagerImpl : AnonymousAnalyticsManagerStub()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.feature.analytics

class AnonymousAnalyticsRecorderImpl : AnonymousAnalyticsRecorderStub()
20 changes: 20 additions & 0 deletions core/analytics-disabled/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Wire
~ Copyright (C) 2024 Wire Swiss GmbH
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see http://www.gnu.org/licenses/.
-->
<resources>
<string name="module_name">Disabled Analytics Module</string>
</resources>
1 change: 1 addition & 0 deletions core/analytics-enabled/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
21 changes: 21 additions & 0 deletions core/analytics-enabled/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id(libs.plugins.wire.android.library.get().pluginId)
id(libs.plugins.wire.kover.get().pluginId)
}

dependencies {
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)

api(project(":core:analytics"))

val composeBom = platform(libs.compose.bom)
implementation(composeBom)
implementation(libs.compose.ui)

implementation(libs.countly.sdk)

testImplementation(libs.junit4)
testImplementation(libs.mockk.core)
testImplementation(libs.coroutines.test)
}
Empty file.
4 changes: 4 additions & 0 deletions core/analytics-enabled/lint-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 8.2.0" type="baseline" client="gradle" dependencies="false" name="AGP (8.2.0)" variant="all" version="8.2.0">

</issues>
21 changes: 21 additions & 0 deletions core/analytics-enabled/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
20 changes: 20 additions & 0 deletions core/analytics-enabled/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Wire
~ Copyright (C) 2024 Wire Swiss GmbH
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see http://www.gnu.org/licenses/.
-->
<manifest>

</manifest>
Loading

0 comments on commit 5d18460

Please sign in to comment.