cryptique-sdk 1.2.10 → 1.2.12

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/lib/cjs/index.js CHANGED
@@ -158,8 +158,9 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
158
158
  timer: null, // Session tracking interval timer
159
159
  countryName: null, // Cached country name
160
160
  isInitialized: false, // Prevents API calls during initialization
161
+ sessionConfirmed: false, // True after first successful POST /api/sdk/track (session exists on server)
161
162
  eip6963Providers: [], // EIP-6963 discovered wallet providers
162
- reportedWalletAddress: null // Last wallet address reported to backend (prevents duplicate calls)
163
+ reportedWalletAddress: null // Last wallet address reported to backend (prevents duplicate calls)
163
164
  };
164
165
 
165
166
  // Ready promise - resolves when SDK is fully initialized (allows consumers to await init)
@@ -168,6 +169,12 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
168
169
  _readyPromiseResolve = resolve;
169
170
  });
170
171
 
172
+ // Session confirmed promise - resolves when first POST /api/sdk/track succeeds (so events can safely use sessionId)
173
+ let _sessionConfirmedPromiseResolve;
174
+ const _sessionConfirmedPromise = new Promise((resolve) => {
175
+ _sessionConfirmedPromiseResolve = resolve;
176
+ });
177
+
171
178
  // Helper to wait for SDK ready with timeout (avoids hanging if init fails)
172
179
  const _waitForReady = (timeoutMs = 5000) => {
173
180
  if (runtimeState.isInitialized) return Promise.resolve();
@@ -177,6 +184,15 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
177
184
  ]);
178
185
  };
179
186
 
187
+ // Helper to wait for first track success before sending events (avoids 404 Session not found)
188
+ const _waitForSessionConfirmed = (timeoutMs = 12000) => {
189
+ if (runtimeState.sessionConfirmed) return Promise.resolve();
190
+ return Promise.race([
191
+ _sessionConfirmedPromise,
192
+ new Promise((_, reject) => setTimeout(() => reject(new Error('Session confirm timeout')), timeoutMs))
193
+ ]);
194
+ };
195
+
180
196
  // Initialize EIP-6963 wallet provider detection (if enabled)
