import { Injectable , Injector , NgZone , OnDestroy } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { VERSION } from '@angular/fire' ; import { Auth , authState } from '@angular/fire/auth' ; import { registerVersion } from 'firebase/app' ; import { Analytics } from './analytics' ; import { setUserId , isSupported } from './firebase' ; @ Injectable ( ) export class UserTrackingService implements OnDestroy { public readonly initialized : Promise < void > ; private disposables : Array < Subscription > = [ ] ; constructor ( auth , zone , injector ) { registerVersion ( 'angularfire' , VERSION . full , 'user-tracking' ) ; let resolveInitialized ; this . initialized = zone . runOutsideAngular ( ( ) => new Promise ( resolve => { resolveInitialized = resolve ; } ) ) ; isSupported ( ) . then ( ( ) => { const analytics = injector . get ( Analytics ) ; if ( analytics ) { this . disposables = [ authState ( auth ) . subscribe ( user => { setUserId ( analytics , user ?. uid ) ; resolveInitialized ( ) ; } ) , ] ; } else { resolveInitialized ( ) ; } } ) ; } ngOnDestroy ( ) { this . disposables . forEach ( it => it . unsubscribe ( ) ) ; } }