Angular13 with Firebase Remote Config
Integrate Firebase Remote Config in an Angular App using AngularFire.
This article will help you to integrate the Firebase Remote Config module into an Angular web application that already has the Firebase initialized.
Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring a code upgrade.
Get started
I. Add Remote Config:
Firebase recommends using AngularFire Framework Library to bind Firebase to an Angular application. So, add AngularFire to your application for easy integration. Use the following command to add the library:
ng add @angular/fire
Maintain the below version compatibility for a smooth integration and working.
II. Code Setup:
Your app.module.ts file will look like below:
import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireRemoteConfigModule, DEFAULTS, SETTINGS, AngularFireRemoteConfig } from '@angular/fire/compat/remote-config';@NgModule({
imports: [
AngularFireModule.initializeApp(environment.firebase),
AngularFireRemoteConfigModule
],
providers: [
{ provide: DEFAULTS, useValue: { enableAwesome: true } },
{
provide: SETTINGS, useFactory: () => isDevMode() ? { minimumFetchIntervalMillis: 10_000 } : {}
}
]
});
export class AppModule {
constructor(private remoteConfig: AngularFireRemoteConfig) {
this.remoteConfig.fetchAndActivate();
}
}
AngularFireRemoteConfig
will dynamically import and configure firebase/remote-config.SETTINGS
will allow you to configure Firebase Remote Config. Also, you can replace the functionisDevMode()
with your logic.DEFAULTS ({[key: string]: string | number | boolean})
tellsAngularFireRemoteConfig
to emit the provided defaults first. In the above code, the default value forenableAwsome
istrue
.fetchAndActivate()
performs fetch and activate operations.
In your component, you can then do the Dependency Injection forAngularFireRemoteConfig
and make calls against the SDK.
import { AngularFireRemoteConfig } from '@angular/fire/compat/remote-config';constructor(private remoteConfig: AngularFireRemoteConfig) { }
If you are using some shared service, probably you can do like below:
import { Injectable } from '@angular/core';
import { AngularFireRemoteConfig } from '@angular/fire/compat/remote-config';@Injectable()
export class RemoteConfigService {
constructor(private remoteConfig: AngularFireRemoteConfig) { } /**
* Get remote config value by key
*/
public async getValueByKey(key: string): Promise<any> {
return this.remoteConfig.getNumber(key).then((value) => {
return value;
}).catch((err) => {
return err;
});
}
}
In the above code, we have used getNumber()
function to get the value of the given key as a number. Similarly, AngularFireRemoteConfig
provides many other functions to get the value.
III. Firebase console:
For first time users, the console looks like the below:
Use the Create configuration button to add the key-value pair with an optional description. Then, using the Publish changes button you can publish the changes. Once you publish, changes are immediately available to your apps and users.
You can either modify or delete the configuration whenever you want by choosing the delete or edit options available in the Firebase console.
Now you can start using the full feature of Firebase Remote Config in your app:
- Quickly roll out changes to your app’s user base.
- Customize your app for segments of your user base.
- Run A/B tests to improve your app.
Good Luck!
If you are interested, check out my blog on Firebase Analytics with AngularFire