import { isPlatformServer } from '@angular/common' ; import { Inject , Injectable , NgZone , OnDestroy , PLATFORM_ID } from '@angular/core' ; import { AngularFireAnalytics } from './analytics' ; import { AngularFireAuth } from '@angular/fire/compat/auth' ; import { Subscription } from 'rxjs' ; import firebase from 'firebase/compat/app' ; import { VERSION } from '@angular/fire' ; @ Injectable ( ) export class UserTrackingService implements OnDestroy { initialized : Promise < void > ; private disposables : Array < Subscription > = [ ] ; constructor ( analytics , @ Inject ( PLATFORM_ID ) platformId , auth , zone ) { firebase . registerVersion ( 'angularfire' , VERSION . full , 'compat-user-tracking' ) ; if ( ! isPlatformServer ( platformId ) ) { let resolveInitialized ; this . initialized = zone . runOutsideAngular ( ( ) => new Promise ( resolve => resolveInitialized = resolve ) ) ; this . disposables = [ auth . authState . subscribe ( user => { analytics . setUserId ( user ?. uid ) ; resolveInitialized ( ) ; } ) , auth . credential . subscribe ( credential => { if ( credential ) { const method = credential . user . isAnonymous ? 'anonymous' : credential . additionalUserInfo . providerId ; if ( credential . additionalUserInfo . isNewUser ) { analytics . logEvent ( 'sign_up' , { method } ) ; } analytics . logEvent ( 'login' , { method } ) ; } } ) ] ; } else { this . initialized = Promise . resolve ( ) ; } } ngOnDestroy ( ) { this . disposables . forEach ( it => it . unsubscribe ( ) ) ; } }