@formo/analytics 1.26.0 → 1.28.0

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 (87) hide show
  1. package/README.md +6 -4
  2. package/dist/cjs/src/FormoAnalytics.d.ts +71 -12
  3. package/dist/cjs/src/FormoAnalytics.js +274 -147
  4. package/dist/cjs/src/event/EventFactory.d.ts +10 -2
  5. package/dist/cjs/src/event/EventFactory.js +32 -21
  6. package/dist/cjs/src/fetch/index.d.ts +10 -2
  7. package/dist/cjs/src/fetch/index.js +122 -4
  8. package/dist/cjs/src/index.d.ts +4 -0
  9. package/dist/cjs/src/index.js +6 -0
  10. package/dist/cjs/src/privy/index.d.ts +9 -0
  11. package/dist/cjs/src/privy/index.js +12 -0
  12. package/dist/cjs/src/privy/types.d.ts +175 -0
  13. package/dist/cjs/src/privy/types.js +12 -0
  14. package/dist/cjs/src/privy/utils.d.ts +32 -0
  15. package/dist/cjs/src/privy/utils.js +188 -0
  16. package/dist/cjs/src/queue/EventQueue.d.ts +24 -2
  17. package/dist/cjs/src/queue/EventQueue.js +158 -49
  18. package/dist/cjs/src/session/index.js +2 -1
  19. package/dist/cjs/src/solana/SolanaAdapter.d.ts +211 -0
  20. package/dist/cjs/src/solana/SolanaAdapter.js +975 -0
  21. package/dist/cjs/src/solana/SolanaManager.d.ts +24 -0
  22. package/dist/cjs/src/solana/SolanaManager.js +80 -0
  23. package/dist/cjs/src/solana/address.d.ts +72 -0
  24. package/dist/cjs/src/solana/address.js +176 -0
  25. package/dist/cjs/src/solana/index.d.ts +13 -0
  26. package/dist/cjs/src/solana/index.js +32 -0
  27. package/dist/cjs/src/solana/types.d.ts +206 -0
  28. package/dist/cjs/src/solana/types.js +80 -0
  29. package/dist/cjs/src/types/base.d.ts +25 -0
  30. package/dist/cjs/src/types/events.d.ts +9 -6
  31. package/dist/cjs/src/utils/address.d.ts +21 -0
  32. package/dist/cjs/src/utils/address.js +48 -1
  33. package/dist/cjs/src/utils/builderCode.d.ts +30 -0
  34. package/dist/cjs/src/utils/builderCode.js +143 -0
  35. package/dist/cjs/src/utils/index.d.ts +1 -0
  36. package/dist/cjs/src/utils/index.js +1 -0
  37. package/dist/cjs/src/version.d.ts +1 -1
  38. package/dist/cjs/src/version.js +1 -1
  39. package/dist/cjs/src/wagmi/WagmiEventHandler.d.ts +24 -0
  40. package/dist/cjs/src/wagmi/WagmiEventHandler.js +242 -24
  41. package/dist/cjs/src/wagmi/types.d.ts +31 -0
  42. package/dist/cjs/src/wagmi/utils.d.ts +79 -0
  43. package/dist/cjs/src/wagmi/utils.js +218 -0
  44. package/dist/esm/src/FormoAnalytics.d.ts +71 -12
  45. package/dist/esm/src/FormoAnalytics.js +275 -148
  46. package/dist/esm/src/event/EventFactory.d.ts +10 -2
  47. package/dist/esm/src/event/EventFactory.js +34 -23
  48. package/dist/esm/src/fetch/index.d.ts +10 -2
  49. package/dist/esm/src/fetch/index.js +123 -2
  50. package/dist/esm/src/index.d.ts +4 -0
  51. package/dist/esm/src/index.js +3 -0
  52. package/dist/esm/src/privy/index.d.ts +9 -0
  53. package/dist/esm/src/privy/index.js +8 -0
  54. package/dist/esm/src/privy/types.d.ts +175 -0
  55. package/dist/esm/src/privy/types.js +11 -0
  56. package/dist/esm/src/privy/utils.d.ts +32 -0
  57. package/dist/esm/src/privy/utils.js +185 -0
  58. package/dist/esm/src/queue/EventQueue.d.ts +24 -2
  59. package/dist/esm/src/queue/EventQueue.js +158 -49
  60. package/dist/esm/src/session/index.js +2 -1
  61. package/dist/esm/src/solana/SolanaAdapter.d.ts +211 -0
  62. package/dist/esm/src/solana/SolanaAdapter.js +972 -0
  63. package/dist/esm/src/solana/SolanaManager.d.ts +24 -0
  64. package/dist/esm/src/solana/SolanaManager.js +77 -0
  65. package/dist/esm/src/solana/address.d.ts +72 -0
  66. package/dist/esm/src/solana/address.js +167 -0
  67. package/dist/esm/src/solana/index.d.ts +13 -0
  68. package/dist/esm/src/solana/index.js +13 -0
  69. package/dist/esm/src/solana/types.d.ts +206 -0
  70. package/dist/esm/src/solana/types.js +74 -0
  71. package/dist/esm/src/types/base.d.ts +25 -0
  72. package/dist/esm/src/types/events.d.ts +9 -6
  73. package/dist/esm/src/utils/address.d.ts +21 -0
  74. package/dist/esm/src/utils/address.js +45 -0
  75. package/dist/esm/src/utils/builderCode.d.ts +30 -0
  76. package/dist/esm/src/utils/builderCode.js +140 -0
  77. package/dist/esm/src/utils/index.d.ts +1 -0
  78. package/dist/esm/src/utils/index.js +1 -0
  79. package/dist/esm/src/version.d.ts +1 -1
  80. package/dist/esm/src/version.js +1 -1
  81. package/dist/esm/src/wagmi/WagmiEventHandler.d.ts +24 -0
  82. package/dist/esm/src/wagmi/WagmiEventHandler.js +242 -24
  83. package/dist/esm/src/wagmi/types.d.ts +31 -0
  84. package/dist/esm/src/wagmi/utils.d.ts +79 -0
  85. package/dist/esm/src/wagmi/utils.js +211 -0
  86. package/dist/index.umd.min.js +1 -1
  87. package/package.json +20 -4
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Utility functions for extracting profile properties from Privy user objects.
3
+ */
4
+ /**
5
+ * Extract profile properties and wallet addresses from a Privy user object.
6
+ *
7
+ * Parses the Privy user's linked accounts into a flat properties object
8
+ * (email, social accounts, etc.) and extracts all linked wallet addresses.
9
+ *
10
+ * @param user - The Privy user object from `usePrivy()`
11
+ * @returns An object with `properties` and `wallets`
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { parsePrivyProperties } from '@formo/analytics';
16
+ *
17
+ * const { user } = usePrivy();
18
+ * if (user) {
19
+ * const { properties, wallets } = parsePrivyProperties(user);
20
+ *
21
+ * for (const wallet of wallets) {
22
+ * formo.identify({ address: wallet.address, userId: user.id }, properties);
23
+ * }
24
+ * }
25
+ * ```
26
+ */
27
+ export function parsePrivyProperties(user) {
28
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
29
+ var accounts = user.linkedAccounts || [];
30
+ // Extract profile properties
31
+ var properties = {
32
+ privyDid: user.id,
33
+ privyCreatedAt: user.createdAt,
34
+ };
35
+ // Email
36
+ if ((_a = user.email) === null || _a === void 0 ? void 0 : _a.address) {
37
+ properties.email = user.email.address;
38
+ }
39
+ // Social accounts - extract usernames/identifiers
40
+ if ((_b = user.apple) === null || _b === void 0 ? void 0 : _b.email) {
41
+ properties.apple = user.apple.email;
42
+ }
43
+ if ((_c = user.discord) === null || _c === void 0 ? void 0 : _c.username) {
44
+ properties.discord = user.discord.username;
45
+ }
46
+ if ((_d = user.farcaster) === null || _d === void 0 ? void 0 : _d.username) {
47
+ properties.farcaster = user.farcaster.username;
48
+ }
49
+ if ((_e = user.github) === null || _e === void 0 ? void 0 : _e.username) {
50
+ properties.github = user.github.username;
51
+ }
52
+ if ((_f = user.google) === null || _f === void 0 ? void 0 : _f.email) {
53
+ properties.google = user.google.email;
54
+ }
55
+ if ((_g = user.instagram) === null || _g === void 0 ? void 0 : _g.username) {
56
+ properties.instagram = user.instagram.username;
57
+ }
58
+ if ((_h = user.line) === null || _h === void 0 ? void 0 : _h.email) {
59
+ properties.line = user.line.email;
60
+ }
61
+ if ((_j = user.linkedin) === null || _j === void 0 ? void 0 : _j.email) {
62
+ properties.linkedin = user.linkedin.email;
63
+ }
64
+ if ((_k = user.spotify) === null || _k === void 0 ? void 0 : _k.email) {
65
+ properties.spotify = user.spotify.email;
66
+ }
67
+ if ((_l = user.telegram) === null || _l === void 0 ? void 0 : _l.username) {
68
+ properties.telegram = user.telegram.username;
69
+ }
70
+ if ((_m = user.tiktok) === null || _m === void 0 ? void 0 : _m.username) {
71
+ properties.tiktok = user.tiktok.username;
72
+ }
73
+ if ((_o = user.twitter) === null || _o === void 0 ? void 0 : _o.username) {
74
+ properties.twitter = user.twitter.username;
75
+ }
76
+ // Fallback to linkedAccounts if convenience accessors are not populated
77
+ if (!properties.email) {
78
+ var emailAccount = accounts.find(function (account) { return account.type === "email"; });
79
+ if (emailAccount === null || emailAccount === void 0 ? void 0 : emailAccount.address) {
80
+ properties.email = emailAccount.address;
81
+ }
82
+ }
83
+ if (!properties.apple) {
84
+ var appleAccount = accounts.find(function (a) { return a.type === "apple_oauth"; });
85
+ if (appleAccount === null || appleAccount === void 0 ? void 0 : appleAccount.email) {
86
+ properties.apple = appleAccount.email;
87
+ }
88
+ }
89
+ if (!properties.discord) {
90
+ var discordAccount = accounts.find(function (a) { return a.type === "discord_oauth"; });
91
+ if (discordAccount === null || discordAccount === void 0 ? void 0 : discordAccount.username) {
92
+ properties.discord = discordAccount.username;
93
+ }
94
+ }
95
+ if (!properties.farcaster) {
96
+ var farcasterAccount = accounts.find(function (a) { return a.type === "farcaster"; });
97
+ if (farcasterAccount === null || farcasterAccount === void 0 ? void 0 : farcasterAccount.username) {
98
+ properties.farcaster = farcasterAccount.username;
99
+ }
100
+ else if (farcasterAccount === null || farcasterAccount === void 0 ? void 0 : farcasterAccount.displayName) {
101
+ properties.farcaster = farcasterAccount.displayName;
102
+ }
103
+ }
104
+ if (!properties.github) {
105
+ var githubAccount = accounts.find(function (a) { return a.type === "github_oauth"; });
106
+ if (githubAccount === null || githubAccount === void 0 ? void 0 : githubAccount.username) {
107
+ properties.github = githubAccount.username;
108
+ }
109
+ }
110
+ if (!properties.google) {
111
+ var googleAccount = accounts.find(function (a) { return a.type === "google_oauth"; });
112
+ if (googleAccount === null || googleAccount === void 0 ? void 0 : googleAccount.email) {
113
+ properties.google = googleAccount.email;
114
+ }
115
+ }
116
+ if (!properties.instagram) {
117
+ var instagramAccount = accounts.find(function (a) { return a.type === "instagram_oauth"; });
118
+ if (instagramAccount === null || instagramAccount === void 0 ? void 0 : instagramAccount.username) {
119
+ properties.instagram = instagramAccount.username;
120
+ }
121
+ }
122
+ if (!properties.line) {
123
+ var lineAccount = accounts.find(function (a) { return a.type === "line"; });
124
+ if (lineAccount === null || lineAccount === void 0 ? void 0 : lineAccount.email) {
125
+ properties.line = lineAccount.email;
126
+ }
127
+ }
128
+ if (!properties.linkedin) {
129
+ var linkedinAccount = accounts.find(function (a) { return a.type === "linkedin_oauth"; });
130
+ if (linkedinAccount === null || linkedinAccount === void 0 ? void 0 : linkedinAccount.email) {
131
+ properties.linkedin = linkedinAccount.email;
132
+ }
133
+ }
134
+ if (!properties.spotify) {
135
+ var spotifyAccount = accounts.find(function (a) { return a.type === "spotify_oauth"; });
136
+ if (spotifyAccount === null || spotifyAccount === void 0 ? void 0 : spotifyAccount.email) {
137
+ properties.spotify = spotifyAccount.email;
138
+ }
139
+ }
140
+ if (!properties.telegram) {
141
+ var telegramAccount = accounts.find(function (a) { return a.type === "telegram"; });
142
+ if (telegramAccount === null || telegramAccount === void 0 ? void 0 : telegramAccount.username) {
143
+ properties.telegram = telegramAccount.username;
144
+ }
145
+ else if (telegramAccount === null || telegramAccount === void 0 ? void 0 : telegramAccount.telegramUserId) {
146
+ properties.telegram = telegramAccount.telegramUserId;
147
+ }
148
+ }
149
+ if (!properties.tiktok) {
150
+ var tiktokAccount = accounts.find(function (a) { return a.type === "tiktok_oauth"; });
151
+ if (tiktokAccount === null || tiktokAccount === void 0 ? void 0 : tiktokAccount.username) {
152
+ properties.tiktok = tiktokAccount.username;
153
+ }
154
+ }
155
+ if (!properties.twitter) {
156
+ var twitterAccount = accounts.find(function (a) { return a.type === "twitter_oauth"; });
157
+ if (twitterAccount === null || twitterAccount === void 0 ? void 0 : twitterAccount.username) {
158
+ properties.twitter = twitterAccount.username;
159
+ }
160
+ }
161
+ // Use OAuth emails as fallback for email if still blank
162
+ // Priority: email -> google -> apple -> linkedin
163
+ if (!properties.email) {
164
+ if (properties.google) {
165
+ properties.email = properties.google;
166
+ }
167
+ else if (properties.apple) {
168
+ properties.email = properties.apple;
169
+ }
170
+ else if (properties.linkedin) {
171
+ properties.email = properties.linkedin;
172
+ }
173
+ }
174
+ // Extract wallet addresses
175
+ var wallets = accounts
176
+ .filter(function (a) { return a.type === "wallet" && a.address; })
177
+ .map(function (a) { return ({
178
+ address: a.address,
179
+ walletClient: a.walletClientType || a.walletClient,
180
+ chainType: a.chainType,
181
+ isEmbedded: a.walletClientType === "privy" || a.walletClient === "privy",
182
+ }); });
183
+ return { properties: properties, wallets: wallets };
184
+ }
185
+ //# sourceMappingURL=utils.js.map
@@ -1,5 +1,8 @@
1
- import { IFormoEvent } from "../types";
1
+ import { IFormoEvent, IFormoEventPayload } from "../types";
2
2
  import { IEventQueue } from "./type";
