@nu-art/analytics-backend 0.400.8 → 0.400.14

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.
@@ -14,6 +14,7 @@ declare class ModuleBE_Analytics_Class extends Module<Config> {
14
14
  private initPlugins;
15
15
  private api_sendEvent;
16
16
  private api_updateUser;
17
+ private api_updateLexicon;
17
18
  }
18
19
  export declare const ModuleBE_Analytics: ModuleBE_Analytics_Class;
19
20
  export {};
@@ -9,6 +9,7 @@ class ModuleBE_Analytics_Class extends Module {
9
9
  addRoutes([
10
10
  createBodyServerApi(ApiDef_Analytics()._v1.sendEvent, this.api_sendEvent),
11
11
  createBodyServerApi(ApiDef_Analytics()._v1.updateUser, this.api_updateUser),
12
+ createBodyServerApi(ApiDef_Analytics()._v1.updateLexicon, this.api_updateLexicon),
12
13
  ]);
13
14
  }
14
15
  //######################### Plugin Management #########################
@@ -36,5 +37,10 @@ class ModuleBE_Analytics_Class extends Module {
36
37
  plugin.updateUser?.(request);
37
38
  });
38
39
  };
40
+ api_updateLexicon = async (request) => {
41
+ return this.plugins.forEach(plugin => {
42
+ plugin.updateLexicon?.(request);
43
+ });
44
+ };
39
45
  }
40
46
  export const ModuleBE_Analytics = new ModuleBE_Analytics_Class();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/analytics-backend",
3
- "version": "0.400.8",
3
+ "version": "0.400.14",
4
4
  "description": "analytics Backend",
5
5
  "license": "Apache-2.0",
6
6
  "scripts": {
@@ -11,12 +11,12 @@
11
11
  "linkDirectory": true
12
12
  },
13
13
  "dependencies": {
14
- "@nu-art/analytics-shared": "0.400.8",
15
- "@nu-art/ts-common": "0.400.8",
16
- "@nu-art/firebase-backend": "0.400.8",
17
- "@nu-art/firebase-shared": "0.400.8",
18
- "@nu-art/thunderstorm-backend": "0.400.8",
19
- "@nu-art/thunderstorm-shared": "0.400.8",
14
+ "@nu-art/analytics-shared": "0.400.14",
15
+ "@nu-art/ts-common": "0.400.14",
16
+ "@nu-art/firebase-backend": "0.400.14",
17
+ "@nu-art/firebase-shared": "0.400.14",
18
+ "@nu-art/thunderstorm-backend": "0.400.14",
19
+ "@nu-art/thunderstorm-shared": "0.400.14",
20
20
  "mixpanel": "~0.18.1"
21
21
  },
22
22
  "devDependencies": {},
@@ -1,4 +1,4 @@
1
- import { Analytics_UpdateUser, TSAnalyticsEvent } from '@nu-art/analytics-shared';
1
+ import { Analytics_UpdateLexicon, Analytics_UpdateUser, TSAnalyticsEvent } from '@nu-art/analytics-shared';
2
2
  import { Logger } from '@nu-art/ts-common';
3
3
  import { AnalyticsPluginBaseConfig } from './types.js';
