@formo/analytics 1.11.6-alpha.6 → 1.11.6-alpha.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formo/analytics",
3
- "version": "1.11.6-alpha.6",
3
+ "version": "1.11.6-alpha.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/getformo/sdk.git"
@@ -12,7 +12,11 @@ interface IFormoAnalytics {
12
12
  /**
13
13
  * Initializes the FormoAnalytics instance with the provided API key and project ID.
14
14
  */
15
- init(apiKey: string, options: Options): Promise<FormoAnalytics>;
15
+ init(
16
+ apiKey: string,
17
+ projectId: string,
18
+ options: Options
19
+ ): Promise<FormoAnalytics>;
16
20
 
17
21
  /**
18
22
  * Tracks page visit events.
@@ -59,6 +63,7 @@ export class FormoAnalytics implements IFormoAnalytics {
59
63
 
60
64
  private constructor(
61
65
  public readonly apiKey: string,
66
+ public readonly projectId: string,
62
67
  public options: Options = {}
63
68
  ) {
64
69
  this.config = {
@@ -72,11 +77,15 @@ export class FormoAnalytics implements IFormoAnalytics {
72
77
  }
73
78
  }
74
79
 
75
- static async init(apiKey: string, options: Options): Promise<FormoAnalytics> {
80
+ static async init(
81
+ apiKey: string,
82
+ projectId: string,
83
+ options: Options
84
+ ): Promise<FormoAnalytics> {
76
85
  const config = {
77
86
  token: apiKey,
78
87
  };
79
- const instance = new FormoAnalytics(apiKey, options);
88
+ const instance = new FormoAnalytics(apiKey, projectId, options);
80
89
  instance.config = config;
81
90
 
82
91
  return instance;
@@ -136,58 +145,58 @@ export class FormoAnalytics implements IFormoAnalytics {
136
145
  console.log('Wallet address before sending event:', address); // Add this log
137
146
 
138
147
  const requestData = {
139
- address: address,
140
- session_id: this.getSessionId(),
141
- timestamp: new Date().toISOString(),
142
- action,
143
- version: '1',
144
- payload,
148
+ address: address,
149
+ session_id: this.getSessionId(),
150
+ timestamp: new Date().toISOString(),
151
+ action,
152
+ version: '1',
153
+ payload,
145
154
  };
146
155
 
147
156
  const sendRequest = async (): Promise<void> => {
148
- try {
149
- const response = await axios.post(
150
- EVENTS_API_URL,
151
- JSON.stringify(requestData),
152
- {
153
- headers: {
154
- 'Content-Type': 'application/json',
155
- Authorization: `Bearer ${this.apiKey}`,
156
- },
157
- }
158
- );
159
-
160
- if (response.status >= 200 && response.status < 300) {
161
- console.log('address:', address);
162
- console.log('Event sent successfully:', action);
163
- } else {
164
- throw new Error(`Failed with status: ${response.status}`);
165
- }
166
- } catch (error) {
167
- attempt++;
168
-
169
- if (attempt <= maxRetries) {
170
- const retryDelay = Math.pow(2, attempt) * 1000;
171
- console.error(
172
- `Attempt ${attempt}: Retrying event "${action}" in ${
173
- retryDelay / 1000
174
- } seconds...`
175
- );
176
- setTimeout(sendRequest, retryDelay);
177
- } else {
178
- H.consumeError(
179
- error as Error,
180
- `Request data: ${JSON.stringify(requestData)}`
181
- );
182
- console.error(
183
- `Event "${action}" failed after ${maxRetries} attempts. Error: ${error}`
184
- );
185
- }
157
+ try {
158
+ const response = await axios.post(
159
+ EVENTS_API_URL,
160
+ JSON.stringify(requestData),
161
+ {
162
+ headers: {
163
+ 'Content-Type': 'application/json',
164
+ Authorization: `Bearer ${this.apiKey}`,
165
+ },
166
+ }
167
+ );
168
+
169
+ if (response.status >= 200 && response.status < 300) {
170
+ console.log('address:', address);
171
+ console.log('Event sent successfully:', action);
172
+ } else {
173
+ throw new Error(`Failed with status: ${response.status}`);
186
174
  }
175
+ } catch (error) {
176
+ attempt++;
177
+
178
+ if (attempt <= maxRetries) {
179
+ const retryDelay = Math.pow(2, attempt) * 1000;
180
+ console.error(
181
+ `Attempt ${attempt}: Retrying event "${action}" in ${
182
+ retryDelay / 1000
183
+ } seconds...`
184
+ );
185
+ setTimeout(sendRequest, retryDelay);
186
+ } else {
187
+ H.consumeError(
188
+ error as Error,
189
+ `Request data: ${JSON.stringify(requestData)}`
190
+ );
191
+ console.error(
192
+ `Event "${action}" failed after ${maxRetries} attempts. Error: ${error}`
193
+ );
194
+ }
195
+ }
187
196
  };
188
197
 
189
198
  await sendRequest();
190
- }
199
+ }
191
200
 
192
201
  // Function to track page hits
193
202
  private trackPageHit() {
@@ -228,22 +237,22 @@ export class FormoAnalytics implements IFormoAnalytics {
228
237
 
229
238
  private trackProvider(provider: EIP1193Provider) {
230
239
  if (provider === this._provider) {
231
- console.log('Provider already tracked.');
232
- return;
240
+ console.log('Provider already tracked.');
241
+ return;
233
242
  }
234
243
 
235
244
  this.currentChainId = undefined;
236
245
  this.currentConnectedAddress = undefined;
237
246
 
238
247
  if (this._provider) {
239
- const eventNames = Object.keys(this._registeredProviderListeners);
240
- for (const eventName of eventNames) {
241
- this._provider.removeListener(
242
- eventName,
243
- this._registeredProviderListeners[eventName]
244
- );
245
- delete this._registeredProviderListeners[eventName];
246
- }
248
+ const eventNames = Object.keys(this._registeredProviderListeners);
249
+ for (const eventName of eventNames) {
250
+ this._provider.removeListener(
251
+ eventName,
252
+ this._registeredProviderListeners[eventName]
253
+ );
254
+ delete this._registeredProviderListeners[eventName];
255
+ }
247
256
  }
248
257
 
249
258
  console.log('Tracking new provider:', provider);
@@ -252,7 +261,7 @@ export class FormoAnalytics implements IFormoAnalytics {
252
261
  this.getCurrentWallet();
253
262
  this.registerAddressChangedListener();
254
263
  this.registerChainChangedListener();
255
- }
264
+ }
256
265
 
257
266
  private async getCurrentWallet() {
258
267
  if (!this.provider) {
@@ -263,21 +272,23 @@ export class FormoAnalytics implements IFormoAnalytics {
263
272
  const sessionData = sessionStorage.getItem(this.walletAddressSessionKey);
264
273
 
265
274
  if (!sessionData) {
266
- console.warn('Session data missing. Attempting to fetch address from provider.');
275
+ console.warn(
276
+ 'Session data missing. Attempting to fetch address from provider.'
277
+ );
267
278
  try {
268
- const accounts = await this.provider.request<string[]>({
269
- method: 'eth_accounts',
270
- });
271
- if (accounts && accounts.length > 0) {
272
- const address = accounts[0];
273
- this.storeWalletAddress(address);
274
- return address;
275
- }
279
+ const accounts = await this.provider.request<string[]>({
280
+ method: 'eth_accounts',
281
+ });
282
+ if (accounts && accounts.length > 0) {
283
+ const address = accounts[0];
284
+ this.storeWalletAddress(address);
285
+ return address;
286
+ }
276
287
  } catch (err) {
277
- console.error('Failed to fetch accounts from provider:', err);
288
+ console.error('Failed to fetch accounts from provider:', err);
278
289
  }
279
290
  return null;
280
- }
291
+ }
281
292
 
282
293
  const parsedData = JSON.parse(sessionData);
283
294
  const sessionExpiry = 30 * 60 * 1000; // 30 minutes
@@ -446,8 +457,8 @@ export class FormoAnalytics implements IFormoAnalytics {
446
457
  sessionStorage.removeItem(this.walletAddressSessionKey);
447
458
  }
448
459
 
449
- init(apiKey: string, options: Options): Promise<FormoAnalytics> {
450
- const instance = new FormoAnalytics(apiKey, options);
460
+ init(apiKey: string, projectId: string, options: Options): Promise<FormoAnalytics> {
461
+ const instance = new FormoAnalytics(apiKey, projectId, options);
451
462
  return Promise.resolve(instance);
452
463
  }
453
464
 
@@ -19,6 +19,7 @@ export const FormoAnalyticsContext = createContext<FormoAnalytics | undefined>(
19
19
  export const FormoAnalyticsProvider = ({
20
20
  apiKey,
21
21
  options,
22
+ projectId,
22
23
  disabled,
23
24
  children,
24
25
  }: FormoAnalyticsProviderProps) => {
@@ -61,7 +62,7 @@ export const FormoAnalyticsProvider = ({
61
62
 
62
63
  // Initialize FormoAnalytics
63
64
  try {
64
- const sdkInstance = await FormoAnalytics.init(apiKey, options);
65
+ const sdkInstance = await FormoAnalytics.init(apiKey, projectId, options);
65
66
  setSdk(sdkInstance);
66
67
  console.log('FormoAnalytics SDK initialized successfully');
67
68
  } catch (error) {
package/src/types/base.ts CHANGED
@@ -8,6 +8,7 @@ export interface Options {
8
8
 
9
9
  export interface FormoAnalyticsProviderProps {
10
10
  apiKey: string;
11
+ projectId: string;
11
12
  options: Options;
12
13
  disabled?: boolean;
13
14
  children: React.ReactNode;