cryptique-sdk 1.2.22 → 1.2.23

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
@@ -568,47 +568,6 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
568
568
  });
569
569
  },
570
570
 
571
- /**
572
- * Call backend to migrate all sessions from old userId to new UUID
573
- *
574
- * This ensures that when a user's localStorage userId is migrated from old format
575
- * to UUID v4, all their existing sessions in the database are also updated to use
576
- * the new UUID, preventing session splitting.
577
- *
578
- * @param {string} siteId - The site ID
579
- * @param {string} oldUserId - Old format userId (e.g., "usr_abc123xyz")
580
- * @param {string} newUserId - New UUID v4 userId
581
- * @returns {Promise<Object>} Migration result with success status and updated count
582
- */
583
- async migrateUserIdOnBackend(siteId, oldUserId, newUserId) {
584
- try {
585
- // Construct migration endpoint URL from track endpoint
586
- const migrationUrl = CONFIG.API.TRACK.replace('/track', '/migrate-user-id');
587
-
588
- const response = await _nativeFetch(migrationUrl, {
589
- method: 'POST',
590
- headers: {
591
- 'Content-Type': 'application/json'
592
- },
593
- body: JSON.stringify({
594
- siteId: siteId,
595
- oldUserId: oldUserId,
596
- newUserId: newUserId
597
- })
598
- });
599
-
600
- if (!response.ok) {
601
- const errorText = await response.text();
602
- throw new Error(`Migration failed: ${response.status} - ${errorText}`);
603
- }
604
-
605
- const result = await response.json();
606
- return result;
607
- } catch (error) {
608
- throw error;
609
- }
610
- },
611
-
612
571
  /**
613
572
  * Get or create persistent user ID
614
573
  *
@@ -621,11 +580,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
621
580
  * - Globally unique across all sites and teams
622
581
  * - Cryptographically secure
623
582
  * - No collisions possible
624
- *
625
- * MIGRATION:
626
- * - Automatically migrates old format ("usr_abc123xyz") to UUID v4
627
- * - Calls backend to migrate all existing sessions to new UUID
628
- * - Validates UUID format on retrieval
583
+ *
584
+ * Non-UUID values in storage are replaced with a new UUID v4.
629
585
  */
630
586
  getUserId() {
631
587
  try {
@@ -636,39 +592,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
636
592
  userId = this.generateUUIDv4();
637
593
  localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, userId);
638
594
  } else {
639
- // Validate format and migrate if needed
640
595
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
641
- const oldFormatRegex = /^usr_[a-z0-9]{9}$/;
642
- const oldTempFormatRegex = /^usr_temp_[a-z0-9]{9}$/;
643
-
644
- // Check if old format detected
645
- if (oldFormatRegex.test(userId) || oldTempFormatRegex.test(userId)) {
646
- const oldUserId = userId; // Store old before migration
647
- const newUserId = this.generateUUIDv4(); // Generate new UUID
648
-
649
-
650
- // CRITICAL: Store new UUID in localStorage IMMEDIATELY
651
- // This prevents multiple migrations if getUserId() is called again
652
- localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, newUserId);
653
-
654
- // Call backend to migrate all sessions (non-blocking)
655
- if (SITE_ID) {
656
- this.migrateUserIdOnBackend(SITE_ID, oldUserId, newUserId)
657
- .then((result) => {
658
- if (result.success) {
659
- } else {
660
- console.warn('⚠️ Migration failed, but continuing with new UUID');
661
- }
662
- })
663
- .catch((err) => {
664
- // UUID already stored in localStorage, migration can happen later
665
- });
666
- }
667
-
668
- // Return new UUID immediately (already stored in localStorage)
669
- userId = newUserId;
670
- } else if (!uuidRegex.test(userId)) {
671
- // Invalid format - regenerate
596
+ if (!uuidRegex.test(userId)) {
672
597
  userId = this.generateUUIDv4();
673
598
  localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, userId);
674
599
  }