4
4
  /**
@@ -34,8 +34,10 @@ export declare abstract class AnalyticsPlugin_Base<R extends any = any, C extend
34
34
  protected abstract translateEvent(event: TSAnalyticsEvent): R;
35
35
  protected abstract sendEvents: (events: R[]) => Promise<void>;
36
36
  protected abstract updateUser_Impl: undefined | ((mode: Analytics_UpdateUser['request']['mode'], data: Analytics_UpdateUser['request']['userData']) => Promise<void>);
37
+ protected abstract updateLexicon_Impl: undefined | ((mode: Analytics_UpdateLexicon['request']['mode'], data: Analytics_UpdateLexicon['request']['lexiconMap']) => Promise<void>);
37
38
  init(config: C): void;
38
39
  private emptyEventBuffer;
39
40
  registerEvent(event: TSAnalyticsEvent): void;
40
41
  updateUser(request: Analytics_UpdateUser['request']): void;
42
+ updateLexicon(request: Analytics_UpdateLexicon['request']): void;
41
43
  }
@@ -58,4 +58,9 @@ export class AnalyticsPlugin_Base extends Logger {
58
58
  return;
59
59
  this.updateUser_Impl?.(request.mode, request.userData);
60
60
  }
61
+ updateLexicon(request) {
62
+ if (!this.config?.active)
63
+ return;
64
+ this.updateLexicon_Impl?.(request.mode, request.lexiconMap);
65
+ }
61
66
  }
@@ -5,5 +5,6 @@ export declare class AnalyticsPlugin_Logger extends AnalyticsPlugin_Base {
5
5
  key: string;
6
6
  protected translateEvent(event: TSAnalyticsEvent): TSAnalyticsEvent;
7
7
  protected updateUser_Impl: undefined;
8
+ protected updateLexicon_Impl: undefined;
8
9
  protected sendEvents: (events: any[]) => Promise<void>;
9
10
  }
@@ -6,6 +6,7 @@ export class AnalyticsPlugin_Logger extends AnalyticsPlugin_Base {
6
6
  return event;
7
7
  }
8
8
  updateUser_Impl = undefined;
9
+ updateLexicon_Impl = undefined;
9
10
  sendEvents = async (events) => {
10
11
  this.logInfoBold('######## Analytics Event - Start ########');
11
12
  this.logInfo(events);
@@ -1,4 +1,4 @@
1
- import { Analytics_UpdateUser, TSAnalyticsEvent } from '@nu-art/analytics-shared';
1
+ import { Analytics_UpdateLexicon, Analytics_UpdateUser, TSAnalyticsEvent } from '@nu-art/analytics-shared';
2
2
  import { AnalyticsPlugin_Base } from './AnalyticsPlugin_Base.js';
3
3
  import mixpanelLib from 'mixpanel';
4
4
  import { TypedMap } from '@nu-art/ts-common';
@@ -23,8 +23,10 @@ export declare class AnalyticsPlugin_MixedPanels extends AnalyticsPlugin_Base<Mi
23
23
  private mixpanel;
24
24
  init(config: MPConfig): void;
25
25
  private prepareUserProps;
26
+ private prepareLexiconMap;
26
27
  protected translateEvent(event: TSAnalyticsEvent): MixedPanelsEvent;
27
28
  protected sendEvents: (events: MixedPanelsEvent[]) => Promise<void>;
28
29
  protected updateUser_Impl: (mode: Analytics_UpdateUser["request"]["mode"], data: Analytics_UpdateUser["request"]["userData"]) => Promise<void>;
30
+ protected updateLexicon_Impl: (mode: Analytics_UpdateLexicon["request"]["mode"], data: Analytics_UpdateLexicon["request"]["lexiconMap"]) => Promise<void>;
29
31
  }
30
32
  export {};
@@ -36,6 +36,15 @@ export class AnalyticsPlugin_MixedPanels extends AnalyticsPlugin_Base {
36
36
  });
37
37
  return props;
38
38
  };
39
+ prepareLexiconMap = (data) => {
40
+ return _keys(data).reduce((map, groupKey) => {
41
+ map[groupKey] = {
42
+ id: data[groupKey].id,
43
+ propDict: { $name: data[groupKey].label },
44
+ };
45
+ return map;
46
+ }, {});
47
+ };
39
48
  //######################### Implemented Logic #########################
40
49
  translateEvent(event) {
41
50
  return {
@@ -95,4 +104,60 @@ export class AnalyticsPlugin_MixedPanels extends AnalyticsPlugin_Base {
95
104
  }
96
105
  });
97
106
  };
107
+ updateLexicon_Impl = (mode, data) => {
108
+ if (!this.mixpanel)
109
+ throw new BadImplementationException(`Calling update lexicon before analytics plugin ${pluginKey_MixedPanels} finished initializing`);
110
+ return new Promise((resolve, reject) => {
111
+ const lexiconMap = this.prepareLexiconMap(data);
112
+ this.logDebug('Updating Lexicon', lexiconMap);
113
+ switch (mode) {
114
+ case 'set': {
115
+ const promises = _keys(lexiconMap)
116
+ .map(groupKey => new Promise((resolve, reject) => {
117
+ this.mixpanel?.groups.set(groupKey, lexiconMap[groupKey].id, lexiconMap[groupKey].propDict, (err) => {
118
+ if (err) {
119
+ this.logError(err);
120
+ reject(err);
121
+ }
122
+ else {
123
+ this.logDebug('Successfully updated lexicon');
124
+ resolve();
125
+ }
126
+ });
127
+ }));
128
+ Promise.all(promises)
129
+ .then(() => resolve())
130
+ .catch(err => {
131
+ this.logError('Failed batch updating lexicon', err);
132
+ reject();
133
+ });
134
+ break;
135
+ }
136
+ case 'set_once': {
137
+ const promises = _keys(lexiconMap)
138
+ .map(groupKey => new Promise((resolve, reject) => {
139
+ this.mixpanel?.groups.set_once(groupKey, lexiconMap[groupKey].id, lexiconMap[groupKey].propDict, (err) => {
140
+ if (err) {
141
+ this.logError(err);
142
+ reject(err);
143
+ }
144
+ else {
145
+ this.logDebug('Successfully updated lexicon');
146
+ resolve();
147
+ }
148
+ });
149
+ }));
150
+ Promise.all(promises)
151
+ .then(() => resolve())
152
+ .catch(err => {
153
+ this.logError('Failed batch updating lexicon', err);
154
+ reject();
155
+ });
156
+ break;
157
+ }
158
+ default:
159
+ throw new BadImplementationException(`No Implementation for mode ${mode}`);
160
+ }
161
+ });
162
+ };
98
163
  }