181
197
  {
182
198
  // Listen for providers being announced
@@ -4404,6 +4420,13 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
4404
4420
 
4405
4421
  throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(responseData)}`);
4406
4422
  }
4423
+
4424
+ // First successful TRACK creates session on server; allow events to send only after this
4425
+ if (response.ok && endpoint === CONFIG.API.TRACK && typeof _sessionConfirmedPromiseResolve === 'function') {
4426
+ _sessionConfirmedPromiseResolve();
4427
+ _sessionConfirmedPromiseResolve = null;
4428
+ runtimeState.sessionConfirmed = true;
4429
+ }
4407
4430
 
4408
4431
  return responseData;
4409
4432
  } catch (error) {
@@ -5254,8 +5277,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
5254
5277
  * NOTE:
5255
5278
  * We intentionally do NOT wrap window.fetch here, to avoid confusing
5256
5279
  * browser DevTools attribution for third‑party requests. All SDK
5257
- * fetch-based calls to backend.cryptique.io are already error-handled
5258
- * inside APIClient.send(), so fetch wrapping is redundant.
5280
+ * fetch-based calls to the SDK API backend (sdkapi.cryptique.io) are already
5281
+ * error-handled inside APIClient.send(), so fetch wrapping is redundant.
5259
5282
  */
5260
5283
  startNetworkTracking() {
5261
5284
  try {
@@ -5274,7 +5297,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
5274
5297
  const xhr = this;
5275
5298
 
5276
5299
  // Pass third-party XHR requests through completely untouched
5277
- if (!xhr._cryptiqueUrl || !xhr._cryptiqueUrl.includes('backend.cryptique.io')) {
5300
+ if (!xhr._cryptiqueUrl || !xhr._cryptiqueUrl.includes('sdkapi.cryptique.io')) {
5278
5301
  return originalXHRSend.apply(this, args);
5279
5302
  }
5280
5303
 
@@ -5884,6 +5907,13 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
5884
5907
  return;
5885
5908
  }
5886
5909
 
5910
+ // Wait for first track success so session exists on server before sending events (avoids 404)
5911
+ try {
5912
+ await _waitForSessionConfirmed();
5913
+ } catch (e) {
5914
+ return;
5915
+ }
5916
+
5887
5917
  // Get session from storage - it returns { id, userId, ... }
5888
5918
  const storedSession = StorageManager.loadSession();
5889
5919
  if (!storedSession || !storedSession.id) {
@@ -6021,6 +6051,13 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
6021
6051
  }
6022
6052
  }
6023
6053
 
6054
+ // Wait for first track success so session exists on server before sending events (avoids 404)
6055
+ try {
6056
+ await _waitForSessionConfirmed();
6057
+ } catch (e) {
6058
+ return;
6059
+ }
6060
+
6024
6061
  // Get session from storage - it returns { id, userId, ... }
6025
6062
  const storedSession = StorageManager.loadSession();
6026
6063
  if (!storedSession || !storedSession.id) {
@@ -6630,14 +6667,14 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
6630
6667
  * Track page view
6631
6668
  */
6632
6669
  trackPageView() {
6633
- // Delay page view tracking to ensure session is initialized
6670
+ // Small delay for ordering; trackAutoEvent waits for session confirmed internally
6634
6671
  setTimeout(() => {
6635
6672
  EventsManager.trackAutoEvent('page_view', {
6636
6673
  page_load_time: performance.now(),
6637
6674
  scroll_position: 0
6638
6675
  }).catch(err => {
6639
6676
  });
6640
- }, 1000); // Wait 1 second for session to be created
6677
+ }, 300);
6641
6678
  },
6642
6679
 
6643
6680
  /**
package/lib/esm/index.js CHANGED
@@ -156,8 +156,9 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
156
156
  timer: null, // Session tracking interval timer
157
157
  countryName: null, // Cached country name
158
158
  isInitialized: false, // Prevents API calls during initialization
159
+ sessionConfirmed: false, // True after first successful POST /api/sdk/track (session exists on server)
159
160
  eip6963Providers: [], // EIP-6963 discovered wallet providers
160
- reportedWalletAddress: null // Last wallet address reported to backend (prevents duplicate calls)
161
+ reportedWalletAddress: null // Last wallet address reported to backend (prevents duplicate calls)
161
162
  };
162
163
 
163
164
  // Ready promise - resolves when SDK is fully initialized (allows consumers to await init)
@@ -166,6 +167,12 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
166
167
  _readyPromiseResolve = resolve;
167
168
  });
168
169
 
170
+ // Session confirmed promise - resolves when first POST /api/sdk/track succeeds (so events can safely use sessionId)
171
+ let _sessionConfirmedPromiseResolve;
172
+ const _sessionConfirmedPromise = new Promise((resolve) => {
173
+ _sessionConfirmedPromiseResolve = resolve;
174
+ });
175
+
169
176
  // Helper to wait for SDK ready with timeout (avoids hanging if init fails)
170
177
  const _waitForReady = (timeoutMs = 5000) => {
171
178
  if (runtimeState.isInitialized) return Promise.resolve();
@@ -175,6 +182,15 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
175
182
  ]);
176
183
  };
177
184
 
185
+ // Helper to wait for first track success before sending events (avoids 404 Session not found)
186
+ const _waitForSessionConfirmed = (timeoutMs = 12000) => {
187
+ if (runtimeState.sessionConfirmed) return Promise.resolve();
188
+ return Promise.race([
189
+ _sessionConfirmedPromise,
190
+ new Promise((_, reject) => setTimeout(() => reject(new Error('Session confirm timeout')), timeoutMs))
191
+ ]);
192
+ };
193
+
178
194
  // Initialize EIP-6963 wallet provider detection (if enabled)
179
195
  {
180
196
  // Listen for providers being announced
@@ -4402,6 +4418,13 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
4402
4418
 
4403
4419
  throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(responseData)}`);
4404
4420
  }
4421
+
4422
+ // First successful TRACK creates session on server; allow events to send only after this
4423
+ if (response.ok && endpoint === CONFIG.API.TRACK && typeof _sessionConfirmedPromiseResolve === 'function') {
4424
+ _sessionConfirmedPromiseResolve();
4425
+ _sessionConfirmedPromiseResolve = null;
4426
+ runtimeState.sessionConfirmed = true;
4427
+ }
4405
4428
 
4406
4429
  return responseData;
