@@ -22,19 +22,21 @@ export class ScreenTrackingService implements OnDestroy {
22
22
private disposable : Subscription | undefined ;
23
23
24
24
constructor (
25
- private analytics : AngularFireAnalytics ,
26
- private router :Router ,
27
- @Optional ( ) @Inject ( AUTOMATICALLY_SET_CURRENT_SCREEN ) private automaticallySetCurrentScreen :boolean | null ,
28
- @Optional ( ) @Inject ( AUTOMATICALLY_LOG_SCREEN_VIEWS ) private automaticallyLogScreenViews :boolean | null ,
29
- @Optional ( ) @Inject ( APP_VERSION ) private providedAppVersion :string | null ,
30
- @Optional ( ) @Inject ( APP_NAME ) private providedAppName :string | null ,
31
- private zone : NgZone
25
+ analytics : AngularFireAnalytics ,
26
+ @ Optional ( ) router :Router ,
27
+ @Optional ( ) @Inject ( AUTOMATICALLY_SET_CURRENT_SCREEN ) automaticallySetCurrentScreen :boolean | null ,
28
+ @Optional ( ) @Inject ( AUTOMATICALLY_LOG_SCREEN_VIEWS ) automaticallyLogScreenViews :boolean | null ,
29
+ @Optional ( ) @Inject ( APP_VERSION ) providedAppVersion :string | null ,
30
+ @Optional ( ) @Inject ( APP_NAME ) providedAppName :string | null ,
31
+ zone : NgZone
32
32
) {
33
- if ( this . automaticallySetCurrentScreen !== false || this . automaticallyLogScreenViews !== false ) {
34
- const app_name = this . providedAppName || DEFAULT_APP_NAME ;
35
- const app_version = this . providedAppVersion || DEFAULT_APP_VERSION ;
36
- const activationEndEvents = this . router . events . pipe ( filter < ActivationEnd > ( e => e instanceof ActivationEnd ) ) ;
37
- const navigationEndEvents = this . router . events . pipe ( filter < NavigationEnd > ( e => e instanceof NavigationEnd ) ) ;
33
+ if ( ! router ) {
34
+ // TODO warning about Router
35
+ } else if ( automaticallySetCurrentScreen !== false || automaticallyLogScreenViews !== false ) {
36
+ const app_name = providedAppName || DEFAULT_APP_NAME ;
37
+ const app_version = providedAppVersion || DEFAULT_APP_VERSION ;
38
+ const activationEndEvents = router . events . pipe ( filter < ActivationEnd > ( e => e instanceof ActivationEnd ) ) ;
39
+ const navigationEndEvents = router . events . pipe ( filter < NavigationEnd > ( e => e instanceof NavigationEnd ) ) ;
38
40
this . disposable = navigationEndEvents . pipe (
39
41
withLatestFrom ( activationEndEvents ) ,
40
42
switchMap ( ( [ navigationEnd , activationEnd ] ) => {
@@ -43,26 +45,26 @@ export class ScreenTrackingService implements OnDestroy {
43
45
const outlet = activationEnd . snapshot . outlet ;
44
46
const component = activationEnd . snapshot . component ;
45
47
const ret = new Array < Promise < void > > ( ) ;
46
- if ( this . automaticallyLogScreenViews !== false ) {
48
+ if ( automaticallyLogScreenViews !== false ) {
47
49
if ( component ) {
48
50
const screen_class = component . hasOwnProperty ( 'name' ) && ( component as any ) . name || component . toString ( ) ;
49
- ret . push ( this . analytics . logEvent ( "screen_view" , { app_name, screen_class, app_version, screen_name, outlet, url } ) ) ;
51
+ ret . push ( analytics . logEvent ( "screen_view" , { app_name, screen_class, app_version, screen_name, outlet, url } ) ) ;
50
52
} else if ( activationEnd . snapshot . routeConfig && activationEnd . snapshot . routeConfig . loadChildren ) {
51
53
ret . push ( ( activationEnd . snapshot . routeConfig . loadChildren as any ) ( ) . then ( ( child :any ) => {
52
54
const screen_class = child . name ;
53
55
console . log ( "logEvent" , "screen_view" , { app_name, screen_class, app_version, screen_name, outlet, url } ) ;
54
- return this . analytics . logEvent ( "screen_view" , { app_name, screen_class, app_version, screen_name, outlet, url } ) ;
56
+ return analytics . logEvent ( "screen_view" , { app_name, screen_class, app_version, screen_name, outlet, url } ) ;
55
57
} ) ) ;
56
58
} else {
57
- ret . push ( this . analytics . logEvent ( "screen_view" , { app_name, app_version, screen_name, outlet, url } ) ) ;
59
+ ret . push ( analytics . logEvent ( "screen_view" , { app_name, app_version, screen_name, outlet, url } ) ) ;
58
60
}
59
61
}
60
- if ( this . automaticallySetCurrentScreen !== false ) {
61
- ret . push ( this . analytics . setCurrentScreen ( screen_name || url , { global : outlet == "primary" } ) ) ;
62
+ if ( automaticallySetCurrentScreen !== false ) {
63
+ ret . push ( analytics . setCurrentScreen ( screen_name || url , { global : outlet == "primary" } ) ) ;
62
64
}
63
65
return Promise . all ( ret ) ;
64
66
} ) ,
65
- runOutsideAngular ( this . zone )
67
+ runOutsideAngular ( zone )
66
68
) . subscribe ( ) ;
67
69
}
68
70
}
0 commit comments