|
1 | 1 | import { Injectable, Optional, NgZone, OnDestroy, ComponentFactoryResolver, Inject, PLATFORM_ID, Injector, NgModuleFactory } from '@angular/core';
|
2 |
| -import { Subscription, from, Observable, empty, of } from 'rxjs'; |
| 2 | +import { Subscription, from, Observable, of } from 'rxjs'; |
3 | 3 | import { filter, withLatestFrom, switchMap, map, tap, pairwise, startWith, groupBy, mergeMap } from 'rxjs/operators';
|
4 | 4 | import { Router, NavigationEnd, ActivationEnd, ROUTES } from '@angular/router';
|
5 | 5 | import { runOutsideAngular } from '@angular/fire';
|
6 | 6 | import { AngularFireAnalytics, DEBUG_MODE } from './analytics';
|
7 | 7 | import { User } from 'firebase/app';
|
8 | 8 | import { Title } from '@angular/platform-browser';
|
9 |
| -import { isPlatformBrowser } from '@angular/common'; |
| 9 | +import { isPlatformBrowser, isPlatformServer } from '@angular/common'; |
10 | 10 |
|
11 | 11 | const FIREBASE_EVENT_ORIGIN_KEY = 'firebase_event_origin';
|
12 | 12 | const FIREBASE_PREVIOUS_SCREEN_CLASS_KEY = 'firebase_previous_class';
|
@@ -101,7 +101,7 @@ export class ScreenTrackingService implements OnDestroy {
|
101 | 101 | const declarations = [].concat.apply([], (lazyModule[ANNOTATIONS] || []).map((f:any) => f.declarations));
|
102 | 102 | const selectors = [].concat.apply([], declarations.map((c:any) => (c[ANNOTATIONS] || []).map((f:any) => f.selector)));
|
103 | 103 | // should I just be grabbing the selector like this or should i match against the route component?
|
104 |
| - // const routerModule = lazyModule.ngInjectorDef.imports.find(i => !!i.ngModule); |
| 104 | + // const routerModule = lazyModule.ngInjectorDef.imports.find(i => i.ngModule && ....); |
105 | 105 | // const route = routerModule.providers[0].find(p => p.provide == ROUTES).useValue[0];
|
106 | 106 | return {...params, [SCREEN_CLASS_KEY]: selectors[0] || DEFAULT_SCREEN_CLASS};
|
107 | 107 | }
|
@@ -157,15 +157,21 @@ export class UserTrackingService implements OnDestroy {
|
157 | 157 | // TODO a user properties injector
|
158 | 158 | constructor(
|
159 | 159 | analytics: AngularFireAnalytics,
|
160 |
| - zone: NgZone |
| 160 | + zone: NgZone, |
| 161 | + @Inject(PLATFORM_ID) platformId:Object |
161 | 162 | ) {
|
162 |
| - this.disposable = from(analytics.app).pipe( |
163 |
| - // TODO can I hook into auth being loaded... |
164 |
| - map(app => app.auth()), |
165 |
| - switchMap(auth => auth ? new Observable<User>(auth.onAuthStateChanged.bind(auth)) : empty()), |
166 |
| - switchMap(user => analytics.setUserId(user ? user.uid : null!)), |
167 |
| - runOutsideAngular(zone) |
168 |
| - ).subscribe(); |
| 163 | + if (!isPlatformServer(platformId)) { |
| 164 | + zone.runOutsideAngular(() => { |
| 165 | + // @ts-ignore zap the import in the UMD |
| 166 | + this.disposable = from(import('firebase/auth')).pipe( |
| 167 | + switchMap(() => analytics.app), |
| 168 | + map(app => app.auth()), |
| 169 | + switchMap(auth => new Observable<User|null>(auth.onAuthStateChanged.bind(auth))), |
| 170 | + switchMap(user => analytics.setUserId(user ? user.uid : null!)), |
| 171 | + runOutsideAngular(zone) |
| 172 | + ).subscribe(); |
| 173 | + }); |
| 174 | + } |
169 | 175 | }
|
170 | 176 |
|
171 | 177 | ngOnDestroy() {
|
|
0 commit comments