@@ -2016,8 +1941,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
2016
1941
  initialize() {
2017
1942
  // Ensure all required fields exist (some may already be set)
2018
1943
  if (!sessionData.sessionId) sessionData.sessionId = null;
2019
- // Get current SITE_ID (may have been set via init() after SDK loaded)
2020
- const currentSiteId = SITE_ID || window.Cryptique?.siteId || window.__CRYPTIQUE_SITE_ID__ || null;
1944
+ // May have been set via init() after SDK loaded
1945
+ const currentSiteId = getCurrentSiteId();
2021
1946
  if (!sessionData.siteId) sessionData.siteId = currentSiteId;
2022
1947
  if (!sessionData.teamId) sessionData.teamId = null;
2023
1948
  if (!sessionData.userId) sessionData.userId = null;
@@ -4111,7 +4036,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
4111
4036
  utm_id: utmData.utm_id,
4112
4037
  session_id: sessionData.sessionId,
4113
4038
  wallet_address: sessionData.wallet_address,
4114
- siteId: SITE_ID
4039
+ siteId: getCurrentSiteId()
4115
4040
  };
4116
4041
 
4117
4042
  // Send PATCH request to update wallet address
@@ -6851,7 +6776,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
6851
6776
  return { success: false, error: 'No user ID' };
6852
6777
  }
6853
6778
 
6854
- if (!SITE_ID) {
6779
+ if (!getCurrentSiteId()) {
6855
6780
  console.error('❌ [Identity] Site ID not found');
6856
6781
  return { success: false, error: 'Site ID not found' };
6857
6782
  }
@@ -7634,7 +7559,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7634
7559
  const session = StorageManager.loadSession();
7635
7560
  const currentDistinctId = StorageManager.getDistinctId();
7636
7561
 
7637
- if (!session || !currentDistinctId || !SITE_ID) {
7562
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7638
7563
  console.error('❌ [People] No active session or distinct ID found');
7639
7564
  return { success: false, error: 'No active session' };
7640
7565
  }
@@ -7683,7 +7608,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7683
7608
  const session = StorageManager.loadSession();
7684
7609
  const currentDistinctId = StorageManager.getDistinctId();
7685
7610
 
7686
- if (!session || !currentDistinctId || !SITE_ID) {
7611
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7687
7612
  console.error('❌ [People] No active session or distinct ID found');
7688
7613
  return { success: false, error: 'No active session' };
7689
7614
  }
@@ -7732,7 +7657,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7732
7657
  const session = StorageManager.loadSession();
7733
7658
  const currentDistinctId = StorageManager.getDistinctId();
7734
7659
 
7735
- if (!session || !currentDistinctId || !SITE_ID) {
7660
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7736
7661
  console.error('❌ [People] No active session or distinct ID found');
7737
7662
  return { success: false, error: 'No active session' };
7738
7663
  }
@@ -7787,7 +7712,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7787
7712
  const session = StorageManager.loadSession();
7788
7713
  const currentDistinctId = StorageManager.getDistinctId();
7789
7714
 
7790
- if (!session || !currentDistinctId || !SITE_ID) {
7715
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7791
7716
  console.error('❌ [People] No active session or distinct ID found');
7792
7717
  return { success: false, error: 'No active session' };
7793
7718
  }
@@ -7843,7 +7768,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7843
7768
  const session = StorageManager.loadSession();
7844
7769
  const currentDistinctId = StorageManager.getDistinctId();
7845
7770
 
7846
- if (!session || !currentDistinctId || !SITE_ID) {
7771
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7847
7772
  console.error('❌ [People] No active session or distinct ID found');
7848
7773
  return { success: false, error: 'No active session' };
7849
7774
  }
@@ -7899,7 +7824,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7899
7824
  const session = StorageManager.loadSession();
7900
7825
  const currentDistinctId = StorageManager.getDistinctId();
7901
7826
 
7902
- if (!session || !currentDistinctId || !SITE_ID) {
7827
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7903
7828
  console.error('❌ [People] No active session or distinct ID found');
7904
7829
  return { success: false, error: 'No active session' };
7905
7830
  }
@@ -7955,7 +7880,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7955
7880
  const session = StorageManager.loadSession();
7956
7881
  const currentDistinctId = StorageManager.getDistinctId();
7957
7882
 
7958
- if (!session || !currentDistinctId || !SITE_ID) {
7883
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7959
7884
  console.error('❌ [People] No active session or distinct ID found');
7960
7885
  return { success: false, error: 'No active session' };
7961
7886
  }
@@ -8006,7 +7931,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8006
7931
  const session = StorageManager.loadSession();
8007
7932
  const currentDistinctId = StorageManager.getDistinctId();
8008
7933
 
8009
- if (!session || !currentDistinctId || !SITE_ID) {
7934
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8010
7935
  console.error('❌ [People] No active session or distinct ID found');
8011
7936
  return { success: false, error: 'No active session' };
8012
7937
  }
@@ -8050,7 +7975,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8050
7975
  const session = StorageManager.loadSession();
8051
7976
  const currentDistinctId = StorageManager.getDistinctId();
8052
7977
 
8053
- if (!session || !currentDistinctId || !SITE_ID) {
7978
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8054
7979
  console.error('❌ [People] No active session or distinct ID found');
8055
7980
  return { success: false, error: 'No active session' };
8056
7981
  }
@@ -8092,7 +8017,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8092
8017
  const session = StorageManager.loadSession();
8093
8018
  const currentDistinctId = StorageManager.getDistinctId();
8094
8019
 
8095
- if (!session || !currentDistinctId || !SITE_ID) {
8020
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8096
8021
  console.error('❌ [People] No active session or distinct ID found');
8097
8022
  return { success: false, error: 'No active session' };
8098
8023
  }
@@ -8259,7 +8184,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8259
8184
  try {
8260
8185
  const session = StorageManager.loadSession();
8261
8186
  const currentDistinctId = StorageManager.getDistinctId();
8262
- if (!session || !currentDistinctId || !SITE_ID) {
8187
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8263
8188
  console.error('❌ [Groups] No active session or distinct ID found');
8264
8189
  return { success: false, error: 'No active session' };
8265
8190
  }
package/lib/esm/index.js CHANGED
@@ -566,47 +566,6 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
566
566
  });
