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

查看:24
本文介绍了在单个 android 应用程序中使用多个 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(字符串).setApplicationId(字符串).setDatabaseUrl(字符串).建造();

然后当您想使用 Analytics 时,您需要通过调用设置默认值:

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

请记住,只有这个 DEFAULT firebase 应用将用于分析

但首先你需要删除清单中的init provider

 <提供者android:name="com.google.firebase.provider.FirebaseInitProvider"android:authorities="${applicationId}.firebaseinitprovider"工具:节点=删除"/>

手动初始化默认 firebase 应用

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

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

<块引用>

@ceph3us 我试过你的解决方案,但对我不起作用.如果我按照您的建议在运行时初始化 firebase 然后我收到一个错误:缺少 google_app_id.Firebase 分析已停用.你确定是在职的?– rMozes

首先

  1. 您是否通过添加清单工具:node="remove"删除了默认提供程序?
  2. 您是否按照我的描述初始化了 ['DEFAULT'] firebase 应用程序
  3. 在发送任何事件之前,您是否检查了 ['DEFAULT'] firebase 应用是否已初始化?

ad 1) error: Missing google_app_id 表明 gardle 插件没有按预期删除提供程序 - 您的应用程序正在启动一个默认提供程序,该提供程序抱怨缺少应用程序 ID

广告 3) 在 firebase 应用程序初始化之前不要进行任何依赖于 firebase 应用程序的调用

 protected boolean isDefaultFirebaseAppInitialized() {尝试 {//尝试获取返回 FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME) != null;//捕获非法状态 exc} catch (IllegalStateException ise) {//在这种情况下未初始化返回假;}}//检查默认应用if(isDefaultFirebaseAppInitialized()) {//获取跟踪器FirebaseAnalytics trakerInstance = FirebaseAnalytics.getInstance(context);//记录事件trackerInstance.logEvent(eventCategory, params);} 别的 {//推迟}

<块引用>

@ceph3us 1. 如你所说,我删除了提供者,如果我不删除提供者并尝试初始化默认应用程序然后我会得到一个关于默认 Firebase 应用程序的 IllegalStateException 已存在.2.我如您所述,已初始化默认 Firebase 应用程序.3. 是的,我登录了应用名称和 app_id 并且有一个日志: AppName: [DEFAULT], Googleapp id: valid_app_id 但是当我想向分析发布一些东西时,然后它说:缺少google_app_id.Firebase 分析已停用.– rMozes

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

<块引用>

@ceph3us 我在 onCreate 方法中初始化 FirebaseApp应用程序子类.当按钮按下时,我将事件发送到 firebase点击.反正我上传了一个测试项目到github上,你能不能拿一个看它?我可能误解了一些东西.github.com/rMozes/TestFirebaseAnalytics – rMozes

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

https://github.com/rMozes/TestFirebaseAnalytics/比较/主...c3ph3us:patch-2

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

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

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

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

提示如何

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

an 实现方法示例(通过id添加新资源)

如果您通过反射正确更改 R 字段或使用自己的文本替换对 Resources.getString(R.string.google_app_id) 的调用您将不会收到消息错误的应用程序 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 two more chances :)

by define the string in xml: as a placeholder

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

1) change the placeholder id via reflections to other one before any call to init/or use from firebase:

hint how to

2) provide a own text for the placeholder id via own Resources class implementation for Resources.getString(R.string.google_app_id) call:

an example how to achieve it (adding a new resources by id)

if you proper change R field via reflections or substitute a call to Resources.getString(R.string.google_app_id) with own text you will not get message wrong app id: "fuck_you_google"

& good luck :)

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

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