@journium/js 1.0.1 → 1.0.2

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.esm.js CHANGED
@@ -425,7 +425,7 @@ const isNode = () => {
425
425
  var _a;
426
426
  return typeof process !== 'undefined' && !!((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
427
427
  };
428
- const fetchRemoteConfig = async (apiHost, publishableKey, fetchFn) => {
428
+ const fetchRemoteOptions = async (apiHost, publishableKey, fetchFn) => {
429
429
  const endpoint = '/v1/configs';
430
430
  const url = `${apiHost}${endpoint}?ingestion_key=${encodeURIComponent(publishableKey)}`;
431
431
  try {
@@ -447,42 +447,43 @@ const fetchRemoteConfig = async (apiHost, publishableKey, fetchFn) => {
447
447
  },
448
448
  });
449
449
  if (!response.ok) {
450
- throw new Error(`Config fetch failed: ${response.status} ${response.statusText}`);
450
+ throw new Error(`Options fetch failed: ${response.status} ${response.statusText}`);
451
451
  }
452
452
  const data = await response.json();
453
453
  return data;
454
454
  }
455
455
  catch (error) {
456
- console.warn('Failed to fetch remote config:', error);
456
+ console.warn('Failed to fetch remote options:', error);
457
457
  return null;
458
458
  }
459
459
  };
460
- const mergeConfigs = (localConfig, remoteConfig) => {
461
- if (!remoteConfig && !localConfig) {
460
+ const mergeOptions = (localOptions, remoteOptions) => {
461
+ if (!remoteOptions && !localOptions) {
462
462
  return {};
463
463
  }
464
- if (!remoteConfig) {
465
- return localConfig;
464
+ if (!remoteOptions) {
465
+ return localOptions;
466
466
  }
467
- if (!localConfig) {
468
- return remoteConfig;
467
+ if (!localOptions) {
468
+ return remoteOptions;
469
469
  }
470
- // Deep merge local config into remote config
471
- // Local config takes precedence over remote config
472
- const merged = { ...remoteConfig };
470
+ // Deep merge local options into remote options
471
+ // Local options takes precedence over remote options
472
+ const merged = { ...remoteOptions };
473
473
  // Handle primitive values
474
- Object.keys(localConfig).forEach(key => {
475
- if (localConfig[key] !== undefined && localConfig[key] !== null) {
476
- if (typeof localConfig[key] === 'object' && !Array.isArray(localConfig[key])) {
477
- // Deep merge objects - local config overrides remote
474
+ Object.keys(localOptions).forEach(key => {
475
+ const localValue = localOptions[key];
476
+ if (localValue !== undefined && localValue !== null) {
477
+ if (typeof localValue === 'object' && !Array.isArray(localValue)) {
478
+ // Deep merge objects - local options overrides remote
478
479
  merged[key] = {
479
480
  ...(merged[key] || {}),
480
- ...localConfig[key]
481
+ ...localValue
481
482
  };
482
483
  }
483
484
  else {
484
- // Override primitive values and arrays with local config
485
- merged[key] = localConfig[key];
485
+ // Override primitive values and arrays with local options
486
+ merged[key] = localValue;
486
487
  }
487
488
  }
488
489
  });
@@ -679,113 +680,113 @@ class JourniumClient {
679
680
  ...config,
680
681
  apiHost: config.apiHost || 'https://events.journium.app'
681
682
  };
682
- // Generate storage key for config caching
683
- this.configStorageKey = `jrnm_${config.publishableKey}_config`;
683
+ // Generate storage key for options caching
684
+ this.optionsStorageKey = `jrnm_${config.publishableKey}_options`;
684
685
  // Generate default values
685
- const defaultConfig = {
686
+ const defaultOptions = {
686
687
  debug: false,
687
688
  flushAt: 20,
688
689
  flushInterval: 10000,
689
690
  sessionTimeout: 30 * 60 * 1000, // 30 minutes
690
691
  };
691
- // Initialize effective config with local config taking precedence over defaults
692
- this.effectiveConfig = { ...defaultConfig };
693
- if (this.config.config) {
694
- this.effectiveConfig = mergeConfigs(this.config.config, defaultConfig);
692
+ // Initialize effective options with local options taking precedence over defaults
693
+ this.effectiveOptions = { ...defaultOptions };
694
+ if (this.config.options) {
695
+ this.effectiveOptions = mergeOptions(defaultOptions, this.config.options);
695
696
  }
696
697
  // Initialize identity manager
697
- this.identityManager = new BrowserIdentityManager(this.effectiveConfig.sessionTimeout, this.config.publishableKey);
698
+ this.identityManager = new BrowserIdentityManager(this.effectiveOptions.sessionTimeout, this.config.publishableKey);
698
699
  // Initialize synchronously with cached config, fetch fresh config in background
699
700
  this.initializeSync();
700
- this.fetchRemoteConfigAsync();
701
+ this.fetchRemoteOptionsAsync();
701
702
  }
702
- loadCachedConfig() {
703
+ loadCachedOptions() {
703
704
  var _a;
704
705
  if (typeof window === 'undefined' || !window.localStorage) {
705
706
  return null;
706
707
  }
707
708
  try {
708
- const cached = window.localStorage.getItem(this.configStorageKey);
709
+ const cached = window.localStorage.getItem(this.optionsStorageKey);
709
710
  return cached ? JSON.parse(cached) : null;
710
711
  }
711
712
  catch (error) {
712
- if ((_a = this.effectiveConfig) === null || _a === void 0 ? void 0 : _a.debug) {
713
+ if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
713
714
  console.warn('Journium: Failed to load cached config:', error);
714
715
  }
715
716
  return null;
716
717
  }
717
718
  }
718
- saveCachedConfig(config) {
719
+ saveCachedOptions(options) {
719
720
  var _a;
720
721
  if (typeof window === 'undefined' || !window.localStorage) {
721
722
  return;
722
723
  }
723
724
  try {
724
- window.localStorage.setItem(this.configStorageKey, JSON.stringify(config));
725
+ window.localStorage.setItem(this.optionsStorageKey, JSON.stringify(options));
725
726
  }
726
727
  catch (error) {
727
- if ((_a = this.effectiveConfig) === null || _a === void 0 ? void 0 : _a.debug) {
728
+ if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
728
729
  console.warn('Journium: Failed to save config to cache:', error);
729
730
  }
730
731
  }
731
732
  }
732
733
  initializeSync() {
733
- // Step 1: Load cached remote config from localStorage (synchronous)
734
- const cachedRemoteConfig = this.loadCachedConfig();
735
- // Step 2: If no local config provided, use cached remote config
736
- if (!this.config.config && cachedRemoteConfig) {
737
- this.effectiveConfig = mergeConfigs(undefined, cachedRemoteConfig);
738
- if (this.effectiveConfig.debug) {
739
- console.log('Journium: Using cached remote configuration:', cachedRemoteConfig);
734
+ // Step 1: Load cached remote options from localStorage (synchronous)
735
+ const cachedRemoteOptions = this.loadCachedOptions();
736
+ // Step 2: If no local options provided, use cached remote options
737
+ if (!this.config.options && cachedRemoteOptions) {
738
+ this.effectiveOptions = cachedRemoteOptions;
739
+ if (this.effectiveOptions.debug) {
740
+ console.log('Journium: Using cached remote options:', cachedRemoteOptions);
740
741
  }
741
742
  }
742
743
  // Step 3: Mark as initialized immediately - no need to wait for remote fetch
743
744
  this.initialized = true;
744
745
  // Step 4: Start flush timer immediately
745
- if (this.effectiveConfig.flushInterval && this.effectiveConfig.flushInterval > 0) {
746
+ if (this.effectiveOptions.flushInterval && this.effectiveOptions.flushInterval > 0) {
746
747
  this.startFlushTimer();
747
748
  }
748
- if (this.effectiveConfig.debug) {
749
- console.log('Journium: Client initialized with effective config:', this.effectiveConfig);
749
+ if (this.effectiveOptions.debug) {
750
+ console.log('Journium: Client initialized with effective options:', this.effectiveOptions);
750
751
  }
751
752
  }
752
- async fetchRemoteConfigAsync() {
753
+ async fetchRemoteOptionsAsync() {
753
754
  // Fetch fresh config in background
754
755
  if (this.config.publishableKey) {
755
- await this.fetchAndCacheRemoteConfig();
756
+ await this.fetchAndCacheRemoteOptions();
756
757
  }
757
758
  }
758
- async fetchAndCacheRemoteConfig() {
759
+ async fetchAndCacheRemoteOptions() {
759
760
  try {
760
- if (this.effectiveConfig.debug) {
761
+ if (this.effectiveOptions.debug) {
761
762
  console.log('Journium: Fetching remote configuration in background...');
762
763
  }
763
- const remoteConfigResponse = await fetchRemoteConfig(this.config.apiHost, this.config.publishableKey);
764
- if (remoteConfigResponse && remoteConfigResponse.success) {
764
+ const remoteOptionsResponse = await fetchRemoteOptions(this.config.apiHost, this.config.publishableKey);
765
+ if (remoteOptionsResponse && remoteOptionsResponse.success) {
765
766
  // Save remote config to cache for next session
766
- this.saveCachedConfig(remoteConfigResponse.config);
767
- // Update effective config: local config (if provided) overrides fresh remote config
768
- if (!this.config.config) {
769
- // No local config provided, use fresh remote config
770
- this.effectiveConfig = mergeConfigs(undefined, remoteConfigResponse.config);
767
+ this.saveCachedOptions(remoteOptionsResponse.config);
768
+ // Update effective options: local options (if provided) overrides fresh remote options
769
+ if (!this.config.options) {
770
+ // No local options provided, use fresh remote options
771
+ this.effectiveOptions = remoteOptionsResponse.config;
771
772
  }
772
773
  else {
773
- // Local config provided, merge it over fresh remote config
774
- this.effectiveConfig = mergeConfigs(this.config.config, remoteConfigResponse.config);
774
+ // Local options provided, merge it over fresh remote options
775
+ this.effectiveOptions = mergeOptions(remoteOptionsResponse.config, this.config.options);
775
776
  }
776
- // Update session timeout if provided in fresh effective config
777
- if (this.effectiveConfig.sessionTimeout) {
778
- this.identityManager.updateSessionTimeout(this.effectiveConfig.sessionTimeout);
777
+ // Update session timeout if provided in fresh effective options
778
+ if (this.effectiveOptions.sessionTimeout) {
779
+ this.identityManager.updateSessionTimeout(this.effectiveOptions.sessionTimeout);
779
780
  }
780
- if (this.effectiveConfig.debug) {
781
- console.log('Journium: Background remote configuration applied:', remoteConfigResponse.config);
782
- console.log('Journium: New effective configuration:', this.effectiveConfig);
781
+ if (this.effectiveOptions.debug) {
782
+ console.log('Journium: Background remote options applied:', remoteOptionsResponse.config);
783
+ console.log('Journium: New effective options:', this.effectiveOptions);
783
784
  }
784
785
  }
785
786
  }
786
787
  catch (error) {
787
- if (this.effectiveConfig.debug) {
788
- console.warn('Journium: Background remote config fetch failed:', error);
788
+ if (this.effectiveOptions.debug) {
789
+ console.warn('Journium: Background remote options fetch failed:', error);
789
790
  }
790
791
  }
791
792
  }
@@ -796,7 +797,7 @@ class JourniumClient {
796
797
  // Use universal setInterval (works in both browser and Node.js)
797
798
  this.flushTimer = setInterval(() => {
798
799
  this.flush();
799
- }, this.effectiveConfig.flushInterval);
800
+ }, this.effectiveOptions.flushInterval);
800
801
  }
801
802
  async sendEvents(events) {
802
803
  if (!events.length)
@@ -815,12 +816,12 @@ class JourniumClient {
815
816
  if (!response.ok) {
816
817
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
817
818
  }
818
- if (this.effectiveConfig.debug) {
819
+ if (this.effectiveOptions.debug) {
819
820
  console.log('Journium: Successfully sent events', events);
820
821
  }
821
822
  }
822
823
  catch (error) {
823
- if (this.effectiveConfig.debug) {
824
+ if (this.effectiveOptions.debug) {
824
825
  console.error('Journium: Failed to send events', error);
825
826
  }
826
827
  throw error;
@@ -830,7 +831,7 @@ class JourniumClient {
830
831
  var _a;
831
832
  // Don't identify if SDK is not properly configured
832
833
  if (!this.config || !this.config.publishableKey || !this.initialized) {
833
- if ((_a = this.effectiveConfig) === null || _a === void 0 ? void 0 : _a.debug) {
834
+ if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
834
835
  console.warn('Journium: identify() call rejected - SDK not ready');
835
836
  }
836
837
  return;
@@ -843,7 +844,7 @@ class JourniumClient {
843
844
  $anon_distinct_id: previousDistinctId,
844
845
  };
845
846
  this.track('$identify', identifyProperties);
846
- if (this.effectiveConfig.debug) {
847
+ if (this.effectiveOptions.debug) {
847
848
  console.log('Journium: User identified', { distinctId, attributes, previousDistinctId });
848
849
  }
849
850
  }
@@ -851,14 +852,14 @@ class JourniumClient {
851
852
  var _a;
852
853
  // Don't reset if SDK is not properly configured
853
854
  if (!this.config || !this.config.publishableKey || !this.initialized) {
854
- if ((_a = this.effectiveConfig) === null || _a === void 0 ? void 0 : _a.debug) {
855
+ if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
855
856
  console.warn('Journium: reset() call rejected - SDK not ready');
856
857
  }
857
858
  return;
858
859
  }
859
860
  // Reset identity in identity manager
860
861
  this.identityManager.reset();
861
- if (this.effectiveConfig.debug) {
862
+ if (this.effectiveOptions.debug) {
862
863
  console.log('Journium: User identity reset');
863
864
  }
864
865
  }
@@ -866,7 +867,7 @@ class JourniumClient {
866
867
  var _a;
867
868
  // Don't track if SDK is not properly configured
868
869
  if (!this.config || !this.config.publishableKey || !this.initialized) {
869
- if ((_a = this.effectiveConfig) === null || _a === void 0 ? void 0 : _a.debug) {
870
+ if ((_a = this.effectiveOptions) === null || _a === void 0 ? void 0 : _a.debug) {
870
871
  console.warn('Journium: track() call rejected - SDK not ready');
871
872
  }
872
873
  return;
@@ -894,10 +895,10 @@ class JourniumClient {
894
895
  properties: eventProperties,
895
896
  };
896
897
  this.queue.push(journiumEvent);
897
- if (this.effectiveConfig.debug) {
898
+ if (this.effectiveOptions.debug) {
898
899
  console.log('Journium: Event tracked', journiumEvent);
899
900
  }
900
- if (this.queue.length >= this.effectiveConfig.flushAt) {
901
+ if (this.queue.length >= this.effectiveOptions.flushAt) {
901
902
  this.flush();
902
903
  }
903
904
  }
@@ -925,6 +926,9 @@ class JourniumClient {
925
926
  }
926
927
  this.flush();
927
928
  }
929
+ getEffectiveOptions() {
930
+ return this.effectiveOptions;
931
+ }
928
932
  }
929
933
 
930
934
  class PageviewTracker {
@@ -990,11 +994,11 @@ class PageviewTracker {
990
994
  }
991
995
 
992
996
  class AutocaptureTracker {
993
- constructor(client, config = {}) {
997
+ constructor(client, options = {}) {
994
998
  this.listeners = new Map();
995
999
  this.isActive = false;
996
1000
  this.client = client;
997
- this.config = {
1001
+ this.options = {
998
1002
  captureClicks: true,
999
1003
  captureFormSubmits: true,
1000
1004
  captureFormChanges: true,
@@ -1002,7 +1006,7 @@ class AutocaptureTracker {
1002
1006
  ignoreClasses: ['journium-ignore'],
1003
1007
  ignoreElements: ['script', 'style', 'noscript'],
1004
1008
  captureContentText: true,
1005
- ...config,
1009
+ ...options,
1006
1010
  };
1007
1011
  }
1008
1012
  start() {
@@ -1010,16 +1014,16 @@ class AutocaptureTracker {
1010
1014
  return;
1011
1015
  }
1012
1016
  this.isActive = true;
1013
- if (this.config.captureClicks) {
1017
+ if (this.options.captureClicks) {
1014
1018
  this.addClickListener();
1015
1019
  }
1016
- if (this.config.captureFormSubmits) {
1020
+ if (this.options.captureFormSubmits) {
1017
1021
  this.addFormSubmitListener();
1018
1022
  }
1019
- if (this.config.captureFormChanges) {
1023
+ if (this.options.captureFormChanges) {
1020
1024
  this.addFormChangeListener();
1021
1025
  }
1022
- if (this.config.captureTextSelection) {
1026
+ if (this.options.captureTextSelection) {
1023
1027
  this.addTextSelectionListener();
1024
1028
  }
1025
1029
  }
@@ -1103,17 +1107,17 @@ class AutocaptureTracker {
1103
1107
  return true;
1104
1108
  }
1105
1109
  // Check if element should be ignored by tag name
1106
- if ((_a = this.config.ignoreElements) === null || _a === void 0 ? void 0 : _a.includes(element.tagName.toLowerCase())) {
1110
+ if ((_a = this.options.ignoreElements) === null || _a === void 0 ? void 0 : _a.includes(element.tagName.toLowerCase())) {
1107
1111
  return true;
1108
1112
  }
1109
1113
  // Check if element has ignore classes
1110
- if ((_b = this.config.ignoreClasses) === null || _b === void 0 ? void 0 : _b.some(cls => element.classList.contains(cls))) {
1114
+ if ((_b = this.options.ignoreClasses) === null || _b === void 0 ? void 0 : _b.some(cls => element.classList.contains(cls))) {
1111
1115
  return true;
1112
1116
  }
1113
1117
  // Check parent elements for ignore classes
1114
1118
  let parent = element.parentElement;
1115
1119
  while (parent) {
1116
- if ((_c = this.config.ignoreClasses) === null || _c === void 0 ? void 0 : _c.some(cls => parent.classList.contains(cls))) {
1120
+ if ((_c = this.options.ignoreClasses) === null || _c === void 0 ? void 0 : _c.some(cls => parent.classList.contains(cls))) {
1117
1121
  return true;
1118
1122
  }
1119
1123
  parent = parent.parentElement;
@@ -1145,7 +1149,7 @@ class AutocaptureTracker {
1145
1149
  }
1146
1150
  });
1147
1151
  // Element content
1148
- if (this.config.captureContentText) {
1152
+ if (this.options.captureContentText) {
1149
1153
  const text = this.getElementText(element);
1150
1154
  if (text) {
1151
1155
  properties.$element_text = text.substring(0, 200); // Limit text length
@@ -1332,18 +1336,18 @@ class AutocaptureTracker {
1332
1336
  }
1333
1337
  }
1334
1338
 
1335
- class Journium {
1339
+ class JourniumAnalytics {
1336
1340
  constructor(config) {
1337
1341
  var _a, _b;
1338
1342
  this.config = config;
1339
1343
  this.client = new JourniumClient(config);
1340
1344
  this.pageviewTracker = new PageviewTracker(this.client);
1341
- const autocaptureConfig = this.resolveAutocaptureConfig((_a = config.config) === null || _a === void 0 ? void 0 : _a.autocapture);
1342
- this.autocaptureTracker = new AutocaptureTracker(this.client, autocaptureConfig);
1345
+ const autocaptureOptions = this.resolveAutocaptureOptions((_a = config.options) === null || _a === void 0 ? void 0 : _a.autocapture);
1346
+ this.autocaptureTracker = new AutocaptureTracker(this.client, autocaptureOptions);
1343
1347
  // Store resolved autocapture state for startAutocapture method
1344
- this.autocaptureEnabled = ((_b = config.config) === null || _b === void 0 ? void 0 : _b.autocapture) !== false;
1348
+ this.autocaptureEnabled = ((_b = config.options) === null || _b === void 0 ? void 0 : _b.autocapture) !== false;
1345
1349
  }
1346
- resolveAutocaptureConfig(autocapture) {
1350
+ resolveAutocaptureOptions(autocapture) {
1347
1351
  if (autocapture === false) {
1348
1352
  return {
1349
1353
  captureClicks: false,
@@ -1370,7 +1374,12 @@ class Journium {
1370
1374
  this.pageviewTracker.capturePageview(properties);
1371
1375
  }
1372
1376
  startAutocapture() {
1373
- this.pageviewTracker.startAutocapture();
1377
+ // Check if automatic pageview tracking is enabled (defaults to true)
1378
+ const effectiveOptions = this.client.getEffectiveOptions();
1379
+ const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
1380
+ if (autoTrackPageviews) {
1381
+ this.pageviewTracker.startAutocapture();
1382
+ }
1374
1383
  if (this.autocaptureEnabled) {
1375
1384
  this.autocaptureTracker.start();
1376
1385
  }
@@ -1382,6 +1391,9 @@ class Journium {
1382
1391
  async flush() {
1383
1392
  return this.client.flush();
1384
1393
  }
1394
+ getEffectiveOptions() {
1395
+ return this.client.getEffectiveOptions();
1396
+ }
1385
1397
  destroy() {
1386
1398
  this.pageviewTracker.stopAutocapture();
1387
1399
  this.autocaptureTracker.stop();
@@ -1389,8 +1401,8 @@ class Journium {
1389
1401
  }
1390
1402
  }
1391
1403
  const init = (config) => {
1392
- return new Journium(config);
1404
+ return new JourniumAnalytics(config);
1393
1405
  };
1394
1406
 
1395
- export { AutocaptureTracker, BrowserIdentityManager, Journium, JourniumClient, PageviewTracker, fetchRemoteConfig, generateId, generateUuidv7, getCurrentTimestamp, getCurrentUrl, getPageTitle, getReferrer, init, isBrowser, isNode, mergeConfigs };
1407
+ export { AutocaptureTracker, BrowserIdentityManager, JourniumAnalytics, JourniumClient, PageviewTracker, fetchRemoteOptions, generateId, generateUuidv7, getCurrentTimestamp, getCurrentUrl, getPageTitle, getReferrer, init, isBrowser, isNode, mergeOptions };
1396
1408
  //# sourceMappingURL=index.esm.js.map