在单个Android应用中使用多个firebase帐户进行Google Analytics

在单个Android应用中使用多个firebase帐户进行Google Analytics,第1张

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

我有一个用例,其中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 instanceFirebaseAnalytics trakerInstance = FirebaseAnalytics.getInstance(context);// create bundle for paramsBundle params = new Bundle();// put param for example actionparams.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全部内容,希望文章能够帮你解决在单个Android应用中使用多个firebase帐户进行Google Analytics所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1102091.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存