Google Analytics is an app measurement solution, available at no charge, that provides insight on app usage and user engagement.
As a prerequisite, ensure that AngularFire
has been added to your project via
ng add @angular/fire
Provide a Analytics instance in the application's app.config.ts
:
import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
import { provideAnalytics, getAnalytics } from '@angular/fire/analytics';
export const appConfig: ApplicationConfig = {
providers: [
provideFirebaseApp(() => initializeApp({ ... })),
provideAnalytics(() => getAnalytics()),
...
],
...,
}
Next inject Analytics
into your component:
import { Component, inject } from '@angular/core';
import { Analytics } from '@angular/fire/analytics';
@Component({ ... })
export class UserProfileComponent {
private analytics = inject(Analytics);
...
}
AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API.
Update the imports from import { ... } from 'firebase/analytics'
to import { ... } from '@angular/fire/analytics'
and follow the official documentation.