hidro
hidro

Reputation: 12521

Configure Firebase Analaytics + Google Tag Manager (GTM) per build variant

Before Firebase Analytics became available we use a multi flavour, multi build type Android Gradle project setup, and supply a different GTM container ID for each build variant, as follows:

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

where Gradle would inject different GTM_CONTAINER_ID per build variant.

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

and then just start firing events with this:

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

Where do we specify the GTM container ID to be used? Or is it auto derived by the file name we download from GTM dashboard and put under assets/containers? If so, how do we use different GTM configuration per build variant as we do with GTM legacy Android container?

Upvotes: 12

Views: 2408

Answers (2)

hidro
hidro

Reputation: 12521

This is how we set up our Gradle multi-flavor project to use different GTM container for each build variant:

/
|_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 want to have 3 GTM containers, 1 shared for debug build of both flavors, and 1 each for release build of each flavor.

GTM will connect to the FA dashboard of the project specified by your google-services.json. Multi-flavor multi-build type google-services.json support has been available since plugin version 2.1.0 [1]

Upvotes: 8

Eric Burley
Eric Burley

Reputation: 576

The container ID is derived from the container file name, as you surmised. To use a per-build variant you can use gradle copy task to stage the correct container.

Upvotes: 3

Related Questions