567
567
  },
568
568
 
569
- /**
570
- * Call backend to migrate all sessions from old userId to new UUID
571
- *
572
- * This ensures that when a user's localStorage userId is migrated from old format
573
- * to UUID v4, all their existing sessions in the database are also updated to use
574
- * the new UUID, preventing session splitting.
575
- *
576
- * @param {string} siteId - The site ID
577
- * @param {string} oldUserId - Old format userId (e.g., "usr_abc123xyz")
578
- * @param {string} newUserId - New UUID v4 userId
579
- * @returns {Promise<Object>} Migration result with success status and updated count
580
- */
581
- async migrateUserIdOnBackend(siteId, oldUserId, newUserId) {
582
- try {
583
- // Construct migration endpoint URL from track endpoint
584
- const migrationUrl = CONFIG.API.TRACK.replace('/track', '/migrate-user-id');
585
-
586
- const response = await _nativeFetch(migrationUrl, {
587
- method: 'POST',
588
- headers: {
589
- 'Content-Type': 'application/json'
590
- },
591
- body: JSON.stringify({
592
- siteId: siteId,
593
- oldUserId: oldUserId,
594
- newUserId: newUserId
595
- })
596
- });
597
-
598
- if (!response.ok) {
599
- const errorText = await response.text();
600
- throw new Error(`Migration failed: ${response.status} - ${errorText}`);
601
- }
602
-
603
- const result = await response.json();
604
- return result;
605
- } catch (error) {
606
- throw error;
607
- }
608
- },
609
-
610
569
  /**
611
570
  * Get or create persistent user ID
612
571
  *
@@ -619,11 +578,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
619
578
  * - Globally unique across all sites and teams
620
579
  * - Cryptographically secure
621
580
  * - No collisions possible
622
- *
623
- * MIGRATION:
624
- * - Automatically migrates old format ("usr_abc123xyz") to UUID v4
625
- * - Calls backend to migrate all existing sessions to new UUID
626
- * - Validates UUID format on retrieval
581
+ *
582
+ * Non-UUID values in storage are replaced with a new UUID v4.
627
583
  */
628
584
  getUserId() {
629
585
  try {
@@ -634,39 +590,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
634
590
  userId = this.generateUUIDv4();
635
591
  localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, userId);
636
592
  } else {
637
- // Validate format and migrate if needed
638
593
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
639
- const oldFormatRegex = /^usr_[a-z0-9]{9}$/;
640
- const oldTempFormatRegex = /^usr_temp_[a-z0-9]{9}$/;
641
-
642
- // Check if old format detected
643
- if (oldFormatRegex.test(userId) || oldTempFormatRegex.test(userId)) {
644
- const oldUserId = userId; // Store old before migration
645
- const newUserId = this.generateUUIDv4(); // Generate new UUID
646
-
647
-
648
- // CRITICAL: Store new UUID in localStorage IMMEDIATELY
649
- // This prevents multiple migrations if getUserId() is called again
650
- localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, newUserId);
651
-
652
- // Call backend to migrate all sessions (non-blocking)
653
- if (SITE_ID) {
654
- this.migrateUserIdOnBackend(SITE_ID, oldUserId, newUserId)
655
- .then((result) => {
656
- if (result.success) {
657
- } else {
658
- console.warn('⚠️ Migration failed, but continuing with new UUID');
659
- }
660
- })
661
- .catch((err) => {
662
- // UUID already stored in localStorage, migration can happen later
663
- });
664
- }
665
-
666
- // Return new UUID immediately (already stored in localStorage)
667
- userId = newUserId;
668
- } else if (!uuidRegex.test(userId)) {
669
- // Invalid format - regenerate
594
+ if (!uuidRegex.test(userId)) {
670
595
  userId = this.generateUUIDv4();
671
596
  localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, userId);
672
597
  }
