Firebase remote config condition by userId
Asked Answered
S

1

14

I integrated Firebase into my Android project, to get a different parameter value for different application user. I did the following:

  1. Setup users in my Firebase Projectenter image description here
  2. Created audiences to match the users: enter image description here UIDs were AAAAAAA..., and BBBBBBB... accordingly.
  3. Created a parameter in Remote Config section:enter image description here
  4. Added conditions for this parameter: enter image description here and set values for the conditions: enter image description here
  5. Entered the following code to sign in the user from the application:

    Task resultTask =   
        firebaseAuth.signInWithEmailAndPassword("[email protected]", password);
  6. Made sure sign in was successful.
  7. Then I tried to fetch the remote config parameter:
    firebaseRemoteConfig.fetch() 
                    .addOnCompleteListener(new OnCompleteListener() {
                        @Override
                        public void onComplete(@NonNull Task task) {
                            if (task.isSuccessful()) {
                                // Once the config is successfully fetched it must be activated before newly fetched
                                // values are returned.
                                firebaseRemoteConfig.activateFetched();
                                Log.d(TAG, firebaseRemoteConfig.getString("MyParameter"));
                            } else {
                                Log.d(TAG, "fetch firebase remote config failed. Reason = " + task.getException());
                            }
                        }
                    });

The result was that I always got the default value: DefaultValue

What did I do wrong? What did I miss?

Smithy answered 18/8, 2016 at 9:1 Comment(0)
S
15

After some investigation I figured that Firebase Analytics and Firebase Authentication are 2 different modules which don't automatically inter-connect.

Using Firebase Authentication did not automatically identify the user as part of the particular audience, as I expected.

I needed to tell Firebase Analytics, that the current user has the specific user ID, so it can match it to the relevant audience. I added the following code to the sign-in onComplete callback:

Task<AuthResult> resultTask =   
    firebaseAuth.signInWithEmailAndPassword("[email protected]", password); 
resultTask.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            // Task completed successfully
            if (task.isSuccessful()) {
               firebaseAnalytics.setUserId(task.getResult().getUser().getUid());
            } else {
                Log.d(TAG, "signInWithEmail firebase failed");
            }
        }
    });

The important line is:

firebaseAnalytics.setUserId(task.getResult().getUser().getUid());

Some things to note:

  1. Once a user is in an audience it can never exit the audience, which means, if you change the user in the app from the same device, you will get a match to 2 audiences: The new audience, and the previous one.
  2. While testing this I had to fetch the remote config parameter very frequently. Although I set the cache expiration to 0, I had a different problem: The Firebase server responded sometime with throttling exception. So, while testing make sure not to throttle the server or you'll have to wait a long time between tests.
  3. I'm not sure, but it seams that Firebase Analytics keeps track of the app user with a different id then the one defined with Firebase Authentication. I think, this is why if you sign-in with different user id in Firebase Authentication from the same device, you still match to the audience that was setup for the previuos Firebase user ID.
Smithy answered 18/8, 2016 at 9:1 Comment(3)
Does the user have to log in for the analytics to recognize their unique ID?Fishgig
@IgorGanapolsky: Yes, sign in is required, so Firebase records the unique ID.Smithy
Identifying Analytics data by auth user id, which could be tied back to personally identifiable data such as email address and demographic data, may be against the terms and privacy guidelines. It is now possible to make dynamic user groups, so they are no longer permanent residences of an audience.Meacham

© 2022 - 2024 — McMap. All rights reserved.