@journium/react 1.0.4 → 1.0.6

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/index.cjs CHANGED
@@ -669,14 +669,46 @@ class BrowserIdentityManager {
669
669
  }
670
670
  }
671
671
 
672
+ class Logger {
673
+ static setDebug(enabled) {
674
+ this.isDebugEnabled = enabled;
675
+ }
676
+ static isDebug() {
677
+ return this.isDebugEnabled;
678
+ }
679
+ static log(...args) {
680
+ if (this.isDebugEnabled) {
681
+ console.log(...args);
682
+ }
683
+ }
684
+ static warn(...args) {
685
+ if (this.isDebugEnabled) {
686
+ console.warn(...args);
687
+ }
688
+ }
689
+ static error(...args) {
690
+ if (this.isDebugEnabled) {
691
+ console.error(...args);
692
+ }
693
+ }
694
+ static info(...args) {
695
+ if (this.isDebugEnabled) {
696
+ console.info(...args);
697
+ }
698
+ }
699
+ }
700
+ Logger.isDebugEnabled = false;
701
+
672
702
  class JourniumClient {
673
703
  constructor(config) {
704
+ var _a;
674
705
  this.queue = [];
675
706
  this.flushTimer = null;
676
707
  this.initialized = false;
677
708
  // Validate required configuration
678
709
  if (!config.publishableKey) {
679
- console.error('Journium: publishableKey is required but not provided. SDK will not function.');
710
+ Logger.setDebug(true);
711
+ Logger.error('Journium: publishableKey is required but not provided. SDK will not function.');
680
712
  return;
681
713
  }
682
714
  // Set default apiHost if not provided
@@ -698,6 +730,8 @@ class JourniumClient {
698
730
  if (this.config.options) {
699
731
  this.effectiveOptions = mergeOptions(defaultOptions, this.config.options);
700
732
  }
733
+ // Initialize Logger with debug setting
734
+ Logger.setDebug((_a = this.effectiveOptions.debug) !== null && _a !== void 0 ? _a : false);
701
735
  // Initialize identity manager
702
736
  this.identityManager = new BrowserIdentityManager(this.effectiveOptions.sessionTimeout, this.config.publishableKey);
703
737
  // Initialize synchronously with cached config, fetch fresh config in background
@@ -705,7 +739,6 @@ class JourniumClient {
705
739
  this.fetchRemoteOptionsAsync();
706
740
  }
707
741
  loadCachedOptions() {
708
- var _a;
709
742
  if (typeof window === 'undefined' || !window.localStorage) {
710
743
  return null;
711
744
  }
@@ -714,14 +747,11 @@ class JourniumClient {
714
747
  return cached ? JSON.parse(cached) : null;
715
748
  }
716
749
  catch (error) {
717
- if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
718
- console.warn('Journium: Failed to load cached config:', error);
719
- }
750
+ Logger.warn('Journium: Failed to load cached config:', error);
720
751
  return null;
721
752
  }
722
753
  }
723
754
  saveCachedOptions(options) {
724
- var _a;
725
755
  if (typeof window === 'undefined' || !window.localStorage) {
726
756
  return;
727
757
  }
@@ -729,9 +759,7 @@ class JourniumClient {
729
759
  window.localStorage.setItem(this.optionsStorageKey, JSON.stringify(options));
730
760
  }
731
761
  catch (error) {
732
- if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
733
- console.warn('Journium: Failed to save config to cache:', error);
734
- }
762
+ Logger.warn('Journium: Failed to save config to cache:', error);
735
763
  }
736
764
  }
737
765
  initializeSync() {
@@ -740,9 +768,7 @@ class JourniumClient {
740
768
  // Step 2: If no local options provided, use cached remote options
741
769
  if (!this.config.options && cachedRemoteOptions) {
742
770
  this.effectiveOptions = cachedRemoteOptions;
743
- if (this.effectiveOptions.debug) {
744
- console.log('Journium: Using cached remote options:', cachedRemoteOptions);
745
- }
771
+ Logger.log('Journium: Using cached remote options:', cachedRemoteOptions);
746
772
  }
747
773
  // Step 3: Mark as initialized immediately - no need to wait for remote fetch
748
774
  this.initialized = true;
@@ -750,9 +776,7 @@ class JourniumClient {
750
776
  if (this.effectiveOptions.flushInterval && this.effectiveOptions.flushInterval > 0) {
751
777
  this.startFlushTimer();
752
778
  }
753
- if (this.effectiveOptions.debug) {
754
- console.log('Journium: Client initialized with effective options:', this.effectiveOptions);
755
- }
779
+ Logger.log('Journium: Client initialized with effective options:', this.effectiveOptions);
756
780
  }
757
781
  async fetchRemoteOptionsAsync() {
758
782
  // Fetch fresh config in background
@@ -761,10 +785,9 @@ class JourniumClient {
761
785
  }
762
786
  }
763
787
  async fetchAndCacheRemoteOptions() {
788
+ var _a;
764
789
  try {
765
- if (this.effectiveOptions.debug) {
766
- console.log('Journium: Fetching remote configuration in background...');
767
- }
790
+ Logger.log('Journium: Fetching remote configuration in background...');
768
791
  const remoteOptionsResponse = await fetchRemoteOptions(this.config.apiHost, this.config.publishableKey);
769
792
  if (remoteOptionsResponse && remoteOptionsResponse.success) {
770
793
  // Save remote config to cache for next session
@@ -782,16 +805,14 @@ class JourniumClient {
782
805
  if (this.effectiveOptions.sessionTimeout) {
783
806
  this.identityManager.updateSessionTimeout(this.effectiveOptions.sessionTimeout);
784
807
  }
785
- if (this.effectiveOptions.debug) {
786
- console.log('Journium: Background remote options applied:', remoteOptionsResponse.config);
787
- console.log('Journium: New effective options:', this.effectiveOptions);
788
- }
808
+ Logger.log('Journium: Background remote options applied:', remoteOptionsResponse.config);
809
+ Logger.log('Journium: New effective options:', this.effectiveOptions);
810
+ // Update Logger debug setting with new options
811
+ Logger.setDebug((_a = this.effectiveOptions.debug) !== null && _a !== void 0 ? _a : false);
789
812
  }
790
813
  }
791
814
  catch (error) {
792
- if (this.effectiveOptions.debug) {
793
- console.warn('Journium: Background remote options fetch failed:', error);
794
- }
815
+ Logger.warn('Journium: Background remote options fetch failed:', error);
795
816
  }
796
817
  }
797
818
  startFlushTimer() {
@@ -820,24 +841,17 @@ class JourniumClient {
820
841
  if (!response.ok) {
821
842
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
822
843
  }
823
- if (this.effectiveOptions.debug) {
824
- console.log('Journium: Successfully sent events', events);
825
- }
844
+ Logger.log('Journium: Successfully sent events', events);
826
845
  }
827
846
  catch (error) {
828
- if (this.effectiveOptions.debug) {
829
- console.error('Journium: Failed to send events', error);
830
- }
847
+ Logger.error('Journium: Failed to send events', error);
831
848
  throw error;
832
849
  }
833
850
  }
834
851
  identify(distinctId, attributes = {}) {
835
- var _a;
836
852
  // Don't identify if SDK is not properly configured
837
853
  if (!this.config || !this.config.publishableKey || !this.initialized) {
838
- if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
839
- console.warn('Journium: identify() call rejected - SDK not ready');
840
- }
854
+ Logger.warn('Journium: identify() call rejected - SDK not ready');
841
855
  return;
842
856
  }
843
857
  // Call identify on identity manager to get previous distinct ID
@@ -848,32 +862,22 @@ class JourniumClient {
848
862
  $anon_distinct_id: previousDistinctId,
849
863
  };
850
864
  this.track('$identify', identifyProperties);
851
- if (this.effectiveOptions.debug) {
852
- console.log('Journium: User identified', { distinctId, attributes, previousDistinctId });
853
- }
865
+ Logger.log('Journium: User identified', { distinctId, attributes, previousDistinctId });
854
866
  }
855
867
  reset() {
856
- var _a;
857
868
  // Don't reset if SDK is not properly configured
858
869
  if (!this.config || !this.config.publishableKey || !this.initialized) {
859
- if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
860
- console.warn('Journium: reset() call rejected - SDK not ready');
861
- }
870
+ Logger.warn('Journium: reset() call rejected - SDK not ready');
862
871
  return;
863
872
  }
864
873
  // Reset identity in identity manager
865
874
  this.identityManager.reset();
866
- if (this.effectiveOptions.debug) {
867
- console.log('Journium: User identity reset');
868
- }
875
+ Logger.log('Journium: User identity reset');
869
876
  }
870
877
  track(event, properties = {}) {
871
- var _a;
872
878
  // Don't track if SDK is not properly configured
873
879
  if (!this.config || !this.config.publishableKey || !this.initialized) {
874
- if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
875
- console.warn('Journium: track() call rejected - SDK not ready');
876
- }
880
+ Logger.warn('Journium: track() call rejected - SDK not ready');
877
881
  return;
878
882
  }
879
883
  const identity = this.identityManager.getIdentity();
@@ -899,9 +903,7 @@ class JourniumClient {
899
903
  properties: eventProperties,
900
904
  };
901
905
  this.queue.push(journiumEvent);
902
- if (this.effectiveOptions.debug) {
903
- console.log('Journium: Event tracked', journiumEvent);
904
- }
906
+ Logger.log('Journium: Event tracked', journiumEvent);
905
907
  if (this.queue.length >= this.effectiveOptions.flushAt) {
906
908
  this.flush();
907
909
  }
@@ -1005,6 +1007,9 @@ class PageviewTracker {
1005
1007
  }
1006
1008
  }
1007
1009
 
1010
+ /**
1011
+ * AutocaptureTracker is responsible for tracking user interactions and capturing them as events.
1012
+ */
1008
1013
  class AutocaptureTracker {
1009
1014
  constructor(client, options = {}) {
1010
1015
  this.listeners = new Map();
@@ -1504,6 +1509,7 @@ exports.BrowserIdentityManager = BrowserIdentityManager;
1504
1509
  exports.JourniumAnalytics = JourniumAnalytics;
1505
1510
  exports.JourniumClient = JourniumClient;
1506
1511
  exports.JourniumProvider = JourniumProvider;
1512
+ exports.Logger = Logger;
1507
1513
  exports.PageviewTracker = PageviewTracker;
1508
1514
  exports.fetchRemoteOptions = fetchRemoteOptions;
1509
1515
  exports.generateId = generateId;