@journium/react 1.0.6 → 1.0.7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAkD,SAAS,EAAE,MAAM,OAAO,CAAC;AACzF,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtE,UAAU,oBAAoB;IAC5B,SAAS,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACpC,MAAM,EAAE,cAAc,GAAG,IAAI,CAAC;IAC9B,gBAAgB,EAAE,oBAAoB,GAAG,IAAI,CAAC;CAC/C;AAID,UAAU,qBAAqB;IAC7B,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,qBAAqB,CAiC5D,CAAC;AAEF,eAAO,MAAM,WAAW,QAAO,oBAM9B,CAAC"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAkD,SAAS,EAAE,MAAM,OAAO,CAAC;AACzF,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtE,UAAU,oBAAoB;IAC5B,SAAS,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACpC,MAAM,EAAE,cAAc,GAAG,IAAI,CAAC;IAC9B,gBAAgB,EAAE,oBAAoB,GAAG,IAAI,CAAC;CAC/C;AAID,UAAU,qBAAqB;IAC7B,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,qBAAqB,CA6C5D,CAAC;AAEF,eAAO,MAAM,WAAW,QAAO,oBAM9B,CAAC"}
package/dist/index.cjs CHANGED
@@ -705,6 +705,7 @@ class JourniumClient {
705
705
  this.queue = [];
706
706
  this.flushTimer = null;
707
707
  this.initialized = false;
708
+ this.optionsChangeCallbacks = new Set();
708
709
  // Validate required configuration
709
710
  if (!config.publishableKey) {
710
711
  Logger.setDebug(true);
@@ -765,11 +766,21 @@ class JourniumClient {
765
766
  initializeSync() {
766
767
  // Step 1: Load cached remote options from localStorage (synchronous)
767
768
  const cachedRemoteOptions = this.loadCachedOptions();
768
- // Step 2: If no local options provided, use cached remote options
769
- if (!this.config.options && cachedRemoteOptions) {
770
- this.effectiveOptions = cachedRemoteOptions;
771
- Logger.log('Journium: Using cached remote options:', cachedRemoteOptions);
769
+ // Step 2: Merge cached remote options with local options (if cached options exist)
770
+ // Local options take precedence over cached remote options
771
+ if (cachedRemoteOptions) {
772
+ if (this.config.options) {
773
+ // Merge: local options override cached remote options
774
+ this.effectiveOptions = mergeOptions(cachedRemoteOptions, this.config.options);
775
+ Logger.log('Journium: Using cached remote options merged with local options:', this.effectiveOptions);
776
+ }
777
+ else {
778
+ // No local options, use cached remote options as-is
779
+ this.effectiveOptions = cachedRemoteOptions;
780
+ Logger.log('Journium: Using cached remote options:', cachedRemoteOptions);
781
+ }
772
782
  }
783
+ // If no cached options, effectiveOptions already has defaults merged with local options from constructor
773
784
  // Step 3: Mark as initialized immediately - no need to wait for remote fetch
774
785
  this.initialized = true;
775
786
  // Step 4: Start flush timer immediately
@@ -809,12 +820,34 @@ class JourniumClient {
809
820
  Logger.log('Journium: New effective options:', this.effectiveOptions);
810
821
  // Update Logger debug setting with new options
811
822
  Logger.setDebug((_a = this.effectiveOptions.debug) !== null && _a !== void 0 ? _a : false);
823
+ // Notify all registered callbacks about the options change
824
+ this.notifyOptionsChange();
812
825
  }
813
826
  }
814
827
  catch (error) {
815
828
  Logger.warn('Journium: Background remote options fetch failed:', error);
816
829
  }
817
830
  }
831
+ /**
832
+ * Register a callback to be notified when effective options change (e.g., when remote options are fetched)
833
+ */
834
+ onOptionsChange(callback) {
835
+ this.optionsChangeCallbacks.add(callback);
836
+ // Return unsubscribe function
837
+ return () => {
838
+ this.optionsChangeCallbacks.delete(callback);
839
+ };
840
+ }
841
+ notifyOptionsChange() {
842
+ this.optionsChangeCallbacks.forEach(callback => {
843
+ try {
844
+ callback(this.effectiveOptions);
845
+ }
846
+ catch (error) {
847
+ Logger.warn('Journium: Error in options change callback:', error);
848
+ }
849
+ });
850
+ }
818
851
  startFlushTimer() {
819
852
  if (this.flushTimer) {
820
853
  clearInterval(this.flushTimer);
@@ -1026,6 +1059,31 @@ class AutocaptureTracker {
1026
1059
  ...options,
1027
1060
  };
1028
1061
  }
1062
+ /**
1063
+ * Update autocapture options and restart if currently active
1064
+ */
1065
+ updateOptions(options) {
1066
+ const wasActive = this.isActive;
1067
+ // Stop if currently active
1068
+ if (wasActive) {
1069
+ this.stop();
1070
+ }
1071
+ // Update options
1072
+ this.options = {
1073
+ captureClicks: true,
1074
+ captureFormSubmits: true,
1075
+ captureFormChanges: true,
1076
+ captureTextSelection: false,
1077
+ ignoreClasses: ['journium-ignore'],
1078
+ ignoreElements: ['script', 'style', 'noscript'],
1079
+ captureContentText: true,
1080
+ ...options,
1081
+ };
1082
+ // Restart if it was active before
1083
+ if (wasActive) {
1084
+ this.start();
1085
+ }
1086
+ }
1029
1087
  start() {
1030
1088
  if (!isBrowser() || this.isActive) {
1031
1089
  return;
@@ -1355,14 +1413,19 @@ class AutocaptureTracker {
1355
1413
 
1356
1414
  class JourniumAnalytics {
1357
1415
  constructor(config) {
1358
- var _a, _b;
1416
+ this.autocaptureStarted = false;
1359
1417
  this.config = config;
1360
1418
  this.client = new JourniumClient(config);
1361
1419
  this.pageviewTracker = new PageviewTracker(this.client);
1362
- const autocaptureOptions = this.resolveAutocaptureOptions((_a = config.options) === null || _a === void 0 ? void 0 : _a.autocapture);
1363
- this.autocaptureTracker = new AutocaptureTracker(this.client, autocaptureOptions);
1364
- // Store resolved autocapture state for startAutocapture method
1365
- this.autocaptureEnabled = ((_b = config.options) === null || _b === void 0 ? void 0 : _b.autocapture) !== false;
1420
+ // Initialize autocapture tracker with effective options (may include cached remote options)
1421
+ // This ensures we use the correct initial state even if cached remote options exist
1422
+ const initialEffectiveOptions = this.client.getEffectiveOptions();
1423
+ const initialAutocaptureOptions = this.resolveAutocaptureOptions(initialEffectiveOptions.autocapture);
1424
+ this.autocaptureTracker = new AutocaptureTracker(this.client, initialAutocaptureOptions);
1425
+ // Listen for options changes (e.g., when fresh remote options are fetched)
1426
+ this.unsubscribeOptionsChange = this.client.onOptionsChange((effectiveOptions) => {
1427
+ this.handleOptionsChange(effectiveOptions);
1428
+ });
1366
1429
  }
1367
1430
  resolveAutocaptureOptions(autocapture) {
1368
1431
  if (autocapture === false) {
@@ -1391,19 +1454,51 @@ class JourniumAnalytics {
1391
1454
  this.pageviewTracker.capturePageview(properties);
1392
1455
  }
1393
1456
  startAutocapture() {
1394
- // Check if automatic pageview tracking is enabled (defaults to true)
1457
+ // Always check effective options (which may include remote options)
1395
1458
  const effectiveOptions = this.client.getEffectiveOptions();
1396
1459
  const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
1460
+ const autocaptureEnabled = effectiveOptions.autocapture !== false;
1461
+ // Update autocapture tracker options if they've changed
1462
+ const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
1463
+ this.autocaptureTracker.updateOptions(autocaptureOptions);
1397
1464
  if (autoTrackPageviews) {
1398
1465
  this.pageviewTracker.startAutocapture();
1399
1466
  }
1400
- if (this.autocaptureEnabled) {
1467
+ if (autocaptureEnabled) {
1401
1468
  this.autocaptureTracker.start();
1402
1469
  }
1470
+ this.autocaptureStarted = true;
1403
1471
  }
1404
1472
  stopAutocapture() {
1405
1473
  this.pageviewTracker.stopAutocapture();
1406
1474
  this.autocaptureTracker.stop();
1475
+ this.autocaptureStarted = false;
1476
+ }
1477
+ /**
1478
+ * Handle effective options change (e.g., when remote options are fetched)
1479
+ */
1480
+ handleOptionsChange(effectiveOptions) {
1481
+ // If autocapture was already started, re-evaluate with new options
1482
+ if (this.autocaptureStarted) {
1483
+ // Stop current autocapture
1484
+ this.pageviewTracker.stopAutocapture();
1485
+ this.autocaptureTracker.stop();
1486
+ this.autocaptureStarted = false;
1487
+ // Re-evaluate if autocapture should be enabled with new options
1488
+ const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
1489
+ const autocaptureEnabled = effectiveOptions.autocapture !== false;
1490
+ // Update autocapture tracker options
1491
+ const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
1492
+ this.autocaptureTracker.updateOptions(autocaptureOptions);
1493
+ // Restart only if still enabled
1494
+ if (autoTrackPageviews) {
1495
+ this.pageviewTracker.startAutocapture();
1496
+ }
1497
+ if (autocaptureEnabled) {
1498
+ this.autocaptureTracker.start();
1499
+ }
1500
+ this.autocaptureStarted = autoTrackPageviews || autocaptureEnabled;
1501
+ }
1407
1502
  }
1408
1503
  async flush() {
1409
1504
  return this.client.flush();
@@ -1411,9 +1506,18 @@ class JourniumAnalytics {
1411
1506
  getEffectiveOptions() {
1412
1507
  return this.client.getEffectiveOptions();
1413
1508
  }
1509
+ /**
1510
+ * Register a callback to be notified when effective options change
1511
+ */
1512
+ onOptionsChange(callback) {
1513
+ return this.client.onOptionsChange(callback);
1514
+ }
1414
1515
  destroy() {
1415
1516
  this.pageviewTracker.stopAutocapture();
1416
1517
  this.autocaptureTracker.stop();
1518
+ if (this.unsubscribeOptionsChange) {
1519
+ this.unsubscribeOptionsChange();
1520
+ }
1417
1521
  this.client.destroy();
1418
1522
  }
1419
1523
  }
@@ -1427,15 +1531,25 @@ const JourniumProvider = ({ children, config, }) => {
1427
1531
  const [effectiveOptions, setEffectiveOptions] = React.useState(null);
1428
1532
  React.useEffect(() => {
1429
1533
  const analyticsInstance = new JourniumAnalytics(config);
1430
- // Get effective options and check if autocapture is enabled
1431
- const effective = analyticsInstance.getEffectiveOptions();
1432
- setEffectiveOptions(effective);
1433
- const autocaptureEnabled = effective.autocapture !== false;
1534
+ // Get initial effective options (may include cached remote options)
1535
+ const initialEffective = analyticsInstance.getEffectiveOptions();
1536
+ setEffectiveOptions(initialEffective);
1537
+ // Check if autocapture should be enabled based on initial effective options
1538
+ const autocaptureEnabled = initialEffective.autocapture !== false;
1434
1539
  if (autocaptureEnabled) {
1435
1540
  analyticsInstance.startAutocapture();
1436
1541
  }
1437
1542
  setAnalytics(analyticsInstance);
1543
+ // Listen for options changes (when remote options are fetched)
1544
+ // Note: JourniumAnalytics already handles restarting autocapture when options change
1545
+ // We just need to update the effectiveOptions state for consumers
1546
+ const unsubscribe = analyticsInstance.onOptionsChange((newOptions) => {
1547
+ setEffectiveOptions(newOptions);
1548
+ });
1438
1549
  return () => {
1550
+ if (unsubscribe) {
1551
+ unsubscribe();
1552
+ }
1439
1553
  analyticsInstance.destroy();
1440
1554
  setAnalytics(null);
1441
1555
  setEffectiveOptions(null);