在单个 android 应用中使用多个 firebase 帐户进行 google 分析
Use multiple firebase accounts in single android app for google analytics
我有一个用例,其中 1 个应用程序将由多个独立的公司(特许经营)使用,这些公司将拥有自己的营销和管理团队。我需要用户在移动应用程序启动时 select 特许经营权,然后从那时起,将所有分析数据推送到该特许经营权的 firebase(google 分析)帐户。同样,从该特许经营服务器发送的任何推送通知都需要发送给用户。
这个配置可行吗?过去我曾经为每个特许经营设置一个 google 分析帐户,只需从特许经营服务器上下载 UA-xxx 号码 selection,然后设置 google 分析基于那个的对象..
通过连接到 google 分析的 firebase 实现此目的的适当方法是什么?
我找到了官方 API 参考资料:https://firebase.google.com/docs/configure/
link 解释了如何为 iOS 做,但没有提到如何在 android 中做。但是它确实说 firebase 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();
然后 当您想使用 Analytics 时,您需要通过调用设置默认值:
FirebaseApp firebaseApp =
FirebaseApp.initializeApp(Context, FirebaseOptions,"[DEFAULT]");
请记住,只有这个 DEFAULT firebase 应用程序将用于分析
但首先您需要删除清单中的初始化提供程序
<!--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
首先
- 您是否通过放入清单 tools:node="remove" 删除了默认提供程序?
- 你有没有 初始化 ['DEFAULT'] firebase 应用程序 如我所述
- 您是否检查 ['DEFAULT'] firebase 应用程序在发送任何事件之前是否已初始化?
广告 1) error: Missing 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/or 使用 firebase 之前,通过反射 将占位符 ID 更改为其他 ID:
hint how to
2) 通过自己的资源 class 实现 Resources.getString(R.string.google_app_id) 调用为占位符 ID 提供自己的文本:
an example how to achieve it(通过id添加新资源)
如果您通过反射正确更改 R 字段或用自己的文本替换对 Resources.getString(R.string.google_app_id) 的调用 您将不会收到消息错误的应用程序 ID:"fuck_you_google"
&祝你好运:)
您的应用中可以有多个 Firebase 实例
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("Your AppId") // Required for Analytics.
.setApiKey("You ApiKey") // Required for Auth.
.setDatabaseUrl("Your DB Url") // If you wanted to
.build();
FirebaseApp.initializeApp(context, options, "CompanyA");
您可以通过
获取 Firebase 实例
FirebaseApp appCompanyA = FirebaseApp.getInstance("CompanyA");
您可以查看使用多个 Firebase 实例使用 Auth 和实时数据库的完整示例 here
我将把这个解决方案留在这里,它可能会与可能需要这个的人的另一个答案重复
希望对您有所帮助:)
我有一个用例,其中 1 个应用程序将由多个独立的公司(特许经营)使用,这些公司将拥有自己的营销和管理团队。我需要用户在移动应用程序启动时 select 特许经营权,然后从那时起,将所有分析数据推送到该特许经营权的 firebase(google 分析)帐户。同样,从该特许经营服务器发送的任何推送通知都需要发送给用户。
这个配置可行吗?过去我曾经为每个特许经营设置一个 google 分析帐户,只需从特许经营服务器上下载 UA-xxx 号码 selection,然后设置 google 分析基于那个的对象..
通过连接到 google 分析的 firebase 实现此目的的适当方法是什么?
我找到了官方 API 参考资料:https://firebase.google.com/docs/configure/ link 解释了如何为 iOS 做,但没有提到如何在 android 中做。但是它确实说 firebase 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();
然后 当您想使用 Analytics 时,您需要通过调用设置默认值:
FirebaseApp firebaseApp =
FirebaseApp.initializeApp(Context, FirebaseOptions,"[DEFAULT]");
请记住,只有这个 DEFAULT firebase 应用程序将用于分析
但首先您需要删除清单中的初始化提供程序
<!--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
首先
- 您是否通过放入清单 tools:node="remove" 删除了默认提供程序?
- 你有没有 初始化 ['DEFAULT'] firebase 应用程序 如我所述
- 您是否检查 ['DEFAULT'] firebase 应用程序在发送任何事件之前是否已初始化?
广告 1) error: Missing 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/or 使用 firebase 之前,通过反射 将占位符 ID 更改为其他 ID:
hint how to
2) 通过自己的资源 class 实现 Resources.getString(R.string.google_app_id) 调用为占位符 ID 提供自己的文本:
an example how to achieve it(通过id添加新资源)
如果您通过反射正确更改 R 字段或用自己的文本替换对 Resources.getString(R.string.google_app_id) 的调用 您将不会收到消息错误的应用程序 ID:"fuck_you_google"
&祝你好运:)
您的应用中可以有多个 Firebase 实例
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("Your AppId") // Required for Analytics.
.setApiKey("You ApiKey") // Required for Auth.
.setDatabaseUrl("Your DB Url") // If you wanted to
.build();
FirebaseApp.initializeApp(context, options, "CompanyA");
您可以通过
获取 Firebase 实例FirebaseApp appCompanyA = FirebaseApp.getInstance("CompanyA");
您可以查看使用多个 Firebase 实例使用 Auth 和实时数据库的完整示例 here
我将把这个解决方案留在这里,它可能会与可能需要这个的人的另一个答案重复
希望对您有所帮助:)