Embedding Huawei analytics in the Android app

image



In the last article, we created a developer account for using Huawei Mobile Services and prepared a project for using them. Now it's time to start embedding specific services.



Here is a complete list of articles from the series:



  1. We create a developer account, connect dependencies, prepare the code for deployment. tyk
  2. We build Huawei Analytics. ← you are here
  3. Huawei.
  4. Huawei maps. Google maps AppGallery.




. , :



interface Analytics {
    fun send(event: AnalyticsEvent)
}

interface AnalyticsEvent {

    val key: String

    val data: Map<String, Any>

}

fun Map<String, Any>.toBundle() =
    Bundle().apply {
        forEach { (key, value) ->
            when (value) {
                is String -> putString(key, value)
                is Int -> putInt(key, value)
                is Boolean -> putBoolean(key, value)
                is Double -> putDouble(key, value)
                is Float -> putFloat(key, value)
                else -> throw IllegalArgumentException("Unknown data type: ${value::class.simpleName}")
            }
        }
    }

open class SimpleEvent(override val key: String) : AnalyticsEvent {

    override val data: Map<String, Any> = hashMapOf()

    override fun toString(): String = "AnalyticsEvent { key = $key, data = $data }"
}

open class ParamsEvent(key: String, vararg params: Pair<String, Any>): SimpleEvent(key) {
    override val data = params.toMap()
}

class EventOpenSomeScreen : SimpleEvent("screen_some_screen")


, - , - :



@Inject
lateinit var analytics: Analytics

...

analytics.send(EventOpenSomeScreen())




, .



  1. , huawei flavor- , google β€” :


dependencies {
  huaweiImplementation 'com.huawei.agconnect:agconnect-core:1.3.1.300'
  huaweiImplementation 'com.huawei.hms:hianalytics:5.0.0.301'

  googleImplementation 'com.google.firebase:firebase-analytics:17.2.3'
}


  1. DI Analytics AnalyticsImpl. AnalyticsImpl . β€” src/huawei/kotlin/com/example :


class AnalyticsImpl(context: Context) : Analytics {

    private val analytics = HiAnalytics.getInstance(context)

    override fun send(event: AnalyticsEvent) {
        analytics.onEvent(event.key, event.data.toBundle())
    }
}


src/google/kotlin/com/example:



class AnalyticsImpl(context: Context) : Analytics {
  private val firebaseAnalytics = FirebaseAnalytics.getInstance(context)

  override fun send(event: AnalyticsEvent) {
      firebaseAnalytics.logEvent(event.key, event.data.toBundle())
  }
}


. API .



,



, , Huawei . :



  1. .
  2. adb shell setprop debug.huawei.hms.analytics.app _APPLICATION_ID__
  3. , AppGallery Connect -> -> -> "" -> -> .
  4. .
  5. adb shell setprop debug.huawei.hms.analytics.app .none.


:



image



β€”



, Huawei , Google.



All the code that is in this series of articles you can see in the repository on GitHub. Here is the link .




All Articles