@@ -2014,8 +1939,8 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
2014
1939
  initialize() {
2015
1940
  // Ensure all required fields exist (some may already be set)
2016
1941
  if (!sessionData.sessionId) sessionData.sessionId = null;
2017
- // Get current SITE_ID (may have been set via init() after SDK loaded)
2018
- const currentSiteId = SITE_ID || window.Cryptique?.siteId || window.__CRYPTIQUE_SITE_ID__ || null;
1942
+ // May have been set via init() after SDK loaded
1943
+ const currentSiteId = getCurrentSiteId();
2019
1944
  if (!sessionData.siteId) sessionData.siteId = currentSiteId;
2020
1945
  if (!sessionData.teamId) sessionData.teamId = null;
2021
1946
  if (!sessionData.userId) sessionData.userId = null;
@@ -4109,7 +4034,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
4109
4034
  utm_id: utmData.utm_id,
4110
4035
  session_id: sessionData.sessionId,
4111
4036
  wallet_address: sessionData.wallet_address,
4112
- siteId: SITE_ID
4037
+ siteId: getCurrentSiteId()
4113
4038
  };
4114
4039
 
4115
4040
  // Send PATCH request to update wallet address
@@ -6849,7 +6774,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
6849
6774
  return { success: false, error: 'No user ID' };
6850
6775
  }
6851
6776
 
