Configure Firebase Analaytics + Google Tag Manager (GTM) for each build option

Before Firebase Analytics is available, we use the Android Gradle project setup with several tastes, a multi build type and supply a different GTM container identifier for each build option as follows:

TagManager.getInstance(context) .loadContainerPreferNonDefault(BuildConfig.GTM_CONTAINER_ID, -1); TagManager.getInstance(context).getDataLayer().pushEvent(eventName, eventData); 

where Gradle will enter a different GTM_CONTAINER_ID for each build option.

How do we achieve this with Firebase Analytics + GTM? According to the docs , we need to download:

  • GTM container file from the GTM control panel [1]
  • google-services.json file from the Firebase console [2]

and then just start with this event:

 FirebaseAnalytics.getInstance(context).logEvent(eventName, bundle); 

Where do we specify the GTM container identifier to be used? Or is it automatically determined by the name of the file that we download from the GTM control panel and put in assets/containers ? If so, how do we use a different GTM configuration for each build option, as is the case with the legacy Android GTM container?

+6
source share
2 answers

The container identifier is inferred from the container file name, as you expected. To use the option for each assembly, you can use the gradle copy task to install the correct container.

+3
source

Thus, we created our Gradle multi-flavor project to use different GTM containers for each build option:

 / |_app/ |_src/ |_flavor1/ | |_google-services.json # Google services config for debug | |_release/ | |_google-services.json # Google services config for flavor1 |_flavor1Release/ | |_assets/ | |_containers/ | |_GTM-ABCXY1.json # GTM container for flavor1 | |_flavor2/ | |_google-services.json # Google services config for debug | |_release/ | |_google-services.json # Google services config for flavor2 |_flavor2Release/ | |_assets/ | |_containers/ | |_GTM-ABCXY2.json # GTM container for flavor2 | |_debug/ | |_assets/ | |_containers/ | |_GTM-ABCXY3.json # GTM container for debug | |_main/ |_res/ |_java/ 

Assuming you have 2 flavors flavor1 and flavor2 , and you want to have 3 GTM containers, 1 common for debugging assembly of both flavors and 1 for each assembly of each flavor.

GTM will connect to the FA control panel of the project specified by your google-services.json . Support for multi-build multi-build type google-services.json , because the plugin is version 2.1.0 [1]

+2
source

All Articles