3
+ type IFormoEventFlushPayload = IFormoEventPayload & {
4
+ sent_at: string;
5
+ };
3
6
  type Options = {
4
7
  apiHost: string;
5
8
  flushAt?: number;
@@ -25,7 +28,26 @@ export declare class EventQueue implements IEventQueue {
25
28
  constructor(writeKey: string, options: Options);
26
29
  private generateMessageId;
27
30
  enqueue(event: IFormoEvent, callback?: (...args: any) => void): Promise<void>;
28
- flush(callback?: (...args: any) => void): Promise<any>;
31
+ flush(callback?: (...args: any) => void): Promise<void | IFormoEventFlushPayload[]>;
32
+ /**
33
+ * Returns the UTF-8 byte length of a string. The browser's keepalive limit
34
+ * is enforced on the wire (UTF-8 bytes), not on JS string length (UTF-16
35
+ * code units). Non-ASCII characters (CJK, emoji) can be 2–4x larger in
36
+ * UTF-8 than their string .length suggests.
37
+ */
38
+ private static byteLength;
39
+ /**
40
+ * Splits events into batches that respect the browser's 64KB keepalive
41
+ * payload size limit. Each batch pairs its serialized data with the
42
+ * original queue items (for per-item callback reporting) and a flag
43
+ * indicating whether keepalive is safe to use.
44
+ */
45
+ private splitIntoBatches;
46
+ /**
47
+ * Sends batches sequentially, notifying per-item callbacks on success/failure.
48
+ * Returns the first error encountered (if any) so the caller can report it.
49
+ */
50
+ private sendBatches;
29
51
  private isErrorRetryable;
30
52
  private isDuplicate;
31
53
  private onPageLeave;
@@ -51,6 +51,16 @@ import { logger } from "../logger";
51
51
  import { EVENTS_API_REQUEST_HEADER } from "../constants";
52
52
  import fetch from "../fetch";
53
53
  var noop = function () { };
54
+ var safeCall = function (fn) {
55
+ var args = [];
56
+ for (var _i = 1; _i < arguments.length; _i++) {
57
+ args[_i - 1] = arguments[_i];
58
+ }
59
+ try {
60
+ fn.apply(void 0, args);
61
+ }
62
+ catch ( /* swallow */_a) { /* swallow */ }
63
+ };
54
64
  var DEFAULT_RETRY = 3;
55
65
  var MAX_RETRY = 5;
56
66
  var MIN_RETRY = 1;
@@ -60,6 +70,10 @@ var MIN_FLUSH_AT = 1;
60
70
  var DEFAULT_QUEUE_SIZE = 1024 * 500; // 500kB
61
71
  var MAX_QUEUE_SIZE = 1024 * 500; // 500kB
62
72
  var MIN_QUEUE_SIZE = 200; // 200 bytes
73
+ // Browsers enforce a 64KB limit on the total body size of in-flight
74
+ // keepalive fetch requests. Payloads exceeding this are silently cancelled,
75
+ // producing a TypeError: Failed to fetch that cannot be resolved by retrying.
76
+ var KEEPALIVE_PAYLOAD_LIMIT = 64 * 1024; // 64kB
63
77
  var DEFAULT_FLUSH_INTERVAL = 1000 * 30; // 1 MINUTE
64
78
  var MAX_FLUSH_INTERVAL = 1000 * 300; // 5 MINUTES
65
79
  var MIN_FLUSH_INTERVAL = 1000 * 10; // 10 SECONDS
@@ -144,7 +158,6 @@ var EventQueue = /** @class */ (function () {
144
158
  });
145
159
  }); });
