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

故事扮演 提交于 2019-12-10 02:40:04

问题


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:

  • a GTM container file from GTM dashboard [1]
  • a google-services.json file from Firebase console [2]

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?


回答1:


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]




回答2:


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.



来源:https://stackoverflow.com/questions/37807970/configure-firebase-analaytics-google-tag-manager-gtm-per-build-variant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!