Skip to content

Commit b8b351a

Browse files
committed
Router as optional, drop this/private from screen tracking
1 parent 075afe6 commit b8b351a

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/analytics/analytics.service.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ export class ScreenTrackingService implements OnDestroy {
2222
private disposable: Subscription|undefined;
2323

2424
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
3232
) {
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));
3840
this.disposable = navigationEndEvents.pipe(
3941
withLatestFrom(activationEndEvents),
4042
switchMap(([navigationEnd, activationEnd]) => {
@@ -43,26 +45,26 @@ export class ScreenTrackingService implements OnDestroy {
4345
const outlet = activationEnd.snapshot.outlet;
4446
const component = activationEnd.snapshot.component;
4547
const ret = new Array<Promise<void>>();
46-
if (this.automaticallyLogScreenViews !== false) {
48+
if (automaticallyLogScreenViews !== false) {
4749
if (component) {
4850
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 }));
5052
} else if (activationEnd.snapshot.routeConfig && activationEnd.snapshot.routeConfig.loadChildren) {
5153
ret.push((activationEnd.snapshot.routeConfig.loadChildren as any)().then((child:any) => {
5254
const screen_class = child.name;
5355
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 });
5557
}));
5658
} 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 }));
5860
}
5961
}
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" }));
6264
}
6365
return Promise.all(ret);
6466
}),
65-
runOutsideAngular(this.zone)
67+
runOutsideAngular(zone)
6668
).subscribe();
6769
}
6870
}

0 commit comments

Comments
 (0)