4407
4430
  } catch (error) {
@@ -5252,8 +5275,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
5252
5275
  * NOTE:
5253
5276
  * We intentionally do NOT wrap window.fetch here, to avoid confusing
5254
5277
  * browser DevTools attribution for third‑party requests. All SDK
5255
- * fetch-based calls to backend.cryptique.io are already error-handled
5256
- * inside APIClient.send(), so fetch wrapping is redundant.
5278
+ * fetch-based calls to the SDK API backend (sdkapi.cryptique.io) are already
5279
+ * error-handled inside APIClient.send(), so fetch wrapping is redundant.
5257
5280
  */
5258
5281
  startNetworkTracking() {
5259
5282
  try {
@@ -5272,7 +5295,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
5272
5295
  const xhr = this;
5273
5296
 
5274
5297
  // Pass third-party XHR requests through completely untouched
5275
- if (!xhr._cryptiqueUrl || !xhr._cryptiqueUrl.includes('backend.cryptique.io')) {
5298
+ if (!xhr._cryptiqueUrl || !xhr._cryptiqueUrl.includes('sdkapi.cryptique.io')) {
5276
5299
  return originalXHRSend.apply(this, args);
5277
5300
  }
5278
5301
 
@@ -5882,6 +5905,13 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
5882
5905
  return;
5883
5906
  }
5884
5907
 
5908
+ // Wait for first track success so session exists on server before sending events (avoids 404)
5909
+ try {
5910
+ await _waitForSessionConfirmed();
5911
+ } catch (e) {
5912
+ return;
5913
+ }
5914
+
5885
5915
  // Get session from storage - it returns { id, userId, ... }
5886
5916
  const storedSession = StorageManager.loadSession();
5887
5917
  if (!storedSession || !storedSession.id) {
@@ -6019,6 +6049,13 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
6019
6049
  }
6020
6050
  }
6021
6051
 
6052
+ // Wait for first track success so session exists on server before sending events (avoids 404)
6053
+ try {
6054
+ await _waitForSessionConfirmed();
6055
+ } catch (e) {
6056
+ return;
6057
+ }
6058
+
6022
6059
  // Get session from storage - it returns { id, userId, ... }
6023
6060
  const storedSession = StorageManager.loadSession();
6024
6061
  if (!storedSession || !storedSession.id) {
@@ -6628,14 +6665,14 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
6628
6665
  * Track page view
6629
6666
  */
6630
6667
  trackPageView() {
6631
- // Delay page view tracking to ensure session is initialized
6668
+ // Small delay for ordering; trackAutoEvent waits for session confirmed internally
6632
6669
  setTimeout(() => {
6633
6670
  EventsManager.trackAutoEvent('page_view', {
6634
6671
  page_load_time: performance.now(),
6635
6672
  scroll_position: 0
6636
6673
  }).catch(err => {
6637
6674
  });
6638
- }, 1000); // Wait 1 second for session to be created
6675
+ }, 300);
6639
6676
  },
6640
6677
 
6641
6678
  /**
package/lib/umd/index.js CHANGED
@@ -162,8 +162,9 @@
162
162
  timer: null, // Session tracking interval timer
163
163
  countryName: null, // Cached country name
164
164
  isInitialized: false, // Prevents API calls during initialization
165
+ sessionConfirmed: false, // True after first successful POST /api/sdk/track (session exists on server)
165
166
  eip6963Providers: [], // EIP-6963 discovered wallet providers
166
- reportedWalletAddress: null // Last wallet address reported to backend (prevents duplicate calls)
167
+ reportedWalletAddress: null // Last wallet address reported to backend (prevents duplicate calls)
167
168
  };
168
169
 
169
170
  // Ready promise - resolves when SDK is fully initialized (allows consumers to await init)
@@ -172,6 +173,12 @@
172
173
  _readyPromiseResolve = resolve;
173
174
  });
174
175
 
176
+ // Session confirmed promise - resolves when first POST /api/sdk/track succeeds (so events can safely use sessionId)
177
+ let _sessionConfirmedPromiseResolve;
178
+ const _sessionConfirmedPromise = new Promise((resolve) => {
179
+ _sessionConfirmedPromiseResolve = resolve;
180
+ });
181
+
175
182
  // Helper to wait for SDK ready with timeout (avoids hanging if init fails)
176
183
  const _waitForReady = (timeoutMs = 5000) => {
177
184
  if (runtimeState.isInitialized) return Promise.resolve();
@@ -181,6 +188,15 @@
181
188
  ]);
182
189
  };
183
190
 
191
+ // Helper to wait for first track success before sending events (avoids 404 Session not found)
192
+ const _waitForSessionConfirmed = (timeoutMs = 12000) => {
193
+ if (runtimeState.sessionConfirmed) return Promise.resolve();
194
+ return Promise.race([
195
+ _sessionConfirmedPromise,
196
+ new Promise((_, reject) => setTimeout(() => reject(new Error('Session confirm timeout')), timeoutMs))
197
+ ]);
198
+ };
199
+
184
200
  // Initialize EIP-6963 wallet provider detection (if enabled)
185
201
  {
186
202
  // Listen for providers being announced
@@ -4408,6 +4424,13 @@
4408
4424
 
4409
4425
  throw new Error(`API request failed: ${response.status} ${response.statusText} - ${JSON.stringify(responseData)}`);
4410
4426
  }
4427
+
4428
+ // First successful TRACK creates session on server; allow events to send only after this
4429
+ if (response.ok && endpoint === CONFIG.API.TRACK && typeof _sessionConfirmedPromiseResolve === 'function') {
4430
+ _sessionConfirmedPromiseResolve();
4431
+ _sessionConfirmedPromiseResolve = null;
4432
+ runtimeState.sessionConfirmed = true;
4433
+ }
4411
4434
 
4412
4435
  return responseData;
4413
4436
  } catch (error) {
@@ -5258,8 +5281,8 @@
5258
5281
  * NOTE:
5259
5282
  * We intentionally do NOT wrap window.fetch here, to avoid confusing
5260
5283
  * browser DevTools attribution for third‑party requests. All SDK
5261
- * fetch-based calls to backend.cryptique.io are already error-handled
5262
- * inside APIClient.send(), so fetch wrapping is redundant.
5284
+ * fetch-based calls to the SDK API backend (sdkapi.cryptique.io) are already
5285
+ * error-handled inside APIClient.send(), so fetch wrapping is redundant.
5263
5286
  */
5264
5287
  startNetworkTracking() {
5265
5288
  try {
@@ -5278,7 +5301,7 @@
5278
5301
  const xhr = this;
5279
5302
 
5280
5303
  // Pass third-party XHR requests through completely untouched
5281
- if (!xhr._cryptiqueUrl || !xhr._cryptiqueUrl.includes('backend.cryptique.io')) {
5304
+ if (!xhr._cryptiqueUrl || !xhr._cryptiqueUrl.includes('sdkapi.cryptique.io')) {
5282
5305
  return originalXHRSend.apply(this, args);
5283
5306
  }
5284
5307
 
@@ -5888,6 +5911,13 @@
5888
5911
  return;
5889
5912
  }
5890
5913
 
5914
+ // Wait for first track success so session exists on server before sending events (avoids 404)
5915
+ try {
5916
+ await _waitForSessionConfirmed();
5917
+ } catch (e) {
5918
+ return;
5919
+ }
5920
+
5891
5921
  // Get session from storage - it returns { id, userId, ... }
5892
5922
  const storedSession = StorageManager.loadSession();
5893
5923
  if (!storedSession || !storedSession.id) {
@@ -6025,6 +6055,13 @@
6025
6055
  }
6026
6056
  }
6027
6057
 
6058
+ // Wait for first track success so session exists on server before sending events (avoids 404)
6059
+ try {
6060
+ await _waitForSessionConfirmed();
6061
+ } catch (e) {
6062
+ return;
6063
+ }
6064
+
6028
6065
  // Get session from storage - it returns { id, userId, ... }
6029
6066
  const storedSession = StorageManager.loadSession();
6030
6067
  if (!storedSession || !storedSession.id) {
@@ -6634,14 +6671,14 @@
6634
6671
  * Track page view
6635
6672
  */
6636
6673
  trackPageView() {
6637
- // Delay page view tracking to ensure session is initialized
6674
+ // Small delay for ordering; trackAutoEvent waits for session confirmed internally
6638
6675
  setTimeout(() => {
6639
6676
  EventsManager.trackAutoEvent('page_view', {
6640
6677
  page_load_time: performance.now(),
6641
6678
  scroll_position: 0
6642
6679
  }).catch(err => {
6643
6680
  });
6644
- }, 1000); // Wait 1 second for session to be created
6681
+ }, 300);
6645
6682
  },
6646
6683
 
6647
6684
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cryptique-sdk",
3
- "version": "1.2.10",
3
+ "version": "1.2.12",
4
4
  "type": "module",
5
5
  "description": "Cryptique Analytics SDK - Comprehensive web analytics and user tracking for modern web applications",
6
6
  "main": "lib/cjs/index.js",