@dereekb/dbx-analytics 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,309 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, Optional, Inject, Directive, Host, Input, NgModule } from '@angular/core';
3
+ import { SubscriptionObject, filterMaybe } from '@dereekb/rxjs';
4
+ import { filter, switchMap, shareReplay, first, tap } from 'rxjs/operators';
5
+ import { Subject, BehaviorSubject, of, merge } from 'rxjs';
6
+ import * as i1 from '@dereekb/dbx-core';
7
+ import { AbstractSubscriptionDirective } from '@dereekb/dbx-core';
8
+
9
+ var AnalyticsStreamEventType;
10
+ (function (AnalyticsStreamEventType) {
11
+ AnalyticsStreamEventType[AnalyticsStreamEventType["PageView"] = 0] = "PageView";
12
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserChange"] = 1] = "UserChange";
13
+ // User Events
14
+ AnalyticsStreamEventType[AnalyticsStreamEventType["NewUserEvent"] = 2] = "NewUserEvent";
15
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserLoginEvent"] = 3] = "UserLoginEvent";
16
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserLogoutEvent"] = 4] = "UserLogoutEvent";
17
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserPropertiesEvent"] = 5] = "UserPropertiesEvent";
18
+ // Events
19
+ AnalyticsStreamEventType[AnalyticsStreamEventType["Event"] = 6] = "Event";
20
+ })(AnalyticsStreamEventType || (AnalyticsStreamEventType = {}));
21
+
22
+ class DbNgxAnalyticsEventEmitterService {
23
+ }
24
+ class DbNgxAnalyticsEventStreamService {
25
+ }
26
+ class DbNgxAnalyticsUserSource {
27
+ }
28
+ class DbNgxAnalyticsServiceListener {
29
+ }
30
+ class AbstractAnalyticsServiceListener {
31
+ constructor() {
32
+ this._sub = new SubscriptionObject();
33
+ }
34
+ listenToService(service) {
35
+ this._service = service;
36
+ this._sub.subscription = service.events$.pipe(filter((e) => this.filterEvent(e)))
37
+ .subscribe((event) => this.updateOnStreamEvent(event));
38
+ }
39
+ filterEvent(streamEvent) {
40
+ return true;
41
+ }
42
+ }
43
+ class DbNgxAnalyticsServiceConfiguration {
44
+ constructor() {
45
+ this.listeners = [];
46
+ }
47
+ }
48
+ class AnalyticsStreamEventAnalyticsEventWrapper {
49
+ constructor(event, type = AnalyticsStreamEventType.Event) {
50
+ this.event = event;
51
+ this.type = type;
52
+ }
53
+ get user() {
54
+ return this.event.user;
55
+ }
56
+ get userId() {
57
+ return (this.user) ? this.user.user : undefined;
58
+ }
59
+ }
60
+ /**
61
+ * Primary analytics service that emits analytics events that components can listen to.
62
+ */
63
+ class DbNgxAnalyticsService {
64
+ constructor(_config, userSource = _config.userSource) {
65
+ this._config = _config;
66
+ this._subject = new Subject();
67
+ this.events$ = this._subject.asObservable();
68
+ this._userSource = new BehaviorSubject(undefined);
69
+ this.user$ = this._userSource.pipe(switchMap(x => (x) ? x.analyticsUser$ : of(undefined)), shareReplay(1));
70
+ this._userSourceSub = new SubscriptionObject();
71
+ this._loggerSub = new SubscriptionObject();
72
+ this._init();
73
+ if (userSource) {
74
+ this.setUserSource(userSource);
75
+ }
76
+ }
77
+ // MARK: Source
78
+ /**
79
+ * Sets the user directly.
80
+ */
81
+ setUser(user) {
82
+ let source;
83
+ if (user) {
84
+ source = { analyticsUser$: of(user) };
85
+ }
86
+ this._userSource.next(source);
87
+ }
88
+ setUserSource(source) {
89
+ this._userSource.next(source);
90
+ }
91
+ // MARK: DbNgxAnalyticsEventEmitterService
92
+ /**
93
+ * Sends an event.
94
+ */
95
+ sendNewUserEvent(user, data) {
96
+ this.sendNextEvent({
97
+ name: DbNgxAnalyticsService.USER_REGISTRATION_EVENT_NAME,
98
+ data
99
+ }, AnalyticsStreamEventType.NewUserEvent, user);
100
+ }
101
+ sendUserLoginEvent(user, data) {
102
+ this.sendNextEvent({
103
+ name: DbNgxAnalyticsService.USER_LOGIN_EVENT_NAME,
104
+ data
105
+ }, AnalyticsStreamEventType.UserLoginEvent, user);
106
+ }
107
+ sendUserLogoutEvent(data, clearUser = true) {
108
+ this.sendNextEvent({
109
+ name: DbNgxAnalyticsService.USER_LOGOUT_EVENT_NAME,
110
+ data
111
+ }, AnalyticsStreamEventType.UserLogoutEvent);
112
+ if (clearUser) {
113
+ this.setUser(undefined);
114
+ }
115
+ }
116
+ sendUserPropertiesEvent(user, data) {
117
+ this.sendNextEvent({
118
+ name: DbNgxAnalyticsService.USER_PROPERTIES_EVENT_NAME,
119
+ data
120
+ }, AnalyticsStreamEventType.UserPropertiesEvent, user);
121
+ }
122
+ sendEventData(name, data) {
123
+ return this.sendEvent({
124
+ name,
125
+ data
126
+ });
127
+ }
128
+ sendEventType(eventType) {
129
+ this.sendNextEvent({
130
+ name: eventType
131
+ }, AnalyticsStreamEventType.Event);
132
+ }
133
+ sendEvent(event) {
134
+ this.sendNextEvent(event, AnalyticsStreamEventType.Event);
135
+ }
136
+ sendPageView(page) {
137
+ this.sendNextEvent({
138
+ name: page
139
+ }, AnalyticsStreamEventType.PageView);
140
+ }
141
+ sendNextEvent(event = {}, type, userOverride) {
142
+ this.user$.pipe(first()).subscribe((analyticsUser) => {
143
+ const user = (userOverride != null) ? userOverride : analyticsUser;
144
+ const analyticsEvent = Object.assign(Object.assign({}, event), { user });
145
+ this.nextEvent(analyticsEvent, type);
146
+ });
147
+ }
148
+ nextEvent(event, type) {
149
+ const wrapper = new AnalyticsStreamEventAnalyticsEventWrapper(event, type);
150
+ this._subject.next(wrapper);
151
+ }
152
+ // MARK: Internal
153
+ _init() {
154
+ if (this._config.isProduction) {
155
+ // Initialize listeners.
156
+ this._config.listeners.forEach((listener) => {
157
+ listener.listenToService(this);
158
+ });
159
+ }
160
+ else {
161
+ console.warn('DbNgxAnalyticsService: Analytics not in production mode. All analytics events are ignored.');
162
+ }
163
+ if (this._config.logEvents || !this._config.isProduction) {
164
+ console.log('DbNgxAnalyticsService: Log analytics events enabled.');
165
+ // Create a new subscription
166
+ this._loggerSub.subscription = this._subject.subscribe((x) => {
167
+ console.log(`DbNgxAnalyticsService: Analytics Event - ${AnalyticsStreamEventType[x.type]} User: ${x.userId} Data: ${JSON.stringify(x.event)}.`);
168
+ });
169
+ }
170
+ this._userSourceSub.subscription = this.user$.subscribe(() => {
171
+ this.sendNextEvent({}, AnalyticsStreamEventType.UserChange);
172
+ });
173
+ }
174
+ destroy() {
175
+ this._userSourceSub.destroy();
176
+ this._loggerSub.destroy();
177
+ }
178
+ }
179
+ // TODO: Make these configurable.
180
+ DbNgxAnalyticsService.USER_REGISTRATION_EVENT_NAME = 'User Registered';
181
+ DbNgxAnalyticsService.USER_LOGIN_EVENT_NAME = 'User Login';
182
+ DbNgxAnalyticsService.USER_LOGOUT_EVENT_NAME = 'User Logout';
183
+ DbNgxAnalyticsService.USER_PROPERTIES_EVENT_NAME = 'User Properties';
184
+ DbNgxAnalyticsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsService, deps: [{ token: DbNgxAnalyticsServiceConfiguration }, { token: DbNgxAnalyticsUserSource, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
185
+ DbNgxAnalyticsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsService });
186
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsService, decorators: [{
187
+ type: Injectable
188
+ }], ctorParameters: function () {
189
+ return [{ type: DbNgxAnalyticsServiceConfiguration }, { type: undefined, decorators: [{
190
+ type: Optional
191
+ }, {
192
+ type: Inject,
193
+ args: [DbNgxAnalyticsUserSource]
194
+ }] }];
195
+ } });
196
+
197
+ var DbNgxActionAnalyticsTriggerType;
198
+ (function (DbNgxActionAnalyticsTriggerType) {
199
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["TRIGGER"] = 0] = "TRIGGER";
200
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["READY"] = 1] = "READY";
201
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["SUCCESS"] = 2] = "SUCCESS";
202
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["ERROR"] = 3] = "ERROR";
203
+ })(DbNgxActionAnalyticsTriggerType || (DbNgxActionAnalyticsTriggerType = {}));
204
+ /**
205
+ * Used to listen to an ActionContext and send analytical events based on action events.
206
+ */
207
+ class DbNgxActionAnalyticsDirective extends AbstractSubscriptionDirective {
208
+ constructor(source, analyticsService) {
209
+ super();
210
+ this.source = source;
211
+ this.analyticsService = analyticsService;
212
+ this._config = new BehaviorSubject(undefined);
213
+ this.config$ = this._config.pipe(filterMaybe(), shareReplay(1));
214
+ }
215
+ get config() {
216
+ return this._config.value;
217
+ }
218
+ set config(config) {
219
+ this._config.next(config);
220
+ }
221
+ ngOnInit() {
222
+ this.sub = this.config$.pipe(switchMap(({ onTriggered, onReady, onSuccess, onError }) => {
223
+ const triggerObs = [];
224
+ if (onTriggered) {
225
+ triggerObs.push(this.source.triggered$.pipe(tap(() => onTriggered(this.analyticsService))));
226
+ }
227
+ if (onReady) {
228
+ triggerObs.push(this.source.valueReady$.pipe(tap((value) => onReady(this.analyticsService, value))));
229
+ }
230
+ if (onSuccess) {
231
+ triggerObs.push(this.source.success$.pipe(tap((result) => onSuccess(this.analyticsService, result))));
232
+ }
233
+ if (onError) {
234
+ triggerObs.push(this.source.error$.pipe(tap((error) => onError(this.analyticsService, error))));
235
+ }
236
+ if (triggerObs.length) {
237
+ return merge(triggerObs);
238
+ }
239
+ else {
240
+ return of();
241
+ }
242
+ })).subscribe();
243
+ }
244
+ ngOnDestroy() {
245
+ this.source.lockSet.onNextUnlock(() => {
246
+ super.ngOnDestroy();
247
+ this._config.complete();
248
+ });
249
+ }
250
+ }
251
+ DbNgxActionAnalyticsDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxActionAnalyticsDirective, deps: [{ token: i1.ActionContextStoreSourceInstance, host: true }, { token: DbNgxAnalyticsService }], target: i0.ɵɵFactoryTarget.Directive });
252
+ DbNgxActionAnalyticsDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.2.0", type: DbNgxActionAnalyticsDirective, selector: "[dbxActionAnalytics]", inputs: { config: ["dbxActionAnalytics", "config"] }, usesInheritance: true, ngImport: i0 });
253
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxActionAnalyticsDirective, decorators: [{
254
+ type: Directive,
255
+ args: [{
256
+ selector: '[dbxActionAnalytics]',
257
+ }]
258
+ }], ctorParameters: function () {
259
+ return [{ type: i1.ActionContextStoreSourceInstance, decorators: [{
260
+ type: Host
261
+ }] }, { type: DbNgxAnalyticsService }];
262
+ }, propDecorators: { config: [{
263
+ type: Input,
264
+ args: ['dbxActionAnalytics']
265
+ }] } });
266
+
267
+ class DbNgxAnalyticsActionModule {
268
+ }
269
+ DbNgxAnalyticsActionModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
270
+ DbNgxAnalyticsActionModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule, declarations: [DbNgxActionAnalyticsDirective], exports: [DbNgxActionAnalyticsDirective] });
271
+ DbNgxAnalyticsActionModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule });
272
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule, decorators: [{
273
+ type: NgModule,
274
+ args: [{
275
+ declarations: [
276
+ DbNgxActionAnalyticsDirective
277
+ ],
278
+ exports: [
279
+ DbNgxActionAnalyticsDirective
280
+ ]
281
+ }]
282
+ }] });
283
+
284
+ class DbNgxAnalyticsModule {
285
+ static forRoot(options) {
286
+ return {
287
+ ngModule: DbNgxAnalyticsModule,
288
+ providers: [
289
+ // Configuration
290
+ options.analyticsConfigurationProvider,
291
+ // Service
292
+ DbNgxAnalyticsService
293
+ ]
294
+ };
295
+ }
296
+ }
297
+ DbNgxAnalyticsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
298
+ DbNgxAnalyticsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule });
299
+ DbNgxAnalyticsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule });
300
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule, decorators: [{
301
+ type: NgModule
302
+ }] });
303
+
304
+ /**
305
+ * Generated bundle index. Do not edit.
306
+ */
307
+
308
+ export { AbstractAnalyticsServiceListener, AnalyticsStreamEventAnalyticsEventWrapper, AnalyticsStreamEventType, DbNgxActionAnalyticsDirective, DbNgxActionAnalyticsTriggerType, DbNgxAnalyticsActionModule, DbNgxAnalyticsEventEmitterService, DbNgxAnalyticsEventStreamService, DbNgxAnalyticsModule, DbNgxAnalyticsService, DbNgxAnalyticsServiceConfiguration, DbNgxAnalyticsServiceListener, DbNgxAnalyticsUserSource };
309
+ //# sourceMappingURL=dereekb-dbx-analytics.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dereekb-dbx-analytics.mjs","sources":["../../../../packages/dbx-analytics/src/lib/analytics.stream.ts","../../../../packages/dbx-analytics/src/lib/analytics.service.ts","../../../../packages/dbx-analytics/src/lib/action/analytics.action.directive.ts","../../../../packages/dbx-analytics/src/lib/action/analytics.action.module.ts","../../../../packages/dbx-analytics/src/lib/analytics.module.ts","../../../../packages/dbx-analytics/src/dereekb-dbx-analytics.ts"],"sourcesContent":["import { Maybe } from \"@dereekb/util\";\nimport { AnalyticsUser, UserAnalyticsEvent, AnalyticsUserId } from \"./analytics\";\n\nexport enum AnalyticsStreamEventType {\n\n PageView,\n UserChange,\n\n // User Events\n NewUserEvent,\n UserLoginEvent,\n UserLogoutEvent,\n UserPropertiesEvent,\n\n // Events\n Event\n\n}\n\nexport interface AnalyticsStreamEvent {\n readonly type: AnalyticsStreamEventType;\n readonly user?: Maybe<AnalyticsUser>;\n readonly event?: UserAnalyticsEvent;\n readonly userId?: AnalyticsUserId;\n}\n","import { Observable, Subject, BehaviorSubject, of } from 'rxjs';\nimport { filter, first, shareReplay, switchMap } from 'rxjs/operators';\nimport { Inject, Injectable, Optional } from '@angular/core';\nimport { SubscriptionObject } from '@dereekb/rxjs';\nimport { AnalyticsEvent, AnalyticsEventData, AnalyticsEventName, AnalyticsUser, NewUserAnalyticsEventData, UserAnalyticsEvent } from './analytics';\nimport { AnalyticsStreamEvent, AnalyticsStreamEventType } from './analytics.stream';\nimport { Maybe, Destroyable } from '@dereekb/util';\n\nexport abstract class DbNgxAnalyticsEventEmitterService {\n abstract sendNewUserEvent(user: AnalyticsUser, data: NewUserAnalyticsEventData): void;\n abstract sendUserLoginEvent(user: AnalyticsUser, data?: AnalyticsEventData): void;\n abstract sendUserLogoutEvent(data?: AnalyticsEventData): void;\n abstract sendUserPropertiesEvent(user: AnalyticsUser, data?: AnalyticsEventData): void;\n abstract sendEventData(name: AnalyticsEventName, data?: AnalyticsEventData): void;\n abstract sendEvent(event: AnalyticsEvent): void;\n abstract sendPageView(page?: string): void;\n}\n\nexport abstract class DbNgxAnalyticsEventStreamService {\n abstract readonly events$: Observable<AnalyticsStreamEvent>;\n}\n\nexport abstract class DbNgxAnalyticsUserSource {\n abstract readonly analyticsUser$: Observable<Maybe<AnalyticsUser>>;\n}\n\nexport abstract class DbNgxAnalyticsServiceListener {\n public abstract listenToService(service: DbNgxAnalyticsService): void;\n}\n\nexport abstract class AbstractAnalyticsServiceListener implements DbNgxAnalyticsServiceListener {\n\n protected _service?: DbNgxAnalyticsService;\n protected _sub = new SubscriptionObject();\n\n public listenToService(service: DbNgxAnalyticsService): void {\n this._service = service;\n this._sub.subscription = service.events$.pipe(filter((e) => this.filterEvent(e)))\n .subscribe((event) => this.updateOnStreamEvent(event));\n }\n\n protected filterEvent(streamEvent: AnalyticsStreamEvent): boolean {\n return true;\n }\n\n protected abstract updateOnStreamEvent(event: AnalyticsStreamEvent): void;\n\n}\n\nexport abstract class DbNgxAnalyticsServiceConfiguration {\n listeners: DbNgxAnalyticsServiceListener[] = [];\n isProduction?: boolean;\n logEvents?: boolean;\n userSource?: DbNgxAnalyticsUserSource;\n}\n\nexport class AnalyticsStreamEventAnalyticsEventWrapper implements AnalyticsStreamEvent {\n\n constructor(public readonly event: UserAnalyticsEvent, public readonly type: AnalyticsStreamEventType = AnalyticsStreamEventType.Event) { }\n\n public get user(): Maybe<AnalyticsUser> {\n return this.event.user;\n }\n\n public get userId(): string | undefined {\n return (this.user) ? this.user.user : undefined;\n }\n\n}\n\n/**\n * Primary analytics service that emits analytics events that components can listen to.\n */\n@Injectable()\nexport class DbNgxAnalyticsService implements DbNgxAnalyticsEventStreamService, DbNgxAnalyticsEventEmitterService, Destroyable {\n\n // TODO: Make these configurable.\n\n static readonly USER_REGISTRATION_EVENT_NAME = 'User Registered';\n static readonly USER_LOGIN_EVENT_NAME = 'User Login';\n static readonly USER_LOGOUT_EVENT_NAME = 'User Logout';\n static readonly USER_PROPERTIES_EVENT_NAME = 'User Properties';\n\n private _subject = new Subject<AnalyticsStreamEvent>();\n readonly events$ = this._subject.asObservable();\n\n private _userSource = new BehaviorSubject<Maybe<DbNgxAnalyticsUserSource>>(undefined);\n readonly user$ = this._userSource.pipe(switchMap(x => (x) ? x.analyticsUser$ : of(undefined)), shareReplay(1));\n\n private _userSourceSub = new SubscriptionObject();\n private _loggerSub = new SubscriptionObject();\n\n constructor(\n private _config: DbNgxAnalyticsServiceConfiguration,\n @Optional() @Inject(DbNgxAnalyticsUserSource) userSource: Maybe<DbNgxAnalyticsUserSource> = _config.userSource\n ) {\n this._init();\n\n if (userSource) {\n this.setUserSource(userSource);\n }\n }\n\n // MARK: Source\n /**\n * Sets the user directly.\n */\n public setUser(user: Maybe<AnalyticsUser>): void {\n let source: Maybe<DbNgxAnalyticsUserSource>;\n\n if (user) {\n source = { analyticsUser$: of(user) };\n }\n\n this._userSource.next(source);\n }\n\n public setUserSource(source: DbNgxAnalyticsUserSource): void {\n this._userSource.next(source);\n }\n\n // MARK: DbNgxAnalyticsEventEmitterService\n /**\n * Sends an event.\n */\n public sendNewUserEvent(user: AnalyticsUser, data: NewUserAnalyticsEventData): void {\n this.sendNextEvent({\n name: DbNgxAnalyticsService.USER_REGISTRATION_EVENT_NAME,\n data\n }, AnalyticsStreamEventType.NewUserEvent, user);\n }\n\n public sendUserLoginEvent(user: AnalyticsUser, data?: AnalyticsEventData): void {\n this.sendNextEvent({\n name: DbNgxAnalyticsService.USER_LOGIN_EVENT_NAME,\n data\n }, AnalyticsStreamEventType.UserLoginEvent, user);\n }\n\n public sendUserLogoutEvent(data?: AnalyticsEventData, clearUser = true): void {\n this.sendNextEvent({\n name: DbNgxAnalyticsService.USER_LOGOUT_EVENT_NAME,\n data\n }, AnalyticsStreamEventType.UserLogoutEvent);\n\n if (clearUser) {\n this.setUser(undefined);\n }\n }\n\n public sendUserPropertiesEvent(user: AnalyticsUser, data?: AnalyticsEventData): void {\n this.sendNextEvent({\n name: DbNgxAnalyticsService.USER_PROPERTIES_EVENT_NAME,\n data\n }, AnalyticsStreamEventType.UserPropertiesEvent, user);\n }\n\n public sendEventData(name: AnalyticsEventName, data?: AnalyticsEventData): void {\n return this.sendEvent({\n name,\n data\n });\n }\n\n public sendEventType(eventType: AnalyticsEventName): void {\n this.sendNextEvent({\n name: eventType\n }, AnalyticsStreamEventType.Event);\n }\n\n public sendEvent(event: AnalyticsEvent): void {\n this.sendNextEvent(event, AnalyticsStreamEventType.Event);\n }\n\n public sendPageView(page?: string): void {\n this.sendNextEvent({\n name: page\n }, AnalyticsStreamEventType.PageView);\n }\n\n protected sendNextEvent(event: AnalyticsEvent = {}, type: AnalyticsStreamEventType, userOverride?: AnalyticsUser): void {\n this.user$.pipe(first()).subscribe((analyticsUser) => {\n const user: Maybe<AnalyticsUser> = (userOverride != null) ? userOverride : analyticsUser;\n const analyticsEvent: UserAnalyticsEvent = { ...event, user };\n this.nextEvent(analyticsEvent, type);\n });\n }\n\n protected nextEvent(event: UserAnalyticsEvent, type: AnalyticsStreamEventType): void {\n const wrapper = new AnalyticsStreamEventAnalyticsEventWrapper(event, type);\n this._subject.next(wrapper);\n }\n\n // MARK: Internal\n private _init(): void {\n\n if (this._config.isProduction) {\n // Initialize listeners.\n this._config.listeners.forEach((listener) => {\n listener.listenToService(this);\n });\n } else {\n console.warn('DbNgxAnalyticsService: Analytics not in production mode. All analytics events are ignored.');\n }\n\n if (this._config.logEvents || !this._config.isProduction) {\n console.log('DbNgxAnalyticsService: Log analytics events enabled.');\n\n // Create a new subscription\n this._loggerSub.subscription = this._subject.subscribe((x) => {\n console.log(`DbNgxAnalyticsService: Analytics Event - ${AnalyticsStreamEventType[x.type]} User: ${x.userId} Data: ${JSON.stringify(x.event)}.`);\n });\n }\n\n this._userSourceSub.subscription = this.user$.subscribe(() => {\n this.sendNextEvent({}, AnalyticsStreamEventType.UserChange);\n });\n }\n\n destroy() {\n this._userSourceSub.destroy();\n this._loggerSub.destroy();\n }\n\n}\n","import { filterMaybe } from '@dereekb/rxjs';\nimport { switchMap, tap, shareReplay } from 'rxjs/operators';\nimport { Host, Directive, Input, OnInit, OnDestroy } from '@angular/core';\nimport { BehaviorSubject, merge, Observable, of } from 'rxjs';\nimport { ActionContextStoreSourceInstance, AbstractSubscriptionDirective } from '@dereekb/dbx-core';\nimport { DbNgxAnalyticsService } from '../analytics.service';\nimport { Maybe, ReadableError } from '@dereekb/util';\n\nexport enum DbNgxActionAnalyticsTriggerType {\n TRIGGER,\n READY,\n SUCCESS,\n ERROR\n}\n\nexport interface DbNgxActionAnalyticsConfig<T = any, O = any> {\n onTriggered: (service: DbNgxAnalyticsService) => void;\n onReady: (service: DbNgxAnalyticsService, value: T) => void;\n onSuccess: (service: DbNgxAnalyticsService, value: Maybe<O>) => void;\n onError: (service: DbNgxAnalyticsService, error: Maybe<ReadableError>) => void;\n}\n\n/**\n * Used to listen to an ActionContext and send analytical events based on action events.\n */\n@Directive({\n selector: '[dbxActionAnalytics]',\n})\nexport class DbNgxActionAnalyticsDirective<T> extends AbstractSubscriptionDirective implements OnInit, OnDestroy {\n\n private _config = new BehaviorSubject<Maybe<DbNgxActionAnalyticsConfig>>(undefined);\n readonly config$ = this._config.pipe(filterMaybe(), shareReplay(1));\n\n @Input('dbxActionAnalytics')\n get config(): Maybe<DbNgxActionAnalyticsConfig> {\n return this._config.value;\n }\n\n set config(config: Maybe<DbNgxActionAnalyticsConfig>) {\n this._config.next(config);\n }\n\n constructor(\n @Host() readonly source: ActionContextStoreSourceInstance,\n readonly analyticsService: DbNgxAnalyticsService\n ) {\n super();\n }\n\n ngOnInit(): void {\n this.sub = this.config$.pipe(\n switchMap(({ onTriggered, onReady, onSuccess, onError }) => {\n const triggerObs: Observable<any>[] = [];\n\n if (onTriggered) {\n triggerObs.push(this.source.triggered$.pipe(\n tap(() => onTriggered(this.analyticsService))\n ));\n }\n\n if (onReady) {\n triggerObs.push(this.source.valueReady$.pipe(\n tap((value) => onReady(this.analyticsService, value))\n ));\n }\n\n if (onSuccess) {\n triggerObs.push(this.source.success$.pipe(\n tap((result) => onSuccess(this.analyticsService, result))\n ));\n }\n\n if (onError) {\n triggerObs.push(this.source.error$.pipe(\n tap((error) => onError(this.analyticsService, error))\n ));\n }\n\n if (triggerObs.length) {\n return merge(triggerObs);\n } else {\n return of();\n }\n })\n ).subscribe();\n }\n\n override ngOnDestroy(): void {\n this.source.lockSet.onNextUnlock(() => {\n super.ngOnDestroy();\n this._config.complete();\n });\n }\n\n}\n","import { NgModule } from '@angular/core';\nimport { DbNgxActionAnalyticsDirective } from './analytics.action.directive';\n\n@NgModule({\n declarations: [\n DbNgxActionAnalyticsDirective\n ],\n exports: [\n DbNgxActionAnalyticsDirective\n ]\n})\nexport class DbNgxAnalyticsActionModule { }\n","import { NgModule, ModuleWithProviders, Provider } from '@angular/core';\nimport { DbNgxAnalyticsService } from './analytics.service';\n\n\nexport interface DbNgxAnalyticsModuleOptions {\n\n /**\n * Provides a AnalyticsServiceConfiguration value.\n */\n analyticsConfigurationProvider: Provider;\n\n}\n\n@NgModule()\nexport class DbNgxAnalyticsModule {\n\n static forRoot(options: DbNgxAnalyticsModuleOptions): ModuleWithProviders<DbNgxAnalyticsModule> {\n return {\n ngModule: DbNgxAnalyticsModule,\n providers: [\n // Configuration\n options.analyticsConfigurationProvider,\n // Service\n DbNgxAnalyticsService\n ]\n };\n }\n\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;;IAGY;AAAZ,WAAY,wBAAwB;IAElC,+EAAQ,CAAA;IACR,mFAAU,CAAA;;IAGV,uFAAY,CAAA;IACZ,2FAAc,CAAA;IACd,6FAAe,CAAA;IACf,qGAAmB,CAAA;;IAGnB,yEAAK,CAAA;AAEP,CAAC,EAdW,wBAAwB,KAAxB,wBAAwB;;MCKd,iCAAiC;CAQtD;MAEqB,gCAAgC;CAErD;MAEqB,wBAAwB;CAE7C;MAEqB,6BAA6B;CAElD;MAEqB,gCAAgC;IAAtD;QAGY,SAAI,GAAG,IAAI,kBAAkB,EAAE,CAAC;KAc3C;IAZQ,eAAe,CAAC,OAA8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L,QAAQ,EAAE,oBAAoB;YAC9B,SAAS,EAAE;;gBAET,OAAO,CAAC,8BAA8B;;gBAEtC,qBAAqB;aACtB;SACF,CAAC;KACH;;iHAZU,oBAAoB;kHAApB,oBAAoB;kHAApB,oBAAoB;2FAApB,oBAAoB;kBADhC,QAAQ;;;ACbT;;;;;;"}
@@ -0,0 +1,305 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, Optional, Inject, Directive, Host, Input, NgModule } from '@angular/core';
3
+ import { SubscriptionObject, filterMaybe } from '@dereekb/rxjs';
4
+ import { filter, switchMap, shareReplay, first, tap } from 'rxjs/operators';
5
+ import { Subject, BehaviorSubject, of, merge } from 'rxjs';
6
+ import * as i1 from '@dereekb/dbx-core';
7
+ import { AbstractSubscriptionDirective } from '@dereekb/dbx-core';
8
+
9
+ var AnalyticsStreamEventType;
10
+ (function (AnalyticsStreamEventType) {
11
+ AnalyticsStreamEventType[AnalyticsStreamEventType["PageView"] = 0] = "PageView";
12
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserChange"] = 1] = "UserChange";
13
+ // User Events
14
+ AnalyticsStreamEventType[AnalyticsStreamEventType["NewUserEvent"] = 2] = "NewUserEvent";
15
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserLoginEvent"] = 3] = "UserLoginEvent";
16
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserLogoutEvent"] = 4] = "UserLogoutEvent";
17
+ AnalyticsStreamEventType[AnalyticsStreamEventType["UserPropertiesEvent"] = 5] = "UserPropertiesEvent";
18
+ // Events
19
+ AnalyticsStreamEventType[AnalyticsStreamEventType["Event"] = 6] = "Event";
20
+ })(AnalyticsStreamEventType || (AnalyticsStreamEventType = {}));
21
+
22
+ class DbNgxAnalyticsEventEmitterService {
23
+ }
24
+ class DbNgxAnalyticsEventStreamService {
25
+ }
26
+ class DbNgxAnalyticsUserSource {
27
+ }
28
+ class DbNgxAnalyticsServiceListener {
29
+ }
30
+ class AbstractAnalyticsServiceListener {
31
+ constructor() {
32
+ this._sub = new SubscriptionObject();
33
+ }
34
+ listenToService(service) {
35
+ this._service = service;
36
+ this._sub.subscription = service.events$.pipe(filter((e) => this.filterEvent(e)))
37
+ .subscribe((event) => this.updateOnStreamEvent(event));
38
+ }
39
+ filterEvent(streamEvent) {
40
+ return true;
41
+ }
42
+ }
43
+ class DbNgxAnalyticsServiceConfiguration {
44
+ constructor() {
45
+ this.listeners = [];
46
+ }
47
+ }
48
+ class AnalyticsStreamEventAnalyticsEventWrapper {
49
+ constructor(event, type = AnalyticsStreamEventType.Event) {
50
+ this.event = event;
51
+ this.type = type;
52
+ }
53
+ get user() {
54
+ return this.event.user;
55
+ }
56
+ get userId() {
57
+ return (this.user) ? this.user.user : undefined;
58
+ }
59
+ }
60
+ /**
61
+ * Primary analytics service that emits analytics events that components can listen to.
62
+ */
63
+ class DbNgxAnalyticsService {
64
+ constructor(_config, userSource = _config.userSource) {
65
+ this._config = _config;
66
+ this._subject = new Subject();
67
+ this.events$ = this._subject.asObservable();
68
+ this._userSource = new BehaviorSubject(undefined);
69
+ this.user$ = this._userSource.pipe(switchMap(x => (x) ? x.analyticsUser$ : of(undefined)), shareReplay(1));
70
+ this._userSourceSub = new SubscriptionObject();
71
+ this._loggerSub = new SubscriptionObject();
72
+ this._init();
73
+ if (userSource) {
74
+ this.setUserSource(userSource);
75
+ }
76
+ }
77
+ // MARK: Source
78
+ /**
79
+ * Sets the user directly.
80
+ */
81
+ setUser(user) {
82
+ let source;
83
+ if (user) {
84
+ source = { analyticsUser$: of(user) };
85
+ }
86
+ this._userSource.next(source);
87
+ }
88
+ setUserSource(source) {
89
+ this._userSource.next(source);
90
+ }
91
+ // MARK: DbNgxAnalyticsEventEmitterService
92
+ /**
93
+ * Sends an event.
94
+ */
95
+ sendNewUserEvent(user, data) {
96
+ this.sendNextEvent({
97
+ name: DbNgxAnalyticsService.USER_REGISTRATION_EVENT_NAME,
98
+ data
99
+ }, AnalyticsStreamEventType.NewUserEvent, user);
100
+ }
101
+ sendUserLoginEvent(user, data) {
102
+ this.sendNextEvent({
103
+ name: DbNgxAnalyticsService.USER_LOGIN_EVENT_NAME,
104
+ data
105
+ }, AnalyticsStreamEventType.UserLoginEvent, user);
106
+ }
107
+ sendUserLogoutEvent(data, clearUser = true) {
108
+ this.sendNextEvent({
109
+ name: DbNgxAnalyticsService.USER_LOGOUT_EVENT_NAME,
110
+ data
111
+ }, AnalyticsStreamEventType.UserLogoutEvent);
112
+ if (clearUser) {
113
+ this.setUser(undefined);
114
+ }
115
+ }
116
+ sendUserPropertiesEvent(user, data) {
117
+ this.sendNextEvent({
118
+ name: DbNgxAnalyticsService.USER_PROPERTIES_EVENT_NAME,
119
+ data
120
+ }, AnalyticsStreamEventType.UserPropertiesEvent, user);
121
+ }
122
+ sendEventData(name, data) {
123
+ return this.sendEvent({
124
+ name,
125
+ data
126
+ });
127
+ }
128
+ sendEventType(eventType) {
129
+ this.sendNextEvent({
130
+ name: eventType
131
+ }, AnalyticsStreamEventType.Event);
132
+ }
133
+ sendEvent(event) {
134
+ this.sendNextEvent(event, AnalyticsStreamEventType.Event);
135
+ }
136
+ sendPageView(page) {
137
+ this.sendNextEvent({
138
+ name: page
139
+ }, AnalyticsStreamEventType.PageView);
140
+ }
141
+ sendNextEvent(event = {}, type, userOverride) {
142
+ this.user$.pipe(first()).subscribe((analyticsUser) => {
143
+ const user = (userOverride != null) ? userOverride : analyticsUser;
144
+ const analyticsEvent = { ...event, user };
145
+ this.nextEvent(analyticsEvent, type);
146
+ });
147
+ }
148
+ nextEvent(event, type) {
149
+ const wrapper = new AnalyticsStreamEventAnalyticsEventWrapper(event, type);
150
+ this._subject.next(wrapper);
151
+ }
152
+ // MARK: Internal
153
+ _init() {
154
+ if (this._config.isProduction) {
155
+ // Initialize listeners.
156
+ this._config.listeners.forEach((listener) => {
157
+ listener.listenToService(this);
158
+ });
159
+ }
160
+ else {
161
+ console.warn('DbNgxAnalyticsService: Analytics not in production mode. All analytics events are ignored.');
162
+ }
163
+ if (this._config.logEvents || !this._config.isProduction) {
164
+ console.log('DbNgxAnalyticsService: Log analytics events enabled.');
165
+ // Create a new subscription
166
+ this._loggerSub.subscription = this._subject.subscribe((x) => {
167
+ console.log(`DbNgxAnalyticsService: Analytics Event - ${AnalyticsStreamEventType[x.type]} User: ${x.userId} Data: ${JSON.stringify(x.event)}.`);
168
+ });
169
+ }
170
+ this._userSourceSub.subscription = this.user$.subscribe(() => {
171
+ this.sendNextEvent({}, AnalyticsStreamEventType.UserChange);
172
+ });
173
+ }
174
+ destroy() {
175
+ this._userSourceSub.destroy();
176
+ this._loggerSub.destroy();
177
+ }
178
+ }
179
+ // TODO: Make these configurable.
180
+ DbNgxAnalyticsService.USER_REGISTRATION_EVENT_NAME = 'User Registered';
181
+ DbNgxAnalyticsService.USER_LOGIN_EVENT_NAME = 'User Login';
182
+ DbNgxAnalyticsService.USER_LOGOUT_EVENT_NAME = 'User Logout';
183
+ DbNgxAnalyticsService.USER_PROPERTIES_EVENT_NAME = 'User Properties';
184
+ DbNgxAnalyticsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsService, deps: [{ token: DbNgxAnalyticsServiceConfiguration }, { token: DbNgxAnalyticsUserSource, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
185
+ DbNgxAnalyticsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsService });
186
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsService, decorators: [{
187
+ type: Injectable
188
+ }], ctorParameters: function () { return [{ type: DbNgxAnalyticsServiceConfiguration }, { type: undefined, decorators: [{
189
+ type: Optional
190
+ }, {
191
+ type: Inject,
192
+ args: [DbNgxAnalyticsUserSource]
193
+ }] }]; } });
194
+
195
+ var DbNgxActionAnalyticsTriggerType;
196
+ (function (DbNgxActionAnalyticsTriggerType) {
197
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["TRIGGER"] = 0] = "TRIGGER";
198
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["READY"] = 1] = "READY";
199
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["SUCCESS"] = 2] = "SUCCESS";
200
+ DbNgxActionAnalyticsTriggerType[DbNgxActionAnalyticsTriggerType["ERROR"] = 3] = "ERROR";
201
+ })(DbNgxActionAnalyticsTriggerType || (DbNgxActionAnalyticsTriggerType = {}));
202
+ /**
203
+ * Used to listen to an ActionContext and send analytical events based on action events.
204
+ */
205
+ class DbNgxActionAnalyticsDirective extends AbstractSubscriptionDirective {
206
+ constructor(source, analyticsService) {
207
+ super();
208
+ this.source = source;
209
+ this.analyticsService = analyticsService;
210
+ this._config = new BehaviorSubject(undefined);
211
+ this.config$ = this._config.pipe(filterMaybe(), shareReplay(1));
212
+ }
213
+ get config() {
214
+ return this._config.value;
215
+ }
216
+ set config(config) {
217
+ this._config.next(config);
218
+ }
219
+ ngOnInit() {
220
+ this.sub = this.config$.pipe(switchMap(({ onTriggered, onReady, onSuccess, onError }) => {
221
+ const triggerObs = [];
222
+ if (onTriggered) {
223
+ triggerObs.push(this.source.triggered$.pipe(tap(() => onTriggered(this.analyticsService))));
224
+ }
225
+ if (onReady) {
226
+ triggerObs.push(this.source.valueReady$.pipe(tap((value) => onReady(this.analyticsService, value))));
227
+ }
228
+ if (onSuccess) {
229
+ triggerObs.push(this.source.success$.pipe(tap((result) => onSuccess(this.analyticsService, result))));
230
+ }
231
+ if (onError) {
232
+ triggerObs.push(this.source.error$.pipe(tap((error) => onError(this.analyticsService, error))));
233
+ }
234
+ if (triggerObs.length) {
235
+ return merge(triggerObs);
236
+ }
237
+ else {
238
+ return of();
239
+ }
240
+ })).subscribe();
241
+ }
242
+ ngOnDestroy() {
243
+ this.source.lockSet.onNextUnlock(() => {
244
+ super.ngOnDestroy();
245
+ this._config.complete();
246
+ });
247
+ }
248
+ }
249
+ DbNgxActionAnalyticsDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxActionAnalyticsDirective, deps: [{ token: i1.ActionContextStoreSourceInstance, host: true }, { token: DbNgxAnalyticsService }], target: i0.ɵɵFactoryTarget.Directive });
250
+ DbNgxActionAnalyticsDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.2.0", type: DbNgxActionAnalyticsDirective, selector: "[dbxActionAnalytics]", inputs: { config: ["dbxActionAnalytics", "config"] }, usesInheritance: true, ngImport: i0 });
251
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxActionAnalyticsDirective, decorators: [{
252
+ type: Directive,
253
+ args: [{
254
+ selector: '[dbxActionAnalytics]',
255
+ }]
256
+ }], ctorParameters: function () { return [{ type: i1.ActionContextStoreSourceInstance, decorators: [{
257
+ type: Host
258
+ }] }, { type: DbNgxAnalyticsService }]; }, propDecorators: { config: [{
259
+ type: Input,
260
+ args: ['dbxActionAnalytics']
261
+ }] } });
262
+
263
+ class DbNgxAnalyticsActionModule {
264
+ }
265
+ DbNgxAnalyticsActionModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
266
+ DbNgxAnalyticsActionModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule, declarations: [DbNgxActionAnalyticsDirective], exports: [DbNgxActionAnalyticsDirective] });
267
+ DbNgxAnalyticsActionModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule });
268
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsActionModule, decorators: [{
269
+ type: NgModule,
270
+ args: [{
271
+ declarations: [
272
+ DbNgxActionAnalyticsDirective
273
+ ],
274
+ exports: [
275
+ DbNgxActionAnalyticsDirective
276
+ ]
277
+ }]
278
+ }] });
279
+
280
+ class DbNgxAnalyticsModule {
281
+ static forRoot(options) {
282
+ return {
283
+ ngModule: DbNgxAnalyticsModule,
284
+ providers: [
285
+ // Configuration
286
+ options.analyticsConfigurationProvider,
287
+ // Service
288
+ DbNgxAnalyticsService
289
+ ]
290
+ };
291
+ }
292
+ }
293
+ DbNgxAnalyticsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
294
+ DbNgxAnalyticsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule });
295
+ DbNgxAnalyticsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule });
296
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DbNgxAnalyticsModule, decorators: [{
297
+ type: NgModule
298
+ }] });
299
+
300
+ /**
301
+ * Generated bundle index. Do not edit.
302
+ */
303
+
304
+ export { AbstractAnalyticsServiceListener, AnalyticsStreamEventAnalyticsEventWrapper, AnalyticsStreamEventType, DbNgxActionAnalyticsDirective, DbNgxActionAnalyticsTriggerType, DbNgxAnalyticsActionModule, DbNgxAnalyticsEventEmitterService, DbNgxAnalyticsEventStreamService, DbNgxAnalyticsModule, DbNgxAnalyticsService, DbNgxAnalyticsServiceConfiguration, DbNgxAnalyticsServiceListener, DbNgxAnalyticsUserSource };
305
+ //# sourceMappingURL=dereekb-dbx-analytics.mjs.map