6852
- if (!SITE_ID) {
6777
+ if (!getCurrentSiteId()) {
6853
6778
  console.error('❌ [Identity] Site ID not found');
6854
6779
  return { success: false, error: 'Site ID not found' };
6855
6780
  }
@@ -7632,7 +7557,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7632
7557
  const session = StorageManager.loadSession();
7633
7558
  const currentDistinctId = StorageManager.getDistinctId();
7634
7559
 
7635
- if (!session || !currentDistinctId || !SITE_ID) {
7560
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7636
7561
  console.error('❌ [People] No active session or distinct ID found');
7637
7562
  return { success: false, error: 'No active session' };
7638
7563
  }
@@ -7681,7 +7606,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7681
7606
  const session = StorageManager.loadSession();
7682
7607
  const currentDistinctId = StorageManager.getDistinctId();
7683
7608
 
7684
- if (!session || !currentDistinctId || !SITE_ID) {
7609
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7685
7610
  console.error('❌ [People] No active session or distinct ID found');
7686
7611
  return { success: false, error: 'No active session' };
7687
7612
  }
@@ -7730,7 +7655,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7730
7655
  const session = StorageManager.loadSession();
7731
7656
  const currentDistinctId = StorageManager.getDistinctId();
7732
7657
 
7733
- if (!session || !currentDistinctId || !SITE_ID) {
7658
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7734
7659
  console.error('❌ [People] No active session or distinct ID found');
7735
7660
  return { success: false, error: 'No active session' };
7736
7661
  }
@@ -7785,7 +7710,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7785
7710
  const session = StorageManager.loadSession();
7786
7711
  const currentDistinctId = StorageManager.getDistinctId();
7787
7712
 
7788
- if (!session || !currentDistinctId || !SITE_ID) {
7713
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7789
7714
  console.error('❌ [People] No active session or distinct ID found');
7790
7715
  return { success: false, error: 'No active session' };
7791
7716
  }
@@ -7841,7 +7766,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7841
7766
  const session = StorageManager.loadSession();
7842
7767
  const currentDistinctId = StorageManager.getDistinctId();
7843
7768
 
7844
- if (!session || !currentDistinctId || !SITE_ID) {
7769
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7845
7770
  console.error('❌ [People] No active session or distinct ID found');
7846
7771
  return { success: false, error: 'No active session' };
7847
7772
  }
@@ -7897,7 +7822,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7897
7822
  const session = StorageManager.loadSession();
7898
7823
  const currentDistinctId = StorageManager.getDistinctId();
7899
7824
 
7900
- if (!session || !currentDistinctId || !SITE_ID) {
7825
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7901
7826
  console.error('❌ [People] No active session or distinct ID found');
7902
7827
  return { success: false, error: 'No active session' };
7903
7828
  }
@@ -7953,7 +7878,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
7953
7878
  const session = StorageManager.loadSession();
7954
7879
  const currentDistinctId = StorageManager.getDistinctId();
7955
7880
 
7956
- if (!session || !currentDistinctId || !SITE_ID) {
7881
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7957
7882
  console.error('❌ [People] No active session or distinct ID found');
7958
7883
  return { success: false, error: 'No active session' };
7959
7884
  }
@@ -8004,7 +7929,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8004
7929
  const session = StorageManager.loadSession();
8005
7930
  const currentDistinctId = StorageManager.getDistinctId();
8006
7931
 
8007
- if (!session || !currentDistinctId || !SITE_ID) {
7932
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8008
7933
  console.error('❌ [People] No active session or distinct ID found');
8009
7934
  return { success: false, error: 'No active session' };
8010
7935
  }
@@ -8048,7 +7973,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8048
7973
  const session = StorageManager.loadSession();
8049
7974
  const currentDistinctId = StorageManager.getDistinctId();
8050
7975
 
8051
- if (!session || !currentDistinctId || !SITE_ID) {
7976
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8052
7977
  console.error('❌ [People] No active session or distinct ID found');
8053
7978
  return { success: false, error: 'No active session' };
8054
7979
  }
@@ -8090,7 +8015,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8090
8015
  const session = StorageManager.loadSession();
8091
8016
  const currentDistinctId = StorageManager.getDistinctId();
8092
8017
 
8093
- if (!session || !currentDistinctId || !SITE_ID) {
8018
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8094
8019
  console.error('❌ [People] No active session or distinct ID found');
8095
8020
  return { success: false, error: 'No active session' };
8096
8021
  }
@@ -8257,7 +8182,7 @@ if (window.Cryptique && window.Cryptique.initialized) ; else {
8257
8182
  try {
8258
8183
  const session = StorageManager.loadSession();
8259
8184
  const currentDistinctId = StorageManager.getDistinctId();
8260
- if (!session || !currentDistinctId || !SITE_ID) {
8185
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8261
8186
  console.error('❌ [Groups] No active session or distinct ID found');
8262
8187
  return { success: false, error: 'No active session' };
8263
8188
  }
package/lib/umd/index.js CHANGED
@@ -572,47 +572,6 @@
572
572
  });
573
573
  },
574
574
 
575
- /**
576
- * Call backend to migrate all sessions from old userId to new UUID
577
- *
578
- * This ensures that when a user's localStorage userId is migrated from old format
579
- * to UUID v4, all their existing sessions in the database are also updated to use
580
- * the new UUID, preventing session splitting.
581
- *
582
- * @param {string} siteId - The site ID
583
- * @param {string} oldUserId - Old format userId (e.g., "usr_abc123xyz")
584
- * @param {string} newUserId - New UUID v4 userId
585
- * @returns {Promise<Object>} Migration result with success status and updated count
586
- */
587
- async migrateUserIdOnBackend(siteId, oldUserId, newUserId) {
588
- try {
589
- // Construct migration endpoint URL from track endpoint
590
- const migrationUrl = CONFIG.API.TRACK.replace('/track', '/migrate-user-id');
591
-
592
- const response = await _nativeFetch(migrationUrl, {
593
- method: 'POST',
594
- headers: {
595
- 'Content-Type': 'application/json'
596
- },
597
- body: JSON.stringify({
598
- siteId: siteId,
599
- oldUserId: oldUserId,
600
- newUserId: newUserId
601
- })
602
- });
603
-
604
- if (!response.ok) {
605
- const errorText = await response.text();
606
- throw new Error(`Migration failed: ${response.status} - ${errorText}`);
607
- }
608
-
609
- const result = await response.json();
610
- return result;
611
- } catch (error) {
612
- throw error;
613
- }
614
- },
615
-
616
575
  /**
617
576
  * Get or create persistent user ID
618
577
  *
@@ -625,11 +584,8 @@
625
584
  * - Globally unique across all sites and teams
626
585
  * - Cryptographically secure
627
586
  * - No collisions possible
628
- *
629
- * MIGRATION:
630
- * - Automatically migrates old format ("usr_abc123xyz") to UUID v4
631
- * - Calls backend to migrate all existing sessions to new UUID
632
- * - Validates UUID format on retrieval
587
+ *
588
+ * Non-UUID values in storage are replaced with a new UUID v4.
633
589
  */
