@journium/react 1.1.4 → 1.2.1

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/context.d.ts CHANGED
@@ -11,7 +11,16 @@ interface JourniumProviderProps {
11
11
  children: ReactNode;
12
12
  config: JourniumConfig;
13
13
  }
14
+ /**
15
+ * Provides the Journium analytics instance to the React tree.
16
+ * Initializes the SDK on mount and tears it down on unmount.
17
+ * Re-initializes if `config` changes.
18
+ */
14
19
  export declare const JourniumProvider: React.FC<JourniumProviderProps>;
20
+ /**
21
+ * Access the Journium analytics instance and effective options from any component
22
+ * inside `JourniumProvider`. Throws if called outside the provider.
23
+ */
15
24
  export declare const useJournium: () => JourniumContextValue;
16
25
  export {};
17
26
  //# sourceMappingURL=context.d.ts.map
@@ -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,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtE,KAAK,yBAAyB,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AAEzD,UAAU,oBAAoB;IAC5B,SAAS,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAC5C,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,CAuC5D,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,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAEtE,KAAK,yBAAyB,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AAEzD,UAAU,oBAAoB;IAC5B,SAAS,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAC5C,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;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,qBAAqB,CAuC5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,QAAO,oBAM9B,CAAC"}
package/dist/hooks.d.ts CHANGED
@@ -1,8 +1,22 @@
1
+ /** Returns a stable callback for tracking custom events. */
1
2
  export declare const useTrackEvent: () => (event: string, properties?: Record<string, unknown>) => void;
3
+ /** Returns a stable callback for identifying the current user. */
2
4
  export declare const useIdentify: () => (distinctId: string, attributes?: Record<string, unknown>) => void;
5
+ /** Returns a stable callback for resetting the current identity and starting a new anonymous session. */
3
6
  export declare const useReset: () => () => void;
7
+ /** Returns a stable callback for manually capturing a $pageview event. */
4
8
  export declare const useTrackPageview: () => (properties?: Record<string, unknown>) => void;
9
+ /**
10
+ * Automatically captures a $pageview whenever the given dependencies change.
11
+ * Also fires once on mount. Useful for manual SPA route tracking when automatic
12
+ * tracking is disabled.
13
+ */
5
14
  export declare const useAutoTrackPageview: (dependencies?: React.DependencyList, properties?: Record<string, unknown>) => void;
