Skip to content

Commit

Permalink
fix: analytics fixes
Browse files Browse the repository at this point in the history
- fixed issue with DbxAnalyticsService not using the config's user source value
- added DbxFirebaseAnalyticsUserEventsListener for sending user login/logout analytics events
  • Loading branch information
dereekb committed Jan 5, 2023
1 parent b411660 commit f21e0d6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
8 changes: 6 additions & 2 deletions apps/demo/src/root.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FormlyModule } from '@ngx-formly/core';
import { defaultValidationMessages } from '@dereekb/dbx-form';
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
import { RootFirebaseModule } from './root.firebase.module';
import { DbxFirebaseAnalyticsUserSource, DbxFirebaseLoginModule } from '@dereekb/dbx-firebase';
import { DbxFirebaseAnalyticsUserEventsListener, DbxFirebaseAnalyticsUserSource, DbxFirebaseLoginModule } from '@dereekb/dbx-firebase';
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { metaReducers, ROOT_REDUCER } from './app/state/app.state';
Expand Down Expand Up @@ -155,4 +155,8 @@ export function makeSegmentConfig(): DbxAnalyticsSegmentApiServiceConfig {
],
bootstrap: [UIView]
})
export class RootModule {}
export class RootModule {
constructor(readonly dbxFirebaseAnalyticsUserEventsListener: DbxFirebaseAnalyticsUserEventsListener) {
this.dbxFirebaseAnalyticsUserEventsListener.init();
}
}
7 changes: 4 additions & 3 deletions packages/dbx-analytics/src/lib/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable, Subject, BehaviorSubject, of, Subscription, first, shareReplay, switchMap } from 'rxjs';
import { Inject, Injectable, Optional } from '@angular/core';
import { SubscriptionObject, filterMaybe } from '@dereekb/rxjs';
import { SubscriptionObject, filterMaybe, tapLog } from '@dereekb/rxjs';
import { DbxAnalyticsEvent, DbxAnalyticsEventData, DbxAnalyticsEventName, DbxAnalyticsUser, NewUserAnalyticsEventData, DbxUserAnalyticsEvent } from './analytics';
import { DbxAnalyticsStreamEvent, DbxAnalyticsStreamEventType } from './analytics.stream';
import { Maybe, Destroyable } from '@dereekb/util';
Expand Down Expand Up @@ -102,9 +102,10 @@ export class DbxAnalyticsService implements DbxAnalyticsEventStreamService, DbxA
private _userSourceSub = new SubscriptionObject();
private _loggerSub = new SubscriptionObject();

constructor(private _config: DbxAnalyticsServiceConfiguration, @Optional() @Inject(DbxAnalyticsUserSource) userSource: Maybe<DbxAnalyticsUserSource> = _config.userSource) {
constructor(private _config: DbxAnalyticsServiceConfiguration, @Optional() @Inject(DbxAnalyticsUserSource) userSource?: Maybe<DbxAnalyticsUserSource>) {
this._init();

userSource = userSource || _config.userSource;
if (userSource) {
this.setUserSource(userSource);
}
Expand Down Expand Up @@ -237,7 +238,7 @@ export class DbxAnalyticsService implements DbxAnalyticsEventStreamService, DbxA

// Create a new subscription
this._loggerSub.subscription = this._subject.subscribe((x) => {
console.log(`AnalyticsService: Analytics Event - ${DbxAnalyticsStreamEventType[x.type]} User: ${x.userId} Data: ${JSON.stringify(x.event)}.`);
console.log(`AnalyticsService: Analytics Event - ${DbxAnalyticsStreamEventType[x.type]} | User: ${x.userId} | Data: ${JSON.stringify(x.event)}.`);
});
}

Expand Down
38 changes: 38 additions & 0 deletions packages/dbx-firebase/src/lib/analytics/analytics.user.events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable } from '@angular/core';
import { DbxAnalyticsService, DbxAnalyticsUser, DbxAnalyticsUserProperties, DbxAnalyticsUserSource } from '@dereekb/dbx-analytics';
import { filterMaybe, SubscriptionObject } from '@dereekb/rxjs';
import { Destroyable, FactoryWithRequiredInput, Initialized, Maybe } from '@dereekb/util';
import { BehaviorSubject, map, Observable, of, switchMap, shareReplay, combineLatest, first } from 'rxjs';
import { AuthUserInfo } from '../auth/auth';
import { DbxFirebaseAuthService } from '../auth/service/firebase.auth.service';
import { DbxFirebaseAnalyticsUserSource } from './analytics.user.source';

/**
* Service that listens for DbxFirebaseAuthService changes and emits them a user events.
*/
@Injectable({
providedIn: 'root'
})
export class DbxFirebaseAnalyticsUserEventsListener implements Initialized, Destroyable {
private _loginSub = new SubscriptionObject();
private _logoutSub = new SubscriptionObject();

constructor(readonly dbxFirebaseAuthService: DbxFirebaseAuthService, readonly dbxFirebaseAnalyticsUserSource: DbxFirebaseAnalyticsUserSource, readonly dbxAnalyticsService: DbxAnalyticsService) {}

init(): void {
this._loginSub.subscription = this.dbxFirebaseAuthService.onLogIn$.subscribe(() => {
this.dbxFirebaseAnalyticsUserSource.analyticsUser$.pipe(filterMaybe(), first()).subscribe((analyticsUser) => {
this.dbxAnalyticsService.sendUserLoginEvent(analyticsUser);
});
});

this._logoutSub.subscription = this.dbxFirebaseAuthService.onLogOut$.subscribe(() => {
this.dbxAnalyticsService.sendUserLogoutEvent();
});
}

destroy(): void {
this._loginSub.destroy();
this._logoutSub.destroy();
}
}
1 change: 1 addition & 0 deletions packages/dbx-firebase/src/lib/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './analytics.user.events';
export * from './analytics.user.source';

0 comments on commit f21e0d6

Please sign in to comment.