在单个Android应用中使用多个Firebase帐户进行谷歌分析 [英] Use multiple firebase accounts in single android app for google analytics

查看:292
本文介绍了在单个Android应用中使用多个Firebase帐户进行谷歌分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,其中1个应用程序将被多个独立的公司(特许经营)使用,将有自己的营销和管理团队。我需要用户在移动应用程序启动时选择一个特许经营权,然后从那一刻起,将所有分析数据推送到特许经营商的firebase(谷歌分析)账户。同样,从特权服务器发送的任何推送通知都需要发送给用户。

这种配置可能吗?在过去,我曾经为每个特许经营设立一个谷歌分析帐户,只需从特许经营选择的服务器上下载UA-xxx号码,然后建立基于这个的谷歌分析对象。



通过与Google Analytics(分析)相关联的Firebase实现此功能的适当方法是什么?我找到了官方的API参考: https://firebase.google.com/docs/configure/
此链接说明了如何操作对于iOS,但没有提到如何在Android中做到这一点。它确实表示firebase init在用户代码之前运行..也许这意味着它是不可能的?



以下是他们提到的init提供程序: https://firebase.google.com/docs/reference/android/com/ google / firebase / provider / FirebaseInitProvider

解决方案

为每个新的Firebase应用程序创建

  FirebaseApp firebaseApp = 
FirebaseApp.initializeApp(Context,FirebaseOptions,firebaseAppName);

您可以通过传递选项来创建Firebase应用

b
$ b

https:/ /firebase.google.com/docs/reference/android/com/google/firebase/FirebaseOptions.Builder

  FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey(String)
.setApplicationId(String)
.setDatabaseUrl(String)
.build();

然后当您要使用Google Analytics时,您需要设置默认的调用:

  FirebaseApp firebaseApp = 
FirebaseApp.initializeApp(Context,FirebaseOptions,[DEFAULT]);请注意,只有这个 DEFAULT Firebase应用将用于分析工具



但首先关闭所有您需要的删除清单中的init提供程序

 <! - 将firebase提供程序移除为手动初始化 - > 
< provider
android:name =com.google.firebase.provider.FirebaseInitProvider
android:authorities =$ {applicationId} .firebaseinitprovider
tools:node = 去除/>

手动初始化Firebase应用程序

示例如何通过默认的firebase应用程序发送事件(初始化后):

  //获取跟踪器实例
FirebaseAnalytics trakerInstance = FirebaseAnalytics.getInstance(context);
//为params创建包
Bundle params = new Bundle();
//把参数例如动作
params.putString(ACTION_KEY,eventAction);
//发送事件
trackerInstance.logEvent(eventCategory,params);




@ ceph3us我试过你的解决方案,我。如果我
在运行时初始化firebase,如你所建议的,那么我得到一个错误:
缺少google_app_id。 Firebase Analytics已停用。你确定这是
工作吗? - rMozes