146
160
  }
147
- //#region Public functions
148
161
  EventQueue.prototype.generateMessageId = function (event) {
149
162
  return __awaiter(this, void 0, void 0, function () {
150
163
  var formattedTimestamp, eventForHashing, eventString;
@@ -200,7 +213,7 @@ var EventQueue = /** @class */ (function () {
200
213
  };
201
214
  EventQueue.prototype.flush = function (callback) {
202
215
  return __awaiter(this, void 0, void 0, function () {
203
- var err_1, items, sentAt, data, done;
216
+ var items, sentAt, data, batches;
204
217
  var _this = this;
205
218
  return __generator(this, function (_a) {
206
219
  switch (_a.label) {
@@ -214,75 +227,171 @@ var EventQueue = /** @class */ (function () {
214
227
  callback();
215
228
  return [2 /*return*/, Promise.resolve()];
216
229
  }
217
- _a.label = 1;
218
- case 1:
219
- _a.trys.push([1, 4, , 5]);
220
- if (!this.pendingFlush) return [3 /*break*/, 3];
230
+ if (!this.pendingFlush) return [3 /*break*/, 2];
221
231
  return [4 /*yield*/, this.pendingFlush];
222
- case 2:
232
+ case 1:
223
233
  _a.sent();
224
- _a.label = 3;
225
- case 3: return [3 /*break*/, 5];
226
- case 4:
227
- err_1 = _a.sent();
228
- this.pendingFlush = null;
229
- throw err_1;
230
- case 5:
234
+ _a.label = 2;
235
+ case 2:
231
236
  items = this.queue.splice(0, this.flushAt);
232
237
  this.payloadHashes.clear();
233
238
  sentAt = new Date().toISOString();
234
239
  data = items.map(function (item) { return (__assign(__assign({}, item.message), { sent_at: sentAt })); });
235
- done = function (err) {
236
- items.forEach(function (_a) {
237
- var message = _a.message, callback = _a.callback;
238
- return callback(err, message, data);
239
- });
240
- callback(err, data);
241
- };
242
- return [2 /*return*/, (this.pendingFlush = fetch("".concat(this.apiHost), {
243
- headers: EVENTS_API_REQUEST_HEADER(this.writeKey),
244
- method: "POST",
245
- body: JSON.stringify(data),
246
- keepalive: true,
247
- retries: this.retryCount,
248
- retryDelay: function (attempt) { return Math.pow(2, attempt) * 1000; }, // exponential backoff
249
- retryOn: function (_, error) { return _this.isErrorRetryable(error); },
250
- })
251
- .then(function () {
252
- done();
240
+ batches = this.splitIntoBatches(items, data);
241
+ return [2 /*return*/, (this.pendingFlush = this.sendBatches(batches, data)
242
+ .then(function (firstError) {
243
+ if (firstError) {
244
+ safeCall(callback, firstError, data);
245
+ if (typeof _this.errorHandler === "function") {
246
+ safeCall(_this.errorHandler, firstError);
247
+ }
248
+ }
249
+ else {
250
+ safeCall(callback, undefined, data);
251
+ }
253
252
  return Promise.resolve(data);
254
253
  })
255
254
  .catch(function (err) {
255
+ // Defensive: should not be reachable since sendBatches catches
256
+ // all errors internally, but guard against unexpected failures.
257
+ safeCall(callback, err, data);
256
258
  if (typeof _this.errorHandler === "function") {
257
- done(err);
258
- return _this.errorHandler(err);
259
- }
260
- if (err.response) {
261
- var error = new Error(err.response.statusText);
262
- done(error);
263
- throw error;
259
+ safeCall(_this.errorHandler, err);
264
260
  }
265
- done(err);
266
- throw err;
261
+ // Do NOT re-throw — analytics errors should never
262
+ // propagate as unhandled rejections to the host app
267
263
  }))];
268
264
  }
269
265
  });
270
266
  });
271
267
  };
272
- //#region Utility functions
273
- EventQueue.prototype.isErrorRetryable = function (error) {
274
- var _a, _b, _c;
268
+ /**
269
+ * Returns the UTF-8 byte length of a string. The browser's keepalive limit
270
+ * is enforced on the wire (UTF-8 bytes), not on JS string length (UTF-16
271
+ * code units). Non-ASCII characters (CJK, emoji) can be 2–4x larger in
272
+ * UTF-8 than their string .length suggests.
273
+ */
274
+ EventQueue.byteLength = function (str) {
275
+ return new TextEncoder().encode(str).byteLength;
276
+ };
277
+ /**
278
+ * Splits events into batches that respect the browser's 64KB keepalive
279
+ * payload size limit. Each batch pairs its serialized data with the
280
+ * original queue items (for per-item callback reporting) and a flag
281
+ * indicating whether keepalive is safe to use.
282
+ */
283
+ EventQueue.prototype.splitIntoBatches = function (items, data) {
284
+ var serialized = JSON.stringify(data);
285
+ if (EventQueue.byteLength(serialized) <= KEEPALIVE_PAYLOAD_LIMIT) {
286
+ return [{ data: data, items: items, keepalive: true }];
287
+ }
288
+ var batches = [];
289
+ var currentData = [];
290
+ var currentItems = [];
291
+ var currentSize = 2; // account for JSON array brackets "[]"
292
+ for (var i = 0; i < data.length; i++) {
293
+ var event_1 = data[i];
294
+ var eventSize = EventQueue.byteLength(JSON.stringify(event_1));
295
+ var sizeWithEvent = currentSize + (currentData.length > 0 ? 1 : 0) + eventSize;
296
+ if (sizeWithEvent > KEEPALIVE_PAYLOAD_LIMIT) {
297
+ if (currentData.length > 0) {
298
+ batches.push({ data: currentData, items: currentItems, keepalive: true });
299
+ }
300
+ // If a single event exceeds the limit, send it without keepalive
301
+ if (eventSize + 2 > KEEPALIVE_PAYLOAD_LIMIT) {
302
+ batches.push({ data: [event_1], items: [items[i]], keepalive: false });
303
+ currentData = [];
304
+ currentItems = [];
305
+ currentSize = 2;
306
+ }
307
+ else {
308
+ currentData = [event_1];
309
+ currentItems = [items[i]];
310
+ currentSize = 2 + eventSize;
311
+ }
312
+ }
313
+ else {
314
+ currentData.push(event_1);
315
+ currentItems.push(items[i]);
316
+ currentSize = sizeWithEvent;
317
+ }
318
+ }
319
+ if (currentData.length > 0) {
320
+ batches.push({ data: currentData, items: currentItems, keepalive: true });
321
+ }
322
+ return batches;
323
+ };
324
+ /**
325
+ * Sends batches sequentially, notifying per-item callbacks on success/failure.
326
+ * Returns the first error encountered (if any) so the caller can report it.
327
+ */
328
+ EventQueue.prototype.sendBatches = function (batches, allData) {
329
+ return __awaiter(this, void 0, void 0, function () {
330
+ var firstError, _i, batches_1, batch, body, response, error, err_1;
331
+ var _this = this;
332
+ return __generator(this, function (_a) {
333
+ switch (_a.label) {
334
+ case 0:
335
+ _i = 0, batches_1 = batches;
336
+ _a.label = 1;
337
+ case 1:
338
+ if (!(_i < batches_1.length)) return [3 /*break*/, 6];
339
+ batch = batches_1[_i];
340
+ _a.label = 2;
341
+ case 2:
342
+ _a.trys.push([2, 4, , 5]);
343
+ body = JSON.stringify(batch.data);
344
+ return [4 /*yield*/, fetch("".concat(this.apiHost), {
345
+ headers: EVENTS_API_REQUEST_HEADER(this.writeKey),
346
+ method: "POST",
347
+ body: body,
348
+ keepalive: batch.keepalive,
349
+ retries: this.retryCount,
350
+ retryDelay: function (attempt) { return Math.pow(2, attempt) * 1000; },
351
+ retryOn: function (_, error, response) { return _this.isErrorRetryable(error, response); },
352
+ })];
353
+ case 3:
354
+ response = _a.sent();
355
+ if (!response.ok) {
356
+ error = new Error(response.statusText || "HTTP ".concat(response.status));
357
+ error.response = response;
358
+ throw error;
359
+ }
360
+ batch.items.forEach(function (_a) {
361
+ var message = _a.message, cb = _a.callback;
362
+ return safeCall(cb, undefined, message, allData);
363
+ });
364
+ return [3 /*break*/, 5];
365
+ case 4:
366
+ err_1 = _a.sent();
367
+ firstError = firstError || err_1;
368
+ batch.items.forEach(function (_a) {
369
+ var message = _a.message, cb = _a.callback;
370
+ return safeCall(cb, err_1, message, allData);
371
+ });
372
+ return [3 /*break*/, 5];
373
+ case 5:
374
+ _i++;
375
+ return [3 /*break*/, 1];
376
+ case 6: return [2 /*return*/, firstError];
377
+ }
378
+ });
379
+ });
380
+ };
381
+ EventQueue.prototype.isErrorRetryable = function (error, response) {
382
+ var _a, _b;
275
383
  // Retry Network Errors.
276
- if (isNetworkError(error))
384
+ if (error && isNetworkError(error))
277
385
  return true;
278
- // Cannot determine if the request can be retried
279
- if (!(error === null || error === void 0 ? void 0 : error.response))
386
+ // Check response status if available
387
+ var status = (_a = response === null || response === void 0 ? void 0 : response.status) !== null && _a !== void 0 ? _a : (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.status;
388
+ if (!status)
280
389
  return false;
281
390
  // Retry Server Errors (5xx).
282
- if (((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) >= 500 && ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.status) <= 599)
391
+ if (status >= 500 && status <= 599)
283
392
  return true;
284
393
  // Retry if rate limited.
285
- if (((_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.status) === 429)
394
+ if (status === 429)
286
395
  return true;
287
396
  return false;
288
397
  };
@@ -96,7 +96,8 @@ var FormoAnalyticsSession = /** @class */ (function () {
96
96
  var _a;
97
97
  var identifiedKey = this.generateIdentificationKey(address, rdns);
98
98
  var identifiedWallets = ((_a = cookie().get(SESSION_WALLET_IDENTIFIED_KEY)) === null || _a === void 0 ? void 0 : _a.split(",")) || [];
99
- if (!identifiedWallets.includes(identifiedKey)) {
99
+ var alreadyExists = identifiedWallets.includes(identifiedKey);
100
+ if (!alreadyExists) {
100
101
  identifiedWallets.push(identifiedKey);
101
102
  var newValue = identifiedWallets.join(",");
102
103
  cookie().set(SESSION_WALLET_IDENTIFIED_KEY, newValue, {