634
590
  getUserId() {
635
591
  try {
@@ -640,39 +596,8 @@
640
596
  userId = this.generateUUIDv4();
641
597
  localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, userId);
642
598
  } else {
643
- // Validate format and migrate if needed
644
599
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
645
- const oldFormatRegex = /^usr_[a-z0-9]{9}$/;
646
- const oldTempFormatRegex = /^usr_temp_[a-z0-9]{9}$/;
647
-
648
- // Check if old format detected
649
- if (oldFormatRegex.test(userId) || oldTempFormatRegex.test(userId)) {
650
- const oldUserId = userId; // Store old before migration
651
- const newUserId = this.generateUUIDv4(); // Generate new UUID
652
-
653
-
654
- // CRITICAL: Store new UUID in localStorage IMMEDIATELY
655
- // This prevents multiple migrations if getUserId() is called again
656
- localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, newUserId);
657
-
658
- // Call backend to migrate all sessions (non-blocking)
659
- if (SITE_ID) {
660
- this.migrateUserIdOnBackend(SITE_ID, oldUserId, newUserId)
661
- .then((result) => {
662
- if (result.success) {
663
- } else {
664
- console.warn('⚠️ Migration failed, but continuing with new UUID');
665
- }
666
- })
667
- .catch((err) => {
668
- // UUID already stored in localStorage, migration can happen later
669
- });
670
- }
671
-
672
- // Return new UUID immediately (already stored in localStorage)
673
- userId = newUserId;
674
- } else if (!uuidRegex.test(userId)) {
675
- // Invalid format - regenerate
600
+ if (!uuidRegex.test(userId)) {
676
601
  userId = this.generateUUIDv4();
677
602
  localStorage.setItem(CONFIG.STORAGE_KEYS.USER_ID, userId);
678
603
  }
@@ -2020,8 +1945,8 @@
2020
1945
  initialize() {
2021
1946
  // Ensure all required fields exist (some may already be set)
2022
1947
  if (!sessionData.sessionId) sessionData.sessionId = null;
2023
- // Get current SITE_ID (may have been set via init() after SDK loaded)
2024
- const currentSiteId = SITE_ID || window.Cryptique?.siteId || window.__CRYPTIQUE_SITE_ID__ || null;
1948
+ // May have been set via init() after SDK loaded
1949
+ const currentSiteId = getCurrentSiteId();
2025
1950
  if (!sessionData.siteId) sessionData.siteId = currentSiteId;
2026
1951
  if (!sessionData.teamId) sessionData.teamId = null;
2027
1952
  if (!sessionData.userId) sessionData.userId = null;
@@ -4115,7 +4040,7 @@
4115
4040
  utm_id: utmData.utm_id,
4116
4041
  session_id: sessionData.sessionId,
4117
4042
  wallet_address: sessionData.wallet_address,
4118
- siteId: SITE_ID
4043
+ siteId: getCurrentSiteId()
4119
4044
  };
4120
4045
 
4121
4046
  // Send PATCH request to update wallet address
@@ -6855,7 +6780,7 @@
6855
6780
  return { success: false, error: 'No user ID' };
6856
6781
  }
6857
6782
 
