Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e5be927
Firebase v7, analytics and remote-config
jamesdaniels Oct 2, 2019
1cf961b
Cleaning up the DI tokens
jamesdaniels Oct 2, 2019
f5f67ad
Cleaning things up
jamesdaniels Oct 4, 2019
73413e8
DI and jazz
jamesdaniels Oct 4, 2019
dd0efb1
Bumping the tests
jamesdaniels Nov 8, 2019
dffca53
Adding to the root-spec
jamesdaniels Nov 9, 2019
9731e3c
Spelling is good.
jamesdaniels Nov 9, 2019
7308f9c
Merge branch 'master' into firebase-v7
jamesdaniels Nov 9, 2019
dcdf8bc
Have to include UMDs in the karma.conf
jamesdaniels Nov 9, 2019
6f71d46
Just importing performance is destablizing the app
jamesdaniels Nov 11, 2019
6638b9d
Merge branch 'master' into firebase-v7
jamesdaniels Nov 12, 2019
d7d52c8
Adding the zone arg to the app factory
jamesdaniels Nov 12, 2019
eb4bc00
First pass on the new RC API and the start of the AngularFireLazy effort
jamesdaniels Nov 13, 2019
89344cc
Update src/remote-config/tsconfig-test.json
jamesdaniels Nov 14, 2019
075afe6
Reworking things a bit
jamesdaniels Nov 20, 2019
b8b351a
Router as optional, drop this/private from screen tracking
jamesdaniels Nov 20, 2019
1e39052
Minor changes to RC
jamesdaniels Nov 20, 2019
768c21b
It's firebase_screen_class
jamesdaniels Nov 21, 2019
60c0cad
Reworking analytics
jamesdaniels Nov 22, 2019
9b2e920
current!
jamesdaniels Nov 22, 2019
916e069
Use _loadedConfig if available and scope screen_id on outlet
jamesdaniels Nov 22, 2019
5955925
Fixing the types to handle older Firebase SDKs
jamesdaniels Nov 22, 2019
659165e
SEMVER notes on the DI tokens
jamesdaniels Nov 22, 2019
62d90b9
Starting on the docs
jamesdaniels Nov 22, 2019
1a43ad1
Merge branch 'firebase-v7' of github.com:angular/angularfire2 into fi…
jamesdaniels Nov 22, 2019
67e1b55
Monitoring...
jamesdaniels Dec 9, 2019
91778ff
More work on analytics
jamesdaniels Dec 11, 2019
460170c
more expirimentation
jamesdaniels Dec 12, 2019
3c1ad1f
Flushing out RC more and fixing SSR issues
jamesdaniels Dec 14, 2019
e2d83c8
New RC API
jamesdaniels Dec 16, 2019
4601932
Mapping to objects and templates, budget pipe
jamesdaniels Dec 17, 2019
7fe92ed
More strict types
jamesdaniels Dec 17, 2019
d47dc3f
Fix proxy in Node, get component name for analytics in both JIT and AOT
jamesdaniels Dec 21, 2019
05c840b
Cleaning things up, beyond docs ready for release
jamesdaniels Jan 7, 2020
b46b382
Docs and cleanup
jamesdaniels Jan 7, 2020
ff65db8
Fixing analytics spec
jamesdaniels Jan 7, 2020
baaeccf
Adding more API to the docs
jamesdaniels Jan 7, 2020
04a3bb1
Further simplifications to the DI tokens
jamesdaniels Jan 7, 2020
c37fcb7
Add Analytics and RemoteConfig to install-and-setup
jamesdaniels Jan 7, 2020
6753a67
Merge branch 'master' into firebase-v7
jamesdaniels Jan 7, 2020
6f114c6
Our RC Value implements a Partial, so minors dont break
jamesdaniels Jan 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix proxy in Node, get component name for analytics in both JIT and AOT
  • Loading branch information
jamesdaniels committed Dec 21, 2019
commit d47dc3f8bfb093a6cd885226a1b4a79f4dc62120
50 changes: 36 additions & 14 deletions src/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, Optional, NgZone, OnDestroy, ComponentFactoryResolver, Inject, PLATFORM_ID } from '@angular/core';
import { Injectable, Optional, NgZone, OnDestroy, ComponentFactoryResolver, Inject, PLATFORM_ID, Injector, NgModuleFactory } from '@angular/core';
import { Subscription, from, Observable, empty, of } from 'rxjs';
import { filter, withLatestFrom, switchMap, map, tap, pairwise, startWith, groupBy, mergeMap } from 'rxjs/operators';
import { Router, NavigationEnd, ActivationEnd } from '@angular/router';
import { Router, NavigationEnd, ActivationEnd, ROUTES } from '@angular/router';
import { runOutsideAngular } from '@angular/fire';
import { AngularFireAnalytics } from './analytics';
import { AngularFireAnalytics, DEBUG_MODE } from './analytics';
import { User } from 'firebase/app';
import { Title } from '@angular/platform-browser';
import { isPlatformBrowser } from '@angular/common';
Expand All @@ -27,6 +27,8 @@ const DEFAULT_SCREEN_CLASS = '???';
const NG_PRIMARY_OUTLET = 'primary';
const SCREEN_INSTANCE_DELIMITER = '#';