首先


  1. 是否通过放置清单工具来删除默认提供程序:node =remove

  2. 是否按照我描述的方式初始化了['DEFAULT'] firebase应用
  3. 您是否检查了['DEFAULT'] firebase应用程序在发送任何事件之前被初始化?


    ad 1)错误:缺少google_app_id 暗示我gardle插件没有按预期移除提供程序 - 而且您的应用程序正在启动一个默认提供程序,该应用程序抱怨丢失的应用程序ID

    ad 3)在firebase应用程序初始化之前,不要使用任何依赖firebase应用程序的调用

      protected boolean isDefaultFirebaseAppInitialized ){
    尝试{
    //尝试获取
    返回FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME)!= null;
    //捕获非法状态exc
    } catch(IllegalStateException ise){
    //在这种情况下未初始化
    返回false;


    $ b $ //检查默认应用
    if(isDefaultFirebaseAppInitialized()){
    //获取跟踪器
    FirebaseAnalytics trakerInstance = FirebaseAnalytics.getInstance(上下文);
    //记录事件
    trackerInstance.logEvent(eventCategory,params);
    } else {
    //延期
    }




    @ ceph3us 1.我说如果我不移除
    提供程序,并尝试初始化默认的应用程序,那么我会得到
    IllegalStateException关于默认的firebase应用程序已经存在。 2.如您所述,I
    初始化了默认的Firebase应用程序。 3.是的,我记录了
    的应用程序名称和app_id,并且有一个日志:AppName:[DEFAULT],Google
    app id:valid_app_id但是当我想发布一些东西到分析,
    那么它说:缺少google_app_id。 Firebase Analytics已停用。
    - rMozes


    99,99%您尝试在应用初始化之前发送事件!(请参阅上面的示例)

    lockquote
    @ ceph3us我在
    应用程序子类的onCreate方法中初始化FirebaseApp。当按钮是
    点击时,我发送事件到firebase。无论如何,我上传了一个测试项目github,你可以拿
    看看吗?我可能误解了一些东西。
    github.com/rMozes/TestFirebaseAnalytics - rMozes

    尝试(因为初始化是异步的 - 所以直到你测试它的初始化为止不能发送事件):

    https://github.com/rMozes/TestFirebaseAnalytics/compare/master...c3ph3us:patch-2



    如果以上失败你有最后一次机会:)

    在xml字符串中定义

     < string name =google_app_id> fuck_you_google< / string> 

    并通过反射改变它 - 提示如何&祝你好运:)

    如果您通过反射适当更改R字段,您将不会收到消息
    错误的应用程序ID:fuck_you_google

    I have a use case in which 1 app will be used by multiple separate companies (franchises) that will have their own marketing and management teams. I need the user to select a franchise when the mobile app starts up, and then from that point on, push all analytics data to that franchise's firebase (google analytics) account. Similarly any push notifications that are sent from that franchise's servers need to go to the user.

    Is this configuration possible? In the past I used to set up a google analytics account for each franchise and just download the UA-xxx number from the server on franchise selection, and then set up the google analytics object based on that..

    What is the appropriate way to achieve this via firebase connected to google analytics ?

    I found the offical API reference: https://firebase.google.com/docs/configure/ This link explains how to do it for iOS but doesn't mention how to do it in android. It does say however that the firebase init runs before user code.. perhaps that means it is not possible?

    Here is the init provider they mention: https://firebase.google.com/docs/reference/android/com/google/firebase/provider/FirebaseInitProvider

    解决方案

    create for each new firebase app

        FirebaseApp firebaseApp = 
              FirebaseApp.initializeApp(Context, FirebaseOptions,firebaseAppName);
    

    you can create firebase app by passing options:

    https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseOptions.Builder

    FirebaseOptions options = new FirebaseOptions.Builder()
        .setApiKey(String)
        .setApplicationId(String)
        .setDatabaseUrl(String)
        .build();
    

    then when You want to use Analytics you need to set default one by call:

    FirebaseApp firebaseApp = 
          FirebaseApp.initializeApp(Context, FirebaseOptions,"[DEFAULT]");
    

    keep in mind that only this DEFAULT firebase app will be used in analytics

    but first off all you need to remove init provider in manifest

            <!--remove firebase provider to init manually -->
        <provider
            android:name="com.google.firebase.provider.FirebaseInitProvider"
            android:authorities="${applicationId}.firebaseinitprovider"
            tools:node="remove"/>
    

    and init default firebase app manually!

    example how to send event via default firebase app(after initialized):

    // get tracker instance
    FirebaseAnalytics trakerInstance = FirebaseAnalytics.getInstance(context);
    // create bundle for params
    Bundle params = new Bundle();
    // put param for example action
    params.putString(ACTION_KEY, eventAction);
    // send event 
    trackerInstance.logEvent(eventCategory, params);
    

    @ceph3us I tried your solution, and it didn't work for me. If I initialise firebase at runtime as you suggested then I get an error: Missing google_app_id. Firebase Analytics disabled. Are you sure it is working? – rMozes

    first of all

    1. did you removed default provider by putting in manifest tools:node="remove"?
    2. did u initialized ['DEFAULT'] firebase app as i described
    3. did you check if a ['DEFAULT'] firebase app is initialized before sending any event ?

    ad 1) the error: Missing google_app_id suggests me that gardle plugin didn't removed provider as expected - and your app is starting a default provider which complains about missing app id

    ad 3) don't do any calls relying on firebase app before firebase app is initialized

        protected boolean isDefaultFirebaseAppInitialized() {
            try {
                // try get
                return FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME) != null;
                // catch illegal state exc
            } catch (IllegalStateException ise) {
                // on such case not initialized
                return false;
            }
        }
    
    // check on default app 
    if(isDefaultFirebaseAppInitialized()) {
        // get tracker 
        FirebaseAnalytics trakerInstance = FirebaseAnalytics.getInstance(context);
        // log event 
        trackerInstance.logEvent(eventCategory, params);
    } else {
        // postpone
    }
    

    @ceph3us 1. I removed the provider as you said, if I wouldn't remove the provider and try to initialise the default app then I would get a IllegalStateException about default firebase app already exists. 2. I initialised default firebase app as you described. 3. Yes, I logged the app name and app_id and there is a log: AppName: [DEFAULT], Google app id: valid_app_id But when I want to post something to analytics, then it says that: Missing google_app_id. Firebase Analytics disabled. – rMozes

    99,99% you are trying to send event before app is initialized ! (see above example)

    @ceph3us I initialise FirebaseApp in the onCreate method of Application subclass. I send event to firebase when a button is clicked. Anyway I uploaded a test project to github, can you take a look at it? It is possible I misunderstand something. github.com/rMozes/TestFirebaseAnalytics – rMozes

    try (as initialization is asynchronous - so until you test its initialized you cant send events):

    https://github.com/rMozes/TestFirebaseAnalytics/compare/master...c3ph3us:patch-2

    if above fails you have one last chance :)

    define in xml string:

    <string name="google_app_id">fuck_you_google</string>
    

    and change it via reflections before any call to init/or use firebase - hint how to & good luck :)

    if you proper change R field via reflections you will not get message wrong app id: "fuck_you_google"

    这篇关于在单个Android应用中使用多个Firebase帐户进行谷歌分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