15
+ /**
16
+ * Returns `stopAutocapture` to pause DOM event capture and pageview tracking.
17
+ * `startAutocapture` is also exposed for restarting after an explicit stop,
18
+ * but under normal usage autocapture starts automatically on SDK init.
19
+ */
6
20
  export declare const useAutocapture: () => {
7
21
  startAutocapture: () => void;
8
22
  stopAutocapture: () => void;
@@ -1 +1 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,aAAa,gBAId,MAAM,eAAe,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAOvD,CAAC;AAEF,eAAO,MAAM,WAAW,qBAIP,MAAM,eAAe,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAO5D,CAAC;AAEF,eAAO,MAAM,QAAQ,kBAQpB,CAAC;AAEF,eAAO,MAAM,gBAAgB,sBAIX,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAOxC,CAAC;AAEF,eAAO,MAAM,oBAAoB,kBACjB,KAAK,CAAC,cAAc,eACrB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAOrC,CAAC;AAEF,eAAO,MAAM,cAAc;;;CAgB1B,CAAC"}
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAGA,4DAA4D;AAC5D,eAAO,MAAM,aAAa,gBAId,MAAM,eAAe,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAOvD,CAAC;AAEF,kEAAkE;AAClE,eAAO,MAAM,WAAW,qBAIP,MAAM,eAAe,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAO5D,CAAC;AAEF,yGAAyG;AACzG,eAAO,MAAM,QAAQ,kBAQpB,CAAC;AAEF,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,sBAIX,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAOxC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAC/B,eAAc,KAAK,CAAC,cAAmB,EACvC,aAAa,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,SAOrC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,cAAc;;;CAgB1B,CAAC"}
package/dist/index.cjs CHANGED
@@ -1104,28 +1104,31 @@ class PageviewTracker {
1104
1104
  * Start automatic autocapture for pageviews
1105
1105
  * @param captureInitialPageview - whether to fire a $pageview immediately on start (default: true).
1106
1106
  * Pass false when restarting after a remote options update to avoid a spurious pageview.
1107
+ * @param patchHistory - whether to monkey-patch pushState/replaceState/popstate (default: true).
1108
+ * Pass false when a framework-native router tracker (e.g. Next.js) owns SPA pageviews.
1107
1109
  */
1108
- startAutoPageviewTracking(captureInitialPageview = true) {
1110
+ startAutoPageviewTracking(captureInitialPageview = true, patchHistory = true) {
1109
1111
  if (captureInitialPageview) {
1110
1112
  this.capturePageview();
1111
1113
  }
1112
- if (typeof window !== 'undefined') {
1113
- // Store original methods for cleanup
1114
- this.originalPushState = window.history.pushState;
1115
- this.originalReplaceState = window.history.replaceState;
1116
- window.history.pushState = (...args) => {
1117
- this.originalPushState.apply(window.history, args);
1118
- setTimeout(() => this.capturePageview(), 0);
1119
- };
1120
- window.history.replaceState = (...args) => {
1121
- this.originalReplaceState.apply(window.history, args);
1122
- setTimeout(() => this.capturePageview(), 0);
1123
- };
1124
- this.popStateHandler = () => {
1125
- setTimeout(() => this.capturePageview(), 0);
1126
- };
1127
- window.addEventListener('popstate', this.popStateHandler);
1114
+ if (!patchHistory || typeof window === 'undefined') {
1115
+ return;
1128
1116
  }
1117
+ // Store original methods for cleanup
1118
+ this.originalPushState = window.history.pushState;
1119
+ this.originalReplaceState = window.history.replaceState;
1120
+ window.history.pushState = (...args) => {
1121
+ this.originalPushState.apply(window.history, args);
1122
+ setTimeout(() => this.capturePageview(), 0);
1123
+ };
1124
+ window.history.replaceState = (...args) => {
1125
+ this.originalReplaceState.apply(window.history, args);
1126
+ setTimeout(() => this.capturePageview(), 0);
1127
+ };
1128
+ this.popStateHandler = () => {
1129
+ setTimeout(() => this.capturePageview(), 0);
1130
+ };
1131
+ window.addEventListener('popstate', this.popStateHandler);
1129
1132
  }
1130
1133
  /**
1131
1134
  * Stop automatic autocapture for pageviews
@@ -1540,6 +1543,20 @@ class JourniumAnalytics {
1540
1543
  // This handles cached remote options or local options with autocapture enabled
1541
1544
  this.startAutocaptureIfEnabled(initialEffectiveOptions);
1542
1545
  }
1546
+ resolvePageviewOptions(autoTrackPageviews, frameworkHandlesPageviews) {
1547
+ if (autoTrackPageviews === false || frameworkHandlesPageviews) {
1548
+ return { enabled: false, trackSpaPageviews: false, captureInitialPageview: false };
1549
+ }
1550
+ if (autoTrackPageviews === true || autoTrackPageviews === undefined) {
1551
+ return { enabled: true, trackSpaPageviews: true, captureInitialPageview: true };
1552
+ }
1553
+ // object form implies enabled
1554
+ return {
1555
+ enabled: true,
1556
+ trackSpaPageviews: autoTrackPageviews.trackSpaPageviews !== false,
1557
+ captureInitialPageview: autoTrackPageviews.trackInitialPageview !== false,
1558
+ };
1559
+ }
1543
1560
  resolveAutocaptureOptions(autocapture) {
1544
1561
  if (autocapture === false) {
1545
1562
  return {
@@ -1554,39 +1571,50 @@ class JourniumAnalytics {
1554
1571
  }
1555
1572
  return autocapture;
1556
1573
  }
1574
+ /** Track a custom event with optional properties. */
1557
1575
  track(event, properties) {
1558
1576
  this.client.track(event, properties);
1559
1577
  }
1578
+ /** Associate the current session with a known user identity and optional attributes. */
1560
1579
  identify(distinctId, attributes) {
1561
1580
  this.client.identify(distinctId, attributes);
1562
1581
  }
1582
+ /** Clear the current identity, starting a new anonymous session. */
1563
1583
  reset() {
1564
1584
  this.client.reset();
1565
1585
  }
1586
+ /** Manually capture a $pageview event with optional custom properties. */
1566
1587
  capturePageview(properties) {
1567
1588
  this.pageviewTracker.capturePageview(properties);
1568
1589
  }
1590
+ /**
1591
+ * Manually start autocapture (pageview tracking + DOM event capture).
1592
+ * Under normal usage this is not needed — the SDK starts automatically on init.
1593
+ * Useful only if autocapture was explicitly stopped and needs to be restarted.
1594
+ */
1569
1595
  startAutocapture() {
1570
1596
  // Always check effective options (which may include remote options)
1571
1597
  const effectiveOptions = this.client.getEffectiveOptions();
1572
- // Only enable if effectiveOptions are loaded and autoTrackPageviews is not explicitly false
1573
- const autoTrackPageviews = effectiveOptions && Object.keys(effectiveOptions).length > 0
1574
- ? effectiveOptions.autoTrackPageviews !== false
1575
- : false;
1576
- const autocaptureEnabled = effectiveOptions && Object.keys(effectiveOptions).length > 0
1598
+ // Only start if effectiveOptions are actually loaded (non-empty)
1599
+ const hasOptions = effectiveOptions && Object.keys(effectiveOptions).length > 0;
1600
+ const { enabled: autoTrackPageviews, trackSpaPageviews, captureInitialPageview } = hasOptions
1601
+ ? this.resolvePageviewOptions(effectiveOptions.autoTrackPageviews, effectiveOptions._frameworkHandlesPageviews)
1602
+ : { enabled: false, trackSpaPageviews: false, captureInitialPageview: false };
1603
+ const autocaptureEnabled = hasOptions
1577
1604
  ? effectiveOptions.autocapture !== false
1578
1605
  : false;
1579
1606
  // Update autocapture tracker options if they've changed
1580
1607
  const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
1581
1608
  this.autocaptureTracker.updateOptions(autocaptureOptions);
1582
1609
  if (autoTrackPageviews) {
1583
- this.pageviewTracker.startAutoPageviewTracking();
1610
+ this.pageviewTracker.startAutoPageviewTracking(captureInitialPageview, trackSpaPageviews);
1584
1611
  }
1585
1612
  if (autocaptureEnabled) {
1586
1613
  this.autocaptureTracker.start();
1587
1614
  }
1588
1615
  this.autocaptureStarted = true;
1589
1616
  }
1617
+ /** Stop autocapture — pauses pageview tracking and DOM event capture. */
1590
1618
  stopAutocapture() {
1591
1619
  this.pageviewTracker.stopAutocapture();
1592
1620
  this.autocaptureTracker.stop();
@@ -1606,13 +1634,13 @@ class JourniumAnalytics {
1606
1634
  const hasActualOptions = effectiveOptions && Object.keys(effectiveOptions).length > 0;
1607
1635
  if (hasActualOptions) {
1608
1636
  // Use same logic as manual startAutocapture() but only start automatically
1609
- const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
1637
+ const { enabled: autoTrackPageviews, trackSpaPageviews, captureInitialPageview } = this.resolvePageviewOptions(effectiveOptions.autoTrackPageviews, effectiveOptions._frameworkHandlesPageviews);
1610
1638
  const autocaptureEnabled = effectiveOptions.autocapture !== false;
1611
1639
  // Update autocapture tracker options
1612
1640
  const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
1613
1641
  this.autocaptureTracker.updateOptions(autocaptureOptions);
1614
1642
  if (autoTrackPageviews) {
1615
- this.pageviewTracker.startAutoPageviewTracking();
1643
+ this.pageviewTracker.startAutoPageviewTracking(captureInitialPageview, trackSpaPageviews);
1616
1644
  }
1617
1645
  if (autocaptureEnabled) {
1618
1646
  this.autocaptureTracker.start();
@@ -1637,21 +1665,23 @@ class JourniumAnalytics {
1637
1665
  this.autocaptureTracker.stop();
1638
1666
  this.autocaptureStarted = false;
1639
1667
  }
1640
- const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
1668
+ const { enabled: autoTrackPageviews, trackSpaPageviews, captureInitialPageview } = this.resolvePageviewOptions(effectiveOptions.autoTrackPageviews, effectiveOptions._frameworkHandlesPageviews);
1641
1669
  const autocaptureEnabled = effectiveOptions.autocapture !== false;
1642
1670
  const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
1643
1671
  this.autocaptureTracker.updateOptions(autocaptureOptions);
1644
1672
  if (autoTrackPageviews) {
1645
- this.pageviewTracker.startAutoPageviewTracking(isFirstStart);
1673
+ this.pageviewTracker.startAutoPageviewTracking(isFirstStart && captureInitialPageview, trackSpaPageviews);
1646
1674
  }
1647
1675
  if (autocaptureEnabled) {
1648
1676
  this.autocaptureTracker.start();
1649
1677
  }
1650
1678
  this.autocaptureStarted = autoTrackPageviews || autocaptureEnabled;
1651
1679
  }
1680
+ /** Flush all queued events to the ingestion endpoint immediately. */
1652
1681
  async flush() {
1653
1682
  return this.client.flush();
1654
1683
  }
1684
+ /** Return the currently active options (merged local + remote config). */
1655
1685
  getEffectiveOptions() {
1656
1686
  return this.client.getEffectiveOptions();
1657
1687
  }
@@ -1661,6 +1691,7 @@ class JourniumAnalytics {
1661
1691
  onOptionsChange(callback) {
1662
1692
  return this.client.onOptionsChange(callback);
1663
1693
  }
1694
+ /** Tear down the analytics instance: stop all tracking, flush pending events, and release resources. */
1664
1695
  destroy() {
1665
1696
  this.pageviewTracker.stopAutocapture();
1666
1697
  this.autocaptureTracker.stop();
@@ -1670,11 +1701,17 @@ class JourniumAnalytics {
1670
1701
  this.client.destroy();
1671
1702
  }
1672
1703
  }
1704
+ /** Create and return a new JourniumAnalytics instance for the given config. */
1673
1705
  const init = (config) => {
1674
1706
  return new JourniumAnalytics(config);
1675
1707
  };
1676
1708
 
1677
1709
  const JourniumContext = React.createContext(undefined);
1710
+ /**
1711
+ * Provides the Journium analytics instance to the React tree.
1712
+ * Initializes the SDK on mount and tears it down on unmount.
1713
+ * Re-initializes if `config` changes.
1714
+ */
1678
1715
  const JourniumProvider = ({ children, config, }) => {
1679
1716
  const [analytics, setAnalytics] = React.useState(null);
1680
1717
  const [effectiveOptions, setEffectiveOptions] = React.useState(null);
@@ -1702,6 +1739,10 @@ const JourniumProvider = ({ children, config, }) => {
1702
1739
  }, [config]);
1703
1740
  return (React.createElement(JourniumContext.Provider, { value: { analytics, config, effectiveOptions } }, children));
1704
1741
  };
1742
+ /**
1743
+ * Access the Journium analytics instance and effective options from any component
1744
+ * inside `JourniumProvider`. Throws if called outside the provider.
1745
+ */
1705
1746
  const useJournium = () => {
1706
1747
  const context = React.useContext(JourniumContext);
1707
1748
  if (!context) {
@@ -1710,6 +1751,7 @@ const useJournium = () => {
1710
1751
  return context;
1711
1752
  };
1712
1753
 
1754
+ /** Returns a stable callback for tracking custom events. */
1713
1755
  const useTrackEvent = () => {
1714
1756
  const { analytics } = useJournium();
1715
1757
  return React.useCallback((event, properties) => {
@@ -1718,6 +1760,7 @@ const useTrackEvent = () => {
1718
1760
  }
1719
1761
  }, [analytics]);
1720
1762
  };
1763
+ /** Returns a stable callback for identifying the current user. */
1721
1764
  const useIdentify = () => {
1722
1765
  const { analytics } = useJournium();
1723
1766
  return React.useCallback((distinctId, attributes) => {
@@ -1726,6 +1769,7 @@ const useIdentify = () => {
1726
1769
  }
1727
1770
  }, [analytics]);
1728
1771
  };
1772
+ /** Returns a stable callback for resetting the current identity and starting a new anonymous session. */
1729
1773
  const useReset = () => {
1730
1774
  const { analytics } = useJournium();
1731
1775
  return React.useCallback(() => {
@@ -1734,6 +1778,7 @@ const useReset = () => {
1734
1778
  }
1735
1779
  }, [analytics]);
1736
1780
  };
1781
+ /** Returns a stable callback for manually capturing a $pageview event. */
1737
1782
  const useTrackPageview = () => {
1738
1783
  const { analytics } = useJournium();
1739
1784
  return React.useCallback((properties) => {
@@ -1742,12 +1787,22 @@ const useTrackPageview = () => {
1742
1787
  }
1743
1788
  }, [analytics]);
1744
1789
  };
1790
+ /**
1791
+ * Automatically captures a $pageview whenever the given dependencies change.
1792
+ * Also fires once on mount. Useful for manual SPA route tracking when automatic
1793
+ * tracking is disabled.
1794
+ */
1745
1795
  const useAutoTrackPageview = (dependencies = [], properties) => {
1746
1796
  const trackPageview = useTrackPageview();
1747
1797
  React.useEffect(() => {
1748
1798
  trackPageview(properties);
1749
1799
  }, [trackPageview, properties, ...dependencies]);
1750
1800
  };
1801
+ /**
1802
+ * Returns `stopAutocapture` to pause DOM event capture and pageview tracking.
1803
+ * `startAutocapture` is also exposed for restarting after an explicit stop,
1804
+ * but under normal usage autocapture starts automatically on SDK init.
1805
+ */
1751
1806
  const useAutocapture = () => {
1752
1807
  const { analytics } = useJournium();
1753
1808
  const startAutocapture = React.useCallback(() => {