const ANNOTATIONS = '__annotations__';

@Injectable()
export class ScreenTrackingService implements OnDestroy {

Expand All @@ -38,7 +40,9 @@ export class ScreenTrackingService implements OnDestroy {
@Optional() title:Title,
componentFactoryResolver: ComponentFactoryResolver,
@Inject(PLATFORM_ID) platformId:Object,
zone: NgZone
@Optional() @Inject(DEBUG_MODE) debugModeEnabled:boolean|null,
zone: NgZone,
injector: Injector
) {
if (!router || !isPlatformBrowser(platformId)) { return this }
zone.runOutsideAngular(() => {
Expand Down Expand Up @@ -70,23 +74,40 @@ export class ScreenTrackingService implements OnDestroy {
// it's lazy so it's not registered with componentFactoryResolver yet... seems a pain for a depreciated style
return of({...params, [SCREEN_CLASS_KEY]: loadChildren.split('#')[1]});
} else if (typeof component === 'string') {
// TODO figure out when this would this be a string
return of({...params, [SCREEN_CLASS_KEY]: component });
} else if (component) {
const componentFactory = componentFactoryResolver.resolveComponentFactory(component);
return of({...params, [SCREEN_CLASS_KEY]: componentFactory.selector });
} else if (loadChildren) {
const loadedChildren = loadChildren();
var loadedChildren$: Observable<any>;
// TODO clean up this handling...
// can componentFactorymoduleType take an ngmodulefactory or should i pass moduletype?
try { loadedChildren$ = from(zone.runOutsideAngular(() => loadedChildren as any)) } catch(_) { loadedChildren$ = of(loadedChildren as any) }
return loadedChildren$.pipe(map(child => {
const componentFactory = componentFactoryResolver.resolveComponentFactory(child);
return {...params, [SCREEN_CLASS_KEY]: componentFactory.selector };
}));
var loadedChildren$: Observable<any> = (loadedChildren instanceof Observable) ? loadedChildren : from(Promise.resolve(loadedChildren));
return loadedChildren$.pipe(
map(lazyModule => {
if (lazyModule instanceof NgModuleFactory) {
// AOT create an injector
const moduleRef = lazyModule.create(injector);
// INVESTIGATE is this the right way to get at the matching route?
const routes = moduleRef.injector.get(ROUTES);
const component = routes[0][0].component; // should i just be grabbing 0-0 here?
try {
const componentFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(component!);
return {...params, [SCREEN_CLASS_KEY]: componentFactory.selector};
} catch(_) {
return {...params, [SCREEN_CLASS_KEY]: DEFAULT_SCREEN_CLASS};
}
} else {
// JIT look at the annotations
// INVESTIGATE are there public APIs for this stuff?
const declarations = [].concat.apply([], (lazyModule[ANNOTATIONS] || []).map((f:any) => f.declarations));
const selectors = [].concat.apply([], declarations.map((c:any) => (c[ANNOTATIONS] || []).map((f:any) => f.selector)));
// should I just be grabbing the selector like this or should i match against the route component?
// const routerModule = lazyModule.ngInjectorDef.imports.find(i => !!i.ngModule);
// const route = routerModule.providers[0].find(p => p.provide == ROUTES).useValue[0];
return {...params, [SCREEN_CLASS_KEY]: selectors[0] || DEFAULT_SCREEN_CLASS};
}
})
);
} else {
// TODO figure out what forms of router events I might be missing
return of({...params, [SCREEN_CLASS_KEY]: DEFAULT_SCREEN_CLASS});
}
}),
Expand Down Expand Up @@ -116,6 +137,7 @@ export class ScreenTrackingService implements OnDestroy {
[FIREBASE_PREVIOUS_SCREEN_INSTANCE_ID_KEY]: prior[FIREBASE_SCREEN_INSTANCE_ID_KEY],
...current!
} : current!),
tap(params => debugModeEnabled && console.info(SCREEN_VIEW_EVENT, params)),
tap(params => zone.runOutsideAngular(() => analytics.logEvent(SCREEN_VIEW_EVENT, params)))
).subscribe();
});
Expand Down
9 changes: 3 additions & 6 deletions src/analytics/analytics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ReflectiveInjector, Provider } from '@angular/core';
import { TestBed, inject } from '@angular/core/testing';
import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseNameOrConfigToken } from '@angular/fire';
import { AngularFireAnalytics, AngularFireAnalyticsModule, AUTOMATICALLY_SET_CURRENT_SCREEN, AUTOMATICALLY_LOG_SCREEN_VIEWS, ANALYTICS_COLLECTION_ENABLED, AUTOMATICALLY_TRACK_USER_IDENTIFIER, APP_VERSION, APP_NAME } from '@angular/fire/analytics';
import { AngularFireAnalytics, AngularFireAnalyticsModule, ANALYTICS_COLLECTION_ENABLED, APP_VERSION, APP_NAME } from '@angular/fire/analytics';
import { COMMON_CONFIG } from './test-config';


