@formo/analytics 1.15.0 → 1.15.2

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.
Files changed (39) hide show
  1. package/dist/cjs/src/FormoAnalytics.d.ts.map +1 -1
  2. package/dist/cjs/src/FormoAnalytics.js +5 -9
  3. package/dist/cjs/src/FormoAnalytics.js.map +1 -1
  4. package/dist/cjs/src/constants/config.d.ts +1 -1
  5. package/dist/cjs/src/constants/config.d.ts.map +1 -1
  6. package/dist/cjs/src/constants/config.js +1 -1
  7. package/dist/cjs/src/constants/config.js.map +1 -1
  8. package/dist/cjs/src/lib/logger.d.ts +8 -1
  9. package/dist/cjs/src/lib/logger.d.ts.map +1 -1
  10. package/dist/cjs/src/lib/logger.js +15 -5
  11. package/dist/cjs/src/lib/logger.js.map +1 -1
  12. package/dist/cjs/src/lib/queue.d.ts +2 -2
  13. package/dist/cjs/src/lib/queue.d.ts.map +1 -1
  14. package/dist/cjs/src/lib/queue.js +13 -13
  15. package/dist/cjs/src/lib/queue.js.map +1 -1
  16. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  17. package/dist/esm/src/FormoAnalytics.d.ts.map +1 -1
  18. package/dist/esm/src/FormoAnalytics.js +6 -10
  19. package/dist/esm/src/FormoAnalytics.js.map +1 -1
  20. package/dist/esm/src/constants/config.d.ts +1 -1
  21. package/dist/esm/src/constants/config.d.ts.map +1 -1
  22. package/dist/esm/src/constants/config.js +1 -1
  23. package/dist/esm/src/constants/config.js.map +1 -1
  24. package/dist/esm/src/lib/logger.d.ts +8 -1
  25. package/dist/esm/src/lib/logger.d.ts.map +1 -1
  26. package/dist/esm/src/lib/logger.js +15 -5
  27. package/dist/esm/src/lib/logger.js.map +1 -1
  28. package/dist/esm/src/lib/queue.d.ts +2 -2
  29. package/dist/esm/src/lib/queue.d.ts.map +1 -1
  30. package/dist/esm/src/lib/queue.js +13 -13
  31. package/dist/esm/src/lib/queue.js.map +1 -1
  32. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  33. package/dist/index.umd.min.js +1 -1
  34. package/dist/index.umd.min.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/FormoAnalytics.ts +5 -10
  37. package/src/constants/config.ts +1 -1
  38. package/src/lib/logger.ts +28 -6
  39. package/src/lib/queue.ts +11 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formo/analytics",
3
- "version": "1.15.0",
3
+ "version": "1.15.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/getformo/sdk.git"
@@ -21,7 +21,7 @@ import {
21
21
  TransactionStatus,
22
22
  RequestEvent,
23
23
  } from "./types";
