The number of analytics and data visualization systems necessary for everyday work has long exceeded all reasonable limits. With the advent of Firebase, their camp was replenished by one. In this article we will explain how to make friends with Google’s mobile platform with another, more familiar analytics tool - Google Analytics. The platform for experiments was our new product Tviggo (mobile application + a set of instant messenger bots for international calls via GSM - www tviggo. Net).

As you know, Google Tag Manager (GTM) in December 2016 released an update - GTM V5 for iOS and Android, sharpened for work with Firebase. At present, there are practically no real examples of use code for version 5, therefore, in addition to a rather meager parting word, you need to collect information bit by bit on the Quick Start page. So, one of the important differences from v4 and earlier versions is the GTM initialization order. Now the developer is not required to write any boilerplate of the initialization code and you need to forget about loadContainerPreferNonDefault. At the same time, you can focus only on sending events to Firebase, while not forgetting to place a JSON file in the assets / containers with the base container version, which can be downloaded from GTM right after the first publication.
Firebase can transmit any event name and parameters to GTM (in addition to all
standard ones that are automatically generated by the application) for collecting analytical data on the application.
')
Through GTM logic, these events can be broadcast to external systems, such as Google Analytics.
To carry out such a manipulation, it is necessary in the code, in places that are critical for analytics control, to arrange the parameters for uploading data to Firebase (for example, sending the “Start” event with the parameters A = 1, B = 2).
Next, in GTM, you need to create variables A and B, set up the configuration of the variable, then start a trigger (for example, “launch application”), and create a tag (for example, as soon as Firebase triggers the Start event, you must upload the specified content to GA).
Now visually and step by step:
1. Set up a custom variable

2. Register the variable configuration

3. Start the trigger

4. Create a tag

In the application code, we similarly describe the parameters and events generated in GTM in a convenient wrapper for further use:
class TviggoAnalytics(context: Context) { val TviggoStart = Event("TviggoStart") object Params { val call_uid = Param("call_uid") val call_medium = Param("call_medium") } private val firebase: FirebaseAnalytics = FirebaseAnalytics.getInstance(context) inner class Event(val name: String) { fun push(vararg params: Pair<Param, Any?>) { push(HashMap<Param, Any>() + params) } fun push(baseParams: Map<Param, Any?>, vararg params: Pair<Param, Any?>) { push(baseParams + params) } fun push(params: Map<Param, Any?>) { val bundle = Bundle() params.forEach { if (it.value != null) { when (it.value) { is String -> bundle.putString(it.key.name, it.value as String) is Int -> bundle.putInt(it.key.name, it.value as Int) is Long -> bundle.putLong(it.key.name, it.value as Long) is Float -> bundle.putFloat(it.key.name, it.value as Float) is Byte -> bundle.putByte(it.key.name, it.value as Byte) is Short -> bundle.putShort(it.key.name, it.value as Short) is Double -> bundle.putDouble(it.key.name, it.value as Double) is Boolean -> bundle.putBoolean(it.key.name, it.value as Boolean) is Char -> bundle.putChar(it.key.name, it.value as Char) else -> bundle.putString(it.key.name, it.value.toString()) } } } firebase.logEvent(name, bundle) } } class Param(val name: String) }
And further, for example, in the Application class, when creating an application instance, we prescribe sending events to Firebase with the Call ID parameter equal to 2.
val analytics = TviggoAnalytics(applicationContext) override fun onCreate() { super.onCreate() analytics.TviggoStart.push( TviggoAnalytics.Params.call_uid to 2, TviggoAnalytics.Params.call_medium to "GSM" ) }
The parameter “call identifier” is used for an example - in fact, there is still no call identifier at the time of launch.
What we get in the end:
1. Some action was performed in the application, a report on the implementation of which we see in GA, through the transformation of the original Firebase event through the Google Tag Manager container.

2. If we analyze even deeper, we will see that the parameter we created with the value 2 is displayed in the Label (exactly as indicated in the tag).

3. At the same time, all original events in the initial form are also loaded into Firebase.
Thus, through GTM you get a mechanism that allows you to save analytics in any convenient system and analyze the entire stream of data coming from Firebase.
Such a system allows you to quickly manage unloading in GA without rebuilding the application.
I hope the explanation was clear and useful for the habro-community.
PS Yes, our project is written in Kotlin, so the Android code will not look quite familiar, but I think you can do it :)
We are happy to answer your questions!