6858
- if (!SITE_ID) {
6783
+ if (!getCurrentSiteId()) {
6859
6784
  console.error('❌ [Identity] Site ID not found');
6860
6785
  return { success: false, error: 'Site ID not found' };
6861
6786
  }
@@ -7638,7 +7563,7 @@
7638
7563
  const session = StorageManager.loadSession();
7639
7564
  const currentDistinctId = StorageManager.getDistinctId();
7640
7565
 
7641
- if (!session || !currentDistinctId || !SITE_ID) {
7566
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7642
7567
  console.error('❌ [People] No active session or distinct ID found');
7643
7568
  return { success: false, error: 'No active session' };
7644
7569
  }
@@ -7687,7 +7612,7 @@
7687
7612
  const session = StorageManager.loadSession();
7688
7613
  const currentDistinctId = StorageManager.getDistinctId();
7689
7614
 
7690
- if (!session || !currentDistinctId || !SITE_ID) {
7615
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7691
7616
  console.error('❌ [People] No active session or distinct ID found');
7692
7617
  return { success: false, error: 'No active session' };
7693
7618
  }
@@ -7736,7 +7661,7 @@
7736
7661
  const session = StorageManager.loadSession();
7737
7662
  const currentDistinctId = StorageManager.getDistinctId();
7738
7663
 
7739
- if (!session || !currentDistinctId || !SITE_ID) {
7664
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7740
7665
  console.error('❌ [People] No active session or distinct ID found');
7741
7666
  return { success: false, error: 'No active session' };
7742
7667
  }
@@ -7791,7 +7716,7 @@
7791
7716
  const session = StorageManager.loadSession();
7792
7717
  const currentDistinctId = StorageManager.getDistinctId();
7793
7718
 
7794
- if (!session || !currentDistinctId || !SITE_ID) {
7719
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7795
7720
  console.error('❌ [People] No active session or distinct ID found');
7796
7721
  return { success: false, error: 'No active session' };
7797
7722
  }
@@ -7847,7 +7772,7 @@
7847
7772
  const session = StorageManager.loadSession();
7848
7773
  const currentDistinctId = StorageManager.getDistinctId();
7849
7774
 
7850
- if (!session || !currentDistinctId || !SITE_ID) {
7775
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7851
7776
  console.error('❌ [People] No active session or distinct ID found');
7852
7777
  return { success: false, error: 'No active session' };
7853
7778
  }
@@ -7903,7 +7828,7 @@
7903
7828
  const session = StorageManager.loadSession();
7904
7829
  const currentDistinctId = StorageManager.getDistinctId();
7905
7830
 
7906
- if (!session || !currentDistinctId || !SITE_ID) {
7831
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7907
7832
  console.error('❌ [People] No active session or distinct ID found');
7908
7833
  return { success: false, error: 'No active session' };
7909
7834
  }
@@ -7959,7 +7884,7 @@
7959
7884
  const session = StorageManager.loadSession();
7960
7885
  const currentDistinctId = StorageManager.getDistinctId();
7961
7886
 
7962
- if (!session || !currentDistinctId || !SITE_ID) {
7887
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
7963
7888
  console.error('❌ [People] No active session or distinct ID found');
7964
7889
  return { success: false, error: 'No active session' };
7965
7890
  }
@@ -8010,7 +7935,7 @@
8010
7935
  const session = StorageManager.loadSession();
8011
7936
  const currentDistinctId = StorageManager.getDistinctId();
8012
7937
 
8013
- if (!session || !currentDistinctId || !SITE_ID) {
7938
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8014
7939
  console.error('❌ [People] No active session or distinct ID found');
8015
7940
  return { success: false, error: 'No active session' };
8016
7941
  }
@@ -8054,7 +7979,7 @@
8054
7979
  const session = StorageManager.loadSession();
8055
7980
  const currentDistinctId = StorageManager.getDistinctId();
8056
7981
 
8057
- if (!session || !currentDistinctId || !SITE_ID) {
7982
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8058
7983
  console.error('❌ [People] No active session or distinct ID found');
8059
7984
  return { success: false, error: 'No active session' };
8060
7985
  }
@@ -8096,7 +8021,7 @@
8096
8021
  const session = StorageManager.loadSession();
8097
8022
  const currentDistinctId = StorageManager.getDistinctId();
8098
8023
 
8099
- if (!session || !currentDistinctId || !SITE_ID) {
8024
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8100
8025
  console.error('❌ [People] No active session or distinct ID found');
8101
8026
  return { success: false, error: 'No active session' };
8102
8027
  }
@@ -8263,7 +8188,7 @@
8263
8188
  try {
8264
8189
  const session = StorageManager.loadSession();
8265
8190
  const currentDistinctId = StorageManager.getDistinctId();
8266
- if (!session || !currentDistinctId || !SITE_ID) {
8191
+ if (!session || !currentDistinctId || !getCurrentSiteId()) {
8267
8192
  console.error('❌ [Groups] No active session or distinct ID found');
8268
8193
  return { success: false, error: 'No active session' };
8269
8194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cryptique-sdk",
3
- "version": "1.2.22",
3
+ "version": "1.2.23",
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",