Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple firebase accounts in single android app for google analytics

Tags:

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

like image 565
user230910 Avatar asked Aug 07 '17 12:08

user230910


People also ask

Can I have multiple Firebase accounts?

If you do allow multiple accounts with the same email address, your app's sign-in flow cannot rely on an email address to identify a user account. Allowing multiple accounts to have the same email address also changes the behavior of the following functions in the Web SDK: firebase. auth().

How multiple developers can work on the same Android app connected to a single Firebase console?

You can't have two projects of the same package name. Even if you delete it. It will take a least 4-5 days to get deleted fully from the developer's console. So the only solution is to generate a new SHA-1 key by custom signing the app by generating a signed apk from the android studio.

Can two apps use same Firebase?

Yes, You can use the same firebase database in more than one android application as below: In the Project Overview section of Firebase Console add an android application. For adding this application first you need to give that package name. Download config file in JSON format.


2 Answers

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 :)

like image 124
ceph3us Avatar answered Sep 28 '22 04:09

ceph3us


It's possible to have multiple Firebase instance in your apps

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");

Which you can get Firebase instances by

FirebaseApp appCompanyA = FirebaseApp.getInstance("CompanyA");

You can see the full example use of Auth and Realtime Database using multiple Firebase instance here

I'll leave this solution here which may duplicate with another answers for someone who might need this

Hope this help :)

like image 21
Yati Dumrongsukit Avatar answered Sep 28 '22 02:09

Yati Dumrongsukit