24
- import { session, local, logger, EventQueue, fetch } from "./lib";
24
+ import { session, local, logger, EventQueue, fetch, Logger } from "./lib";
25
25
  import {
26
26
  isLocalhost,
27
27
  isAddress,
@@ -95,13 +95,10 @@ export class FormoAnalytics implements IFormoAnalytics {
95
95
  this.session = new FormoAnalyticsSession();
96
96
 
97
97
  // Initialize logger with configuration from options
98
- const loggerConfig = options.logger || {
99
- enabled: false,
100
- };
101
- logger.setEnabled(loggerConfig.enabled);
102
- if (loggerConfig.levels) {
103
- logger.setEnabledLevels(loggerConfig.levels);
104
- }
98
+ Logger.init({
99
+ enabled: options.logger?.enabled || false,
100
+ enabledLevels: options.logger?.levels || [],
101
+ });
105
102
 
106
103
  this.eventQueue = new EventQueue(this.config.writeKey, {
107
104
  url: EVENTS_API_URL,
@@ -498,7 +495,6 @@ export class FormoAnalytics implements IFormoAnalytics {
498
495
 
499
496
  return;
500
497
  } catch (error) {
501
- logger.info("Transaction listener catch");
502
498
  logger.error("Transaction error:", error);
503
499
  const rpcError = error as RPCError;
504
500
  if (rpcError && rpcError?.code === 4001) {
@@ -769,7 +765,6 @@ export class FormoAnalytics implements IFormoAnalytics {
769
765
  try {
770
766
  const accounts = await this.getAccounts();
771
767
  if (accounts && accounts.length > 0) {
772
- // TODO: fetch userId if account is valid, generate userId if no userId matches address from tinybird
773
768
  if (isAddress(accounts[0])) {
774
769
  return accounts[0];
775
770
  }
@@ -1,6 +1,6 @@
1
1
  export const EVENTS_API_HOST = "https://events.formo.so";
2
2
  export const EVENTS_API_URL = `${EVENTS_API_HOST}/events`;
3
- export const USER_API_URL = `${EVENTS_API_HOST}/user-id`;
3
+ export const USER_API_URL = `${EVENTS_API_HOST}/user`;
4
4
 
5
5
  export const EVENTS_API_REQUEST_HEADER = (writeKey: string) => ({
6
6
  "Content-Type": "application/json",
package/src/lib/logger.ts CHANGED
@@ -5,17 +5,39 @@ export class Logger {
5
5
  private enabledLevels: Set<LogLevel>;
6
6
  private enabled: boolean;
7
7
 
8
- private constructor(enabled: boolean = true, enabledLevels: LogLevel[] = []) {
8
+ private constructor(
9
+ enabled: boolean = false,
10
+ enabledLevels: LogLevel[] = []
11
+ ) {
9
12
  this.enabled = enabled;
10
13
  this.enabledLevels = new Set(enabledLevels);
11
14
  }
12
15
 
13
- public static getInstance(
14
- enabled: boolean = false,
15
- enabledLevels: LogLevel[] = []
16
- ): Logger {
16
+ public static init(config: {
17
+ enabled?: boolean;
18
+ enabledLevels?: LogLevel[];
19
+ }): void {
20
+ // Get or create instance
21
+ const instance = Logger.getInstance();
22
+
23
+ // Update configuration
24
+ if (config.enabled !== undefined) {
25
+ instance.setEnabled(config.enabled);
26
+ }
27
+ if (config.enabledLevels !== undefined) {
28
+ instance.setEnabledLevels(config.enabledLevels);
29
+ }
30
+ }
31
+
32
+ public static getInstance(config?: {
33
+ enabled?: boolean;
34
+ enabledLevels?: LogLevel[];
35
+ }): Logger {
17
36
  if (!Logger.instance) {
18
- Logger.instance = new Logger(enabled, enabledLevels);
37
+ Logger.instance = new Logger(
38
+ config?.enabled ?? false,
39
+ config?.enabledLevels ?? []
40
+ );
19
41
  }
20
42
  return Logger.instance;
21
43
  }
package/src/lib/queue.ts CHANGED
@@ -96,16 +96,16 @@ export class EventQueue {
96
96
  }
97
97
 
98
98
  //#region Public functions
99
- async enqueue(message: RequestEvent, callback?: (...args: any) => void) {
99
+ async enqueue(event: RequestEvent, callback?: (...args: any) => void) {
100
100
  callback = callback || noop;
101
101
 
102
- const formattedTimestamp = toDateHourMinute(new Date(message.timestamp));
103
- message.timestamp = formattedTimestamp;
102
+ const formattedTimestamp = toDateHourMinute(new Date(event.timestamp));
103
+ event.timestamp = formattedTimestamp;
104
104
 
105
- const messageString = JSON.stringify(message);
106
- const hashToken = await hash(messageString);
105
+ const eventString = JSON.stringify(event);
106
+ const eventId = await hash(eventString);
107
107
  // check if the message already exists
108
- if (await this.isDuplicated(hashToken)) {
108
+ if (await this.isDuplicate(eventId)) {
109
109
  logger.warn(
110
110
  `Event already enqueued, try again after ${millisecondsToSecond(
111
111
  this.flushIntervalMs
@@ -114,10 +114,10 @@ export class EventQueue {
114
114
  return;
115
115
  }
116
116
 
117
- this.queue.push({ message: { ...message, id: hashToken }, callback });
117
+ this.queue.push({ message: { ...event, id: eventId }, callback });
118
118
 
119
119
  logger.log(
120
- `Event enqueued: ${getActionDescriptor(message.action, message.payload)}`
120
+ `Event enqueued: ${getActionDescriptor(event.action, event.payload)}`
121
121
  );
122
122
 
123
123
  if (!this.flushed) {
@@ -220,11 +220,11 @@ export class EventQueue {
220
220
  return false;
221
221
  }
222
222
 
223
- private async isDuplicated(hashToken: string) {
223
+ private async isDuplicate(eventId: string) {
224
224
  // check if exists a message with identical payload within 1 minute
225
- if (this.payloadHashes.has(hashToken)) return true;
225
+ if (this.payloadHashes.has(eventId)) return true;
226
226
 
227
- this.payloadHashes.add(hashToken);
227
+ this.payloadHashes.add(eventId);
228
228
  return false;
229
229
  }
230
230