Use multiple firebase accounts in single android app for google analytics

前端 未结 2 476
不思量自难忘°
不思量自难忘° 2020-12-02 23:48

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

相关标签:
2条回答
  • 2020-12-03 00:32

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

    0 讨论(0)
  • 2020-12-03 00:44

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

    0 讨论(0)
提交回复
热议问题