+-
在单个Android应用中使用多个firebase帐户进行Google Analytics
我有一个用例,其中1个应用程序将由多个独立的公司(特许经营)使用,这些公司将拥有自己的营销和管理团队.我需要用户在移动应用程序启动时选择特许经营权,然后从那时起,将所有分析数据推送到该特许经营的firebase(谷歌分析)帐户.同样,从该特许经营的服务器发送的任何推送通知都需要发送给用户.

这种配置是否可行?在过去,我曾经为每个特许经营店设置一个谷歌分析帐户,只需从服务器上下载特许经营选择的UA-xxx号码,然后根据该设置设置谷歌分析对象.

通过连接到谷歌分析的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应用程序:

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提供程序

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

和手动初始化默认的firebase应用程序!

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

// 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

首先

>您是否通过放入清单工具删除了默认提供程序:node =“remove”?
>你按照我的描述初始化了[‘DEFAULT’] firebase app
>在发送任何事件之前,您是否检查了[‘DEFAULT’] firebase应用程序是否已初始化?

广告1)错误:缺少google_app_id建议我gardle插件没有按预期删除提供程序 – 并且您的应用程序正在启动默认提供程序,它抱怨缺少应用程序ID

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

    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%! (见上例)

@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

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

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

如果失败,你还有两次机会:)

通过在xml中定义字符串:作为占位符

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

1)在调用init /或从firebase使用之前,通过反射将占位符id更改为其他ID:

hint how to

2)通过Resources.getString(R.string.google_app_id)调用的自己的Resources类实现为占位符id提供自己的文本:

example how to achieve it(按ID添加新资源)

如果您通过反射正确更改R字段或用自己的文本替换对Resources.getString(R.string.google_app_id)的调用,您将不会收到错误的消息应用ID:“fuck_you_google”

&安培;祝好运 :)

点击查看更多相关文章

转载注明原文:在单个Android应用中使用多个firebase帐户进行Google Analytics - 乐贴网