In this tutorial, you will learn how to use Firebase Remote Config in your web application. Firebase Remote Config is a cloud service that allows you to change the behavior and appearance of your app without requiring users to download an app update.
By the end of this tutorial, you will be able to:
You should have a basic understanding of JavaScript and HTML. Familiarity with Firebase would be beneficial but is not required.
First, you need to create a Firebase project:
Add project
, and follow the instructions to create a new project.Once the project is created, you will add Firebase to your app:
Add Firebase to your web app
.var config = {...}
) from the script that appears.Insert the copied script into your HTML file:
<!DOCTYPE html>
<html>
<head>
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.24.0/firebase-app.js"></script>
<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/7.24.0/firebase-remote-config.js"></script>
</head>
<body>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "your-api-key",
authDomain: "your-auth-domain",
databaseURL: "your-database-url",
projectId: "your-project-id",
storageBucket: "your-storage-bucket",
messagingSenderId: "your-messaging-sender-id",
appId: "your-app-id",
measurementId: "your-measurement-id"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
</body>
</html>
Now that Firebase is set up, you can configure Firebase Remote Config:
// Get Remote Config instance.
var remoteConfig = firebase.remoteConfig();
// Enable developer mode to allow frequent fetches.
// Do not use this in production!
remoteConfig.settings = {
minimumFetchIntervalMillis: 3600000,
};
// Set default Remote Config parameter values.
remoteConfig.defaultConfig = {
'welcome_message': 'Welcome'
};
// Fetch and activate.
remoteConfig.fetchAndActivate()
.then(() => {
// On success or failure, log the status.
console.log('Fetch and activate succeeded');
})
.catch((err) => {
console.error('Fetch and activate failed', err);
});
You can fetch and display a Remote Config value using the following code:
// Fetch and activate.
remoteConfig.fetchAndActivate()
.then(() => {
// Get the welcome message
var welcomeMessage = remoteConfig.getString('welcome_message');
// Display the welcome message
document.getElementById('welcome').textContent = welcomeMessage;
})
.catch((err) => {
console.error('Fetch and activate failed', err);
});
In this tutorial, you learned how to set up Firebase in a web application, configure Firebase Remote Config, fetch and activate values from Firebase Remote Config. As a next step, consider learning more about other Firebase features, such as Firebase Authentication and Firebase Cloud Firestore.
Create a web application that uses Firebase Remote Config to display different welcome messages to users based on their location.
Expand the application from Exercise 1 to include a feature flag that enables or disables a feature in your application.
Implement A/B testing in your application using Firebase Remote Config to test two different versions of a feature.
Remember to practice and experiment with different settings and values in Firebase Remote Config to better understand how it works. Happy coding!