Skip to content

Log fire-and-forget events for analytics purposes for Android

License

Notifications You must be signed in to change notification settings

govuk-one-login/mobile-android-logging

Repository files navigation

mobile-android-logging

An Android package usable for HTTP logging and passing analytics to a third-party SDK. Also included is an abstraction class for using Google's Firebase analytics platform.

Functionality provided by Logging module

The main functionality of the logging module is to provide a single source of truth for standardised code logging functionality that makes it simple to switch logging framework if desired.

The three packages have the following functionality:

  1. The :api module contains logger interfaces.
  2. The :impl module contains logger implementations and their associated Hilt modules for dependency injection into their constructors.
  3. The :testdouble module contains fake logger implementations for testing and their Hilt modules for dependency injection into their constructors.

Getting started with Logging module dependencies

Without a version catalog

In your build.gradle.kts files, for each module (and for each build flavor), add dependencies needed for example:

// Refer to the uploaded packages for the latest version:
// https://github.com/orgs/govuk-one-login/packages?repo_name=mobile-android-logging
val loggingLibraryVersion: String by rootProject.extra("1.2.3")

// Both `impl` and `testdouble` artifacts provide the `api` module as a gradle `api` dependency.
implementation("uk.gov.logging:logging-impl:$loggingLibraryVersion")
testImplementation("uk.gov.logging:logging-testdouble:$loggingLibraryVersion")
androidTestImplementation("uk.gov.logging:logging-testdouble:$loggingLibraryVersion")

With a version catalog (recommended)

To migrate to using version catalogs, see Gradle Version Catalogs.

In your root ./gradle/libs.versions.toml file:

[versions]
gov-logging = "1.2.3" # https://github.com/orgs/govuk-one-login/packages?repo_name=mobile-android-logging

[libraries]
uk-gov-logging-api = { module = "uk.gov.logging:logging-api", version.ref = "gov-logging"}
uk-gov-logging-impl = { module = "uk.gov.logging:logging-impl", version.ref = "gov-logging"}
uk-gov-logging-testdouble = { module = "uk.gov.logging:logging-testdouble", version.ref = "gov-logging"}

Then in your build.gradle.kts files:

// Both `impl` and `testdouble` artifacts provide the `api` module as a gradle `api` dependency.
implementation(libs.uk.gov.logging.impl)
testImplementation(libs.uk.gov.logging.testdouble)
androidTestImplementation(libs.uk.gov.logging.testdouble)

Hilt Configuration

Despite there being usable Hilt modules in the logging module, these are mainly for Android Logging. You should also create your own Singleton scoped Hilt modules to provide the specific analytics logger implementation that you need, and also to provide the Firebase analytics implementation. For example:

@InstallIn(SingletonComponent::class)
@Module
object AnalyticsSingletonModule {
    @Provides
    @Singleton
    fun providesAnalyticsLogger(
        analyticsLogger: FirebaseAnalyticsLogger
    ): AnalyticsLogger = MemorisedAnalyticsLogger(analyticsLogger)
}

@InstallIn(SingletonComponent::class)
@Module
class FirebaseSingletonModule {
    @Provides
    @Singleton
    fun providesFirebaseAnalytics(): FirebaseAnalytics = Firebase.analytics
}

Releases

The packages can be found at here at Logging Module Packages.

The tags can be found at here at Logging Module Tags.

GA4 Migration Guide

There has been a breaking change on some of the required parameters. We support both the original and the new GA4. However you will need to import the correct type based on your need.

// GA4 type
import uk.gov.logging.api.analytics.parameters.v2.RequiredParameters
// Original type
import uk.gov.logging.api.analytics.parameters.RequiredParameters

If you need to use both GA4 and the original one in the same file, you will need to import as an alias

import uk.gov.logging.api.analytics.parameters.RequiredParameters
import uk.gov.logging.api.analytics.parameters.v2.RequiredParameters as RequiredParametersV2

Legacy

Implementation Guide One Login Mobile Application Data Schema V1.0.

GA4