import { EventParameter , IAnalytics } from './common' ; import { firebase , FirebaseApp , serialize } from '@nativescript/firebase-core' ; import { ConsentStatus , ConsentType } from '.' ; export * from './common' ; declare const FIRApp ; let defaultAnalytics ; const fb = firebase ( ) ; Object . defineProperty ( fb , 'analytics' , { value : ( ) => { if ( ! defaultAnalytics ) { defaultAnalytics = new Analytics ( ) ; } return defaultAnalytics ; } , writable : false , } ) ; export class Analytics implements IAnalytics { #app : FirebaseApp ; constructor ( ) { if ( defaultAnalytics ) { return defaultAnalytics ; } defaultAnalytics = this ; } handleOpenURL ( url ) { try { FIRAnalytics . handleOpenURL ( NSURL . URLWithString ( url ) ) ; } catch ( e ) { } } handleUserActivity ( userActivity ) { FIRAnalytics . handleUserActivity ( userActivity ) ; } get app ( ) : FirebaseApp { if ( ! this . #app ) { this . #app = FirebaseApp . fromNative ( FIRApp . defaultApp ( ) ) ; } return this . #app ; } get appInstanceId ( ) : string { return FIRAnalytics ?. appInstanceID ?. ( ) ; } setSessionTimeoutInterval ( sessionTimeoutInterval ) { FIRAnalytics . setSessionTimeoutInterval ( sessionTimeoutInterval ) ; } setUserProperty ( key , value ) { FIRAnalytics . setUserPropertyStringForName ( value , key ) ; } setAnalyticsCollectionEnabled ( analyticsCollectionEnabled ) { FIRAnalytics . setAnalyticsCollectionEnabled ( analyticsCollectionEnabled ) ; } setUserId ( userId ) { FIRAnalytics . setUserID ( userId ) ; } logEvent ( name , parameters ) { FIRAnalytics . logEventWithNameParameters ( name , serialize ( parameters ) ) ; } resetAnalyticsData ( ) { FIRAnalytics . resetAnalyticsData ( ) ; } setDefaultEventParameters ( parameters ) { FIRAnalytics . setDefaultEventParameters ( serialize ( parameters ) ) ; } setConsent ( consentSettings ) { const dictionary = { } ; consentSettings . forEach ( ( value , key ) => { let nativeKey ; let nativeValue ; switch ( key ) { case ConsentType . Ad_Storage : nativeKey = FIRConsentTypeAdStorage ; break ; case ConsentType . Analytics_Storage : nativeKey = FIRConsentTypeAnalyticsStorage ; break ; } switch ( value ) { case ConsentStatus . Denied : nativeValue = FIRConsentStatusDenied ; break ; case ConsentStatus . Granted : nativeValue = FIRConsentStatusGranted ; break ; } if ( nativeKey && nativeValue ) { dictionary [ nativeKey ] = nativeValue ; } } ) ; FIRAnalytics . setConsent ( dictionary as any ) ; } }