Firebase Remote Configuration — Making it Work for You | by tomerpacific | September 2023

[ad_1]
Remote configurations are useful because they allow you to change the behavior of your application without having to release another version. A prime example is the use of remote configurations to decide whether a feature should be enabled or disabled. This way you can gradually deploy it to production or test it to see how users respond.
Usually, if you want your application to have this behavior, you will need to develop your server and its logic. But we live in an age of technology and innovation, so someone has created a tool that can help us minimize our development time and help us in this endeavor.
This tool is called Firebase Remote Config (surprise, surprise).
Part of the full Firebase suite also includes Remote Config. You can access it in your project’s Firebase console under the Release & Monitor section on the left sidebar.
You can define your remote configurations in two ways:
- Via Firebase
- Use a template file in JSON format
We will focus on the first option, because the second option is the less intuitive approach.
In Firebase Remote Config, you can define one or more keys that will be defined in these configurations. These keys can be of the following type:
Each key you set has a few other settings that you may find useful. On the one hand, you can set it to a default value (i.e. false for a boolean) or make it use a value that you have defined in your application. But the cool thing you can do, if you click the Add New button at the bottom left, is allow the value of this key to be determined by other factors:
- Conditional value
- Experience
- Personalization
Once you have finished adding a key, be sure to publish your changes so they are deployed
Conditional value
Configure how a value will be set for a selection of users based on various conditions.
Here you can decide what you want to test and how. You will discover several options if you click on the “Applies if” drop-down menu. To illustrate how to use this feature, let’s say you want to target iOS users in the United States. To do this, in the “Applies if” drop-down list, choose Platform, then iOS. After that, you can press the and buttons to add a condition for Country/Region and choose United States.
Also make sure to name your condition, otherwise the Create Condition button will not be enabled.
Notice how the last field in the new condition definition window tells you how many users will be affected by this condition.
Experience
This option allows you to modify the behavior of a value in your remote configurations before taking effect on all your users.
- In the first step, you fill in the name and description of your experience
- Then you choose which application to target and how many users will be affected (in percentage)
- The third step is to set up the metrics to measure this experience. There is a main metric and additional metrics
- Finally, you can decide the number of A/B test groups for this experiment
Personalization
Last but not least, the ability to tailor a specific value of your remote configurations to a user based on their own behavior.
You can define the values that the algorithm can provide to the user based on their behavior. These will be chosen based on a goal that you define (Step 2). This goal can range from the user’s engagement time to the number of clicks they make. In step 3, you set a condition that will target users to become personalized. Finally, in step 4, you add the name and description of this customization.
Each option has much more to offer than I’ve described here, so if you’re interested in learning more, you can use one of the reference links at the bottom. Now that we understand what Remote Config is, let’s see how we can add it to our application.
Before you can do anything about applying remote configurations, you need to make sure to add Firebase to your Android project. This has been documented here. After doing this, follow these steps:
- Add the Firebase Remote Config Library to your project
implementation 'com.google.firebase:firebase-config-ktx'
There is an option to also import the Firebase Analytics module, but it is not required for remote setups. It is used in other areas of remote configurations, such as setting a condition based on a specific event that occurs.
2. After syncing your project, you can access the RemoteConfig
object with this command:
val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig
3. You can define how often your remote configurations will be retrieved and therefore updated. When you’re still developing your app, it’s helpful to set this number relatively low.
val remoteConfigSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 2000
}
If you set the
minimumFetchIntervalInSeconds
be too low, Firebase will launch a FirebaseRemoteConfigFetchThrottledException
4. Set configuration settings
remoteConfig.setConfigSettingsAsync(remoteConfigSettings)
5. You can have application defaults for your remote configurations. These can be created as an XML file in the XML directory in the res folder. This is what the code looks like:
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
This XML file should have an underlying element of a map to wrap all of your default values. For example, let’s say we have defined a key in remote configurations called my_key
whose value is 1
. The XML for the default values will look like this:
<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
<entry>
<key>my_key</key>
<value>1</value>
</entry>
</defaultsMap>
Remote configurations must be retrieved and activated. The fetch action retrieves and stores your remote configurations in the Remote Config object. The activation part is about making these values available to your application. This is why there are two API methods:
fetch
(and use later activate)
remoteConfig.fetch()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
//Remote Configurations fetch successfully
}
}
.addOnFailureListener { error ->
//Remote Configurations fetch failure
}-------------------------
remoteConfig.activate()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
//Remote Configurations activation success
}
}
.addOnFailureListener { error ->
//Remote Configurations activation failure
}
remoteConfig.fetchAndActivate()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
//Remote Configurations fetched and activated successfully
}
}.addOnFailureListener { error ->
//Remote Configurations fetched and activated failure
}
6. Now that our remote configurations have been retrieved and activated, we can access and use them in our application. We can do this by accessing the remoteConfig
object and using one of the getter methods depending on the type of value we define:
val myRemoteConfigValue: String = remoteConfig.getString("my_key")
Since your application will rely on remote configurations for its operation (or part of it), it is crucial to decide how the application will behave if it does not arrive or if it takes too long to receive a response. Essentially, there are two ways to handle loading remote configurations:
- Your application starts and waits for remote configurations to be enabled
- Your application starts and does not wait for remote configuration to be enabled. Instead opt to use remote configurations on the second run of the application
There are good and bad implications for each of these methods, and it’s up to you to decide which one is best for your application. If you choose the first option, you can add a loading screen that expires after a certain period of time. Or, if you choose the second option, you will create a default mechanism for your application’s features and how they should work when the configuration has not yet been received.
[ad_2]
Source link