Expand Down Expand Up @@ -32,7 +32,7 @@ describe('AngularFireAnalytics', () => {
});

it('should have the Firebase Functions instance', () => {
expect(analytics.analytics).toBeDefined();
expect(analytics.app).toBeDefined();
});

});
Expand All @@ -52,10 +52,7 @@ describe('AngularFireAnalytics with different app', () => {
providers: [
{ provide: FirebaseNameOrConfigToken, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG },
{ provide: AUTOMATICALLY_SET_CURRENT_SCREEN, useValue: true }
{ provide: AUTOMATICALLY_LOG_SCREEN_VIEWS, useValue: true },
{ provide: ANALYTICS_COLLECTION_ENABLED, useValue: true },
{ provide: AUTOMATICALLY_TRACK_USER_IDENTIFIER, useValue: true },
{ provide: APP_VERSION, useValue: '0.0' },
{ provide: APP_NAME, useValue: 'Test App!' }
]
Expand All @@ -78,7 +75,7 @@ describe('AngularFireAnalytics with different app', () => {
});

it('should have the Firebase Functions instance', () => {
expect(analytics.analytics).toBeDefined();
expect(analytics.app).toBeDefined();
});

});
Expand Down
31 changes: 16 additions & 15 deletions src/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,25 @@ export class AngularFireAnalytics {
zone: NgZone
) {

if (!isPlatformBrowser(platformId)) {
// TODO flush out non-browser support
if (isPlatformBrowser(platformId)) {

window[DATA_LAYER_NAME] = window[DATA_LAYER_NAME] || [];
this.gtag = window[GTAG_FUNCTION_NAME] || function() { window[DATA_LAYER_NAME].push(arguments) }
this.analyticsInitialized = zone.runOutsideAngular(() =>
new Promise(resolve => {
window[GTAG_FUNCTION_NAME] = (...args: any[]) => {
if (args[0] == 'js') { resolve() }
this.gtag(...args);
}
})
);

} else {

this.analyticsInitialized = Promise.resolve();
this.gtag = () => {}
// TODO fix the proxy for the server
return this;
}

window[DATA_LAYER_NAME] = window[DATA_LAYER_NAME] || [];
this.gtag = window[GTAG_FUNCTION_NAME] || function() { window[DATA_LAYER_NAME].push(arguments) }
this.analyticsInitialized = zone.runOutsideAngular(() =>
new Promise(resolve => {
window[GTAG_FUNCTION_NAME] = (...args: any[]) => {
if (args[0] == 'js') { resolve() }
this.gtag(...args);
}
})
);
}

if (providedAppName) { this.updateConfig({ [APP_NAME_KEY]: providedAppName }) }
if (providedAppVersion) { this.updateConfig({ [APP_VERSION_KEY]: providedAppVersion }) }
Expand Down
33 changes: 16 additions & 17 deletions src/core/angularfire2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,24 @@ export const ɵlazySDKProxy = (klass: any, observable: Observable<any>, zone: Ng
get: (_, name) => zone.runOutsideAngular(() =>
klass[name] || new Proxy(() =>
observable.toPromise().then(mod => {
if (mod) {
const ret = mod[name];
// TODO move to proper type guards
if (typeof ret == 'function') {
return ret.bind(mod);
} else if (ret && ret.then) {
return ret.then((res:any) => zone.run(() => res));
} else {
return zone.run(() => ret);
}
const ret = mod && mod[name];
// TODO move to proper type guards
if (typeof ret == 'function') {
return ret.bind(mod);
} else if (ret && ret.then) {
return ret.then((res:any) => zone.run(() => res));
} else {
// the module is not available, SSR maybe?
// TODO dig into this deeper, maybe return a never resolving promise?
return () => {};
return zone.run(() => ret);
}
}), {
get: (self, name) => self()[name],
// TODO handle callbacks
apply: (self, _, args) => self().then(it => it(...args))
})
get: (self, name) => {
const ret = self();
// TODO proxy so we can have apply?
return ret && ret[name] || (() => {});
},
// TODO handle callbacks as transparently as I can
apply: (self, _, args) => self().then(it => it && it(...args))
}
)
)
});
4 changes: 2 additions & 2 deletions src/remote-config/remote-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('AngularFireRemoteConfig', () => {
});

it('should have the Firebase Functions instance', () => {
expect(rc.remoteConfig).toBeDefined();
expect(rc.getValue).toBeDefined();
});

});
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('AngularFireRemoteConfig with different app', () => {
});

it('should have the Firebase Functions instance', () => {
expect(rc.remoteConfig).toBeDefined();
expect(rc.getValue).toBeDefined();
});

});
Expand Down