Skip to content

Commit

Permalink
Add Firebase Analytics (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Jan 4, 2024
1 parent cc648c6 commit 9235d4d
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 10 deletions.
22 changes: 22 additions & 0 deletions app/src/benchmark/kotlin/ru/pixnews/inject/NoOpAnalyticsModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024, the Pixnews project authors and contributors. Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.inject

import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import dagger.Reusable
import ru.pixnews.foundation.analytics.Analytics
import ru.pixnews.foundation.analytics.NoOpAnalytics
import ru.pixnews.foundation.di.base.scopes.AppScope

@ContributesTo(AppScope::class, replaces = [AnalyticsModule::class])
@Module
object NoOpAnalyticsModule {
@Provides
@Reusable
fun provideAnalytics(): Analytics = NoOpAnalytics
}
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="${firebase_crashlytics_collection_enabled}" />
<meta-data
android:name="firebase_analytics_collection_deactivated"
android:value="${firebase_analytics_collection_deactivated}" />
<meta-data
android:name="google_analytics_adid_collection_enabled"
android:value="${google_analytics_adid_collection_enabled}" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/kotlin/ru/pixnews/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.activity.compose.setContent
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import co.touchlab.kermit.Logger
import ru.pixnews.feature.root.PixnewsRootContent
import ru.pixnews.foundation.analytics.Analytics
import ru.pixnews.foundation.appconfig.AppConfig
import ru.pixnews.foundation.di.ui.base.activity.BaseActivity
import ru.pixnews.foundation.di.ui.base.activity.ContributesActivity
Expand All @@ -32,12 +33,16 @@ class MainActivity : BaseActivity() {
@Inject
internal lateinit var appLoadingStatus: AppLoadingStatus

@Inject
internal lateinit var analytics: Analytics

override fun onPostInjectPreCreate() {
setupSplashScreen()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
analytics.logEvent("main_activity_created")
setContent {
PixnewsRootContent(
appConfig = appConfig,
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/kotlin/ru/pixnews/PixnewsApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import ru.pixnews.inject.MainPixnewsAppComponent
import javax.inject.Inject

class PixnewsApplication : Application(), Configuration.Provider {
override val workManagerConfiguration = localWorkManagerConfiguration
override val workManagerConfiguration
get() = localWorkManagerConfiguration

@field:Inject
lateinit var localWorkManagerConfiguration: Configuration
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/ru/pixnews/inject/AnalyticsModule.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, the Pixnews project authors and contributors. Please see the AUTHORS file for details.
* Copyright (c) 2024, the Pixnews project authors and contributors. Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

Expand All @@ -10,13 +10,13 @@ import dagger.Binds
import dagger.Module
import dagger.Reusable
import ru.pixnews.foundation.analytics.Analytics
import ru.pixnews.foundation.analytics.NoOpAnalytics
import ru.pixnews.foundation.di.base.scopes.AppScope
import ru.pixnews.inject.analytics.LoggingAnalytics

@ContributesTo(AppScope::class)
@Module
interface AnalyticsModule {
@Binds
@Reusable
fun provideAnalytics(analytics: NoOpAnalytics): Analytics
fun bindAnalytics(analytics: LoggingAnalytics): Analytics
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024, the Pixnews project authors and contributors. Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.inject.analytics

import androidx.core.os.bundleOf
import com.google.firebase.Firebase
import com.google.firebase.analytics.analytics
import ru.pixnews.foundation.analytics.Analytics

object FirebaseAnalytics : Analytics {
private val analytics = Firebase.analytics

override fun setEnableAnalytics(enable: Boolean) {
analytics.setAnalyticsCollectionEnabled(enable)
}

override fun setUserId(userId: String?) {
analytics.setUserId(userId)
}

override fun setUserProperty(name: String, value: String) {
analytics.setUserProperty(name, value)
}

override fun logEvent(name: String, params: Map<String, *>?) {
val paramsBundle = if (params != null) {
bundleOf(pairs = params.entries.map { it.key to it.value }.toTypedArray())
} else {
null
}
analytics.logEvent(name, paramsBundle)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024, the Pixnews project authors and contributors. Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.inject.analytics

import co.touchlab.kermit.Logger
import ru.pixnews.foundation.analytics.Analytics
import javax.inject.Inject

class LoggingAnalytics @Inject constructor(
logger: Logger,
) : Analytics {
private val log: Logger = logger.withTag("Analytics")
override fun setEnableAnalytics(enable: Boolean) {
log.i { "setEnableAnalytics($enable)" }
}

override fun setUserId(userId: String?) {
log.i { "setUserId($userId)" }
}

override fun setUserProperty(name: String, value: String) {
log.i { "setUserProperty($name, $value)" }
}

override fun logEvent(name: String, params: Map<String, *>?) {
log.i { "Event `$name`: $params" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024, the Pixnews project authors and contributors. Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.app.inject

import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import dagger.Reusable
import ru.pixnews.foundation.analytics.Analytics
import ru.pixnews.foundation.di.base.scopes.AppScope
import ru.pixnews.inject.AnalyticsModule
import ru.pixnews.inject.analytics.FirebaseAnalytics

@ContributesTo(AppScope::class, replaces = [AnalyticsModule::class])
@Module
public object FirebaseAnalyticsModule {
@Provides
@Reusable
fun bindsAnalytics(): Analytics = FirebaseAnalytics
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
package ru.pixnews.foundation.analytics

public interface Analytics {
public fun setCurrentScreenName(screenName: String, screenClass: String)

public fun setEnableAnalytics(enable: Boolean)

public fun setUserId(userId: String?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

package ru.pixnews.foundation.analytics

public class NoOpAnalytics : Analytics {
public override fun setCurrentScreenName(screenName: String, screenClass: String): Unit = Unit

public object NoOpAnalytics : Analytics {
public override fun setEnableAnalytics(enable: Boolean): Unit = Unit

public override fun setUserId(userId: String?): Unit = Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ extensions.configure<ApplicationExtension>("android") {

defaultConfig {
targetSdk = versionCatalog.findVersion("targetSdk").get().displayName.toInt()
manifestPlaceholders["firebase_crashlytics_collection_enabled"] = "false"
manifestPlaceholders += listOf(
"firebase_crashlytics_collection_enabled" to "false",
"firebase_analytics_collection_deactivated" to "true",
"google_analytics_adid_collection_enabled" to "false",
)
}

val releaseConfig = ReleaseConfig(project)
Expand All @@ -45,11 +49,21 @@ extensions.configure<ApplicationExtension>("android") {
isCrunchPngs = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
signingConfig = signingConfigs.getByName(if (releaseConfig.useReleaseKeystore) "release" else "debug")
manifestPlaceholders += listOf(
"firebase_crashlytics_collection_enabled" to "true",
"firebase_analytics_collection_deactivated" to "false",
"google_analytics_adid_collection_enabled" to "true",
)
}
getByName("debug") {
applicationIdSuffix = ".debug"
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks += listOf("instrumentedTests", "release")
manifestPlaceholders += listOf(
"firebase_crashlytics_collection_enabled" to "false",
"firebase_analytics_collection_deactivated" to "false",
"google_analytics_adid_collection_enabled" to "true",
)
}
create("benchmark") {
initWith(release)
Expand All @@ -62,6 +76,12 @@ extensions.configure<ApplicationExtension>("android") {

proguardFiles("proguard-benchmark.pro")
matchingFallbacks += "release"

manifestPlaceholders += listOf(
"firebase_crashlytics_collection_enabled" to "false",
"firebase_analytics_collection_deactivated" to "true",
"google_analytics_adid_collection_enabled" to "false",
)
}
}

Expand Down
6 changes: 6 additions & 0 deletions script/enable_firebase_analytics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb shell setprop debug.firebase.analytics.app ru.pixnews.debug
# adb logcat -v time -s FA FA-SVC

0 comments on commit 9235d4d

Please sign in to comment.