@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/dist/cjs/src/FormoAnalytics.d.ts +4 -3
- package/dist/cjs/src/FormoAnalytics.d.ts.map +1 -1
- package/dist/cjs/src/FormoAnalytics.js +6 -5
- package/dist/cjs/src/FormoAnalytics.js.map +1 -1
- package/dist/cjs/src/FormoAnalyticsProvider.d.ts +1 -1
- package/dist/cjs/src/FormoAnalyticsProvider.d.ts.map +1 -1
- package/dist/cjs/src/FormoAnalyticsProvider.js +2 -2
- package/dist/cjs/src/FormoAnalyticsProvider.js.map +1 -1
- package/dist/cjs/src/types/base.d.ts +1 -0
- package/dist/cjs/src/types/base.d.ts.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/src/FormoAnalytics.d.ts +4 -3
- package/dist/esm/src/FormoAnalytics.d.ts.map +1 -1
- package/dist/esm/src/FormoAnalytics.js +6 -5
- package/dist/esm/src/FormoAnalytics.js.map +1 -1
- package/dist/esm/src/FormoAnalyticsProvider.d.ts +1 -1
- package/dist/esm/src/FormoAnalyticsProvider.d.ts.map +1 -1
- package/dist/esm/src/FormoAnalyticsProvider.js +2 -2
- package/dist/esm/src/FormoAnalyticsProvider.js.map +1 -1
- package/dist/esm/src/types/base.d.ts +1 -0
- package/dist/esm/src/types/base.d.ts.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/src/FormoAnalytics.ts +83 -72
- package/src/FormoAnalyticsProvider.tsx +2 -1
- package/src/types/base.ts +1 -0
package/package.json
CHANGED
package/src/FormoAnalytics.ts
CHANGED
|
@@ -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(
|
|
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(
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
232
|
-
|
|
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
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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(
|
|
275
|
+
console.warn(
|
|
276
|
+
'Session data missing. Attempting to fetch address from provider.'
|
|
277
|
+
);
|
|
267
278
|
try {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
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) {
|