1- import { Injectable , Inject , Optional , NgZone , InjectionToken } from '@angular/core' ;
1+ import { Injectable , Inject , Optional , NgZone , InjectionToken , PLATFORM_ID } from '@angular/core' ;
22import { of } from 'rxjs' ;
3+ import { isPlatformBrowser } from '@angular/common' ;
34import { map , tap , shareReplay , switchMap } from 'rxjs/operators' ;
45import { FirebaseAppConfig , FirebaseOptions , runOutsideAngular , ɵlazySDKProxy , FirebaseAnalytics , FIREBASE_OPTIONS , FIREBASE_APP_NAME , _firebaseAppFactory } from '@angular/fire' ;
56import { analytics , app } from 'firebase' ;
@@ -15,9 +16,8 @@ const APP_VERSION_KEY = 'app_version';
1516const DEBUG_MODE_KEY = 'debug_mode' ;
1617const ANALYTICS_ID_FIELD = 'measurementId' ;
1718const GTAG_CONFIG_COMMAND = 'config' ;
18-
19- // TODO can we get this from js sdk?
20- const GTAG_FUNCTION = 'gtag' ;
19+ const GTAG_FUNCTION_NAME = 'gtag' ;
20+ const DATA_LAYER_NAME = 'dataLayer' ;
2121
2222// SEMVER: once we move to Typescript 3.6 use `PromiseProxy<analytics.Analytics>`
2323type AnalyticsProxy = {
@@ -36,17 +36,42 @@ export interface AngularFireAnalytics extends AnalyticsProxy {};
3636@Injectable ( )
3737export class AngularFireAnalytics {
3838
39- public updateConfig : ( options : { [ key :string ] : any } ) => void ;
39+ private gtag : ( ...args : any [ ] ) => { } ;
40+ private analyticsInitialized : Promise < void > ;
41+
42+ async updateConfig ( config : { [ key :string ] : any } ) {
43+ await this . analyticsInitialized ;
44+ this . gtag ( GTAG_CONFIG_COMMAND , this . options [ ANALYTICS_ID_FIELD ] , { ...config , update : true } ) ;
45+ } ;
4046
4147 constructor (
42- @Inject ( FIREBASE_OPTIONS ) options :FirebaseOptions ,
48+ @Inject ( FIREBASE_OPTIONS ) private options :FirebaseOptions ,
4349 @Optional ( ) @Inject ( FIREBASE_APP_NAME ) nameOrConfig :string | FirebaseAppConfig | null | undefined ,
4450 @Optional ( ) @Inject ( ANALYTICS_COLLECTION_ENABLED ) analyticsCollectionEnabled :boolean | null ,
4551 @Optional ( ) @Inject ( APP_VERSION ) providedAppVersion :string | null ,
4652 @Optional ( ) @Inject ( APP_NAME ) providedAppName :string | null ,
4753 @Optional ( ) @Inject ( DEBUG_MODE ) debugModeEnabled :boolean | null ,
54+ @Inject ( PLATFORM_ID ) platformId ,
4855 zone : NgZone
4956 ) {
57+
58+ if ( isPlatformBrowser ( platformId ) ) {
59+ window [ DATA_LAYER_NAME ] = window [ DATA_LAYER_NAME ] || [ ] ;
60+ this . gtag = window [ GTAG_FUNCTION_NAME ] || function ( ) { window [ DATA_LAYER_NAME ] . push ( arguments ) }
61+ this . analyticsInitialized = new Promise ( resolve => {
62+ window [ GTAG_FUNCTION_NAME ] = ( ...args : any [ ] ) => {
63+ if ( args [ 0 ] == 'js' ) { resolve ( ) }
64+ this . gtag ( ...args ) ;
65+ }
66+ } ) ;
67+ } else {
68+ this . analyticsInitialized = Promise . reject ( ) ;
69+ }
70+
71+ if ( providedAppName ) { this . updateConfig ( { [ APP_NAME_KEY ] : providedAppName } ) }
72+ if ( providedAppVersion ) { this . updateConfig ( { [ APP_VERSION_KEY ] : providedAppVersion } ) }
73+ if ( debugModeEnabled ) { this . updateConfig ( { [ DEBUG_MODE_KEY ] : 1 } ) }
74+
5075 const analytics = of ( undefined ) . pipe (
5176 // @ts -ignore zapping in the UMD in the build script
5277 switchMap ( ( ) => zone . runOutsideAngular ( ( ) => import ( 'firebase/analytics' ) ) ) ,
@@ -55,18 +80,11 @@ export class AngularFireAnalytics {
5580 map ( app => < analytics . Analytics > app . analytics ( ) ) ,
5681 tap ( analytics => {
5782 if ( analyticsCollectionEnabled === false ) { analytics . setAnalyticsCollectionEnabled ( false ) }
58- if ( providedAppName ) { this . updateConfig ( { [ APP_NAME_KEY ] : providedAppName } ) }
59- if ( providedAppVersion ) { this . updateConfig ( { [ APP_VERSION_KEY ] : providedAppVersion } ) }
60- if ( debugModeEnabled ) { this . updateConfig ( { [ DEBUG_MODE_KEY ] : 1 } ) }
6183 } ) ,
6284 runOutsideAngular ( zone ) ,
6385 shareReplay ( 1 )
6486 ) ;
6587
66- this . updateConfig = ( config : { [ key :string ] : any } ) => analytics . toPromise ( ) . then ( ( ) =>
67- window [ GTAG_FUNCTION ] ( GTAG_CONFIG_COMMAND , options [ ANALYTICS_ID_FIELD ] , { ...config , update : true } )
68- ) ;
69-
7088 return ɵlazySDKProxy ( this , analytics , zone ) ;
7189 }
7290
0 commit comments