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