@journium/react 0.1.0-alpha.1 → 0.1.0-alpha.3

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.js CHANGED
@@ -1,6 +1,4 @@
1
- 'use strict';
2
-
3
- var React = require('react');
1
+ import React, { createContext, useState, useEffect, useContext, useCallback } from 'react';
4
2
 
5
3
  /**
6
4
  * uuidv7: A JavaScript implementation of UUID version 7
@@ -651,7 +649,8 @@ class JourniumClient {
651
649
  // Initialize identity manager
652
650
  this.identityManager = new BrowserIdentityManager(this.config.sessionTimeout, this.config.token);
653
651
  // Initialize synchronously with cached config, fetch fresh config in background
654
- this.initialize();
652
+ this.initializeSync();
653
+ this.fetchRemoteConfigAsync();
655
654
  }
656
655
  loadCachedConfig() {
657
656
  if (typeof window === 'undefined' || !window.localStorage) {
@@ -681,7 +680,7 @@ class JourniumClient {
681
680
  }
682
681
  }
683
682
  }
684
- async initialize() {
683
+ initializeSync() {
685
684
  var _a, _b, _c;
686
685
  // Step 1: Load cached config from localStorage (synchronous)
687
686
  const cachedConfig = this.loadCachedConfig();
@@ -724,11 +723,13 @@ class JourniumClient {
724
723
  this.startFlushTimer();
725
724
  }
726
725
  if (this.config.debug) {
727
- console.log('Journium: Client initialized immediately with config:', this.config);
726
+ console.log('Journium: Client initialized and ready to track events');
728
727
  }
729
- // Step 5: Fetch fresh config in background (don't await)
728
+ }
729
+ async fetchRemoteConfigAsync() {
730
+ // Fetch fresh config in background
730
731
  if (this.config.token) {
731
- this.fetchAndCacheRemoteConfig();
732
+ await this.fetchAndCacheRemoteConfig();
732
733
  }
733
734
  }
734
735
  async fetchAndCacheRemoteConfig() {
@@ -769,7 +770,8 @@ class JourniumClient {
769
770
  if (this.flushTimer) {
770
771
  clearInterval(this.flushTimer);
771
772
  }
772
- this.flushTimer = window.setInterval(() => {
773
+ // Use universal setInterval (works in both browser and Node.js)
774
+ this.flushTimer = setInterval(() => {
773
775
  this.flush();
774
776
  }, this.config.flushInterval);
775
777
  }
@@ -802,8 +804,12 @@ class JourniumClient {
802
804
  }
803
805
  }
804
806
  track(event, properties = {}) {
807
+ var _a;
805
808
  // Don't track if SDK is not properly configured
806
809
  if (!this.config || !this.config.token || !this.config.apiHost || !this.initialized) {
810
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug) {
811
+ console.warn('Journium: track() call rejected - SDK not ready');
812
+ }
807
813
  return;
808
814
  }
809
815
  const identity = this.identityManager.getIdentity();
@@ -1273,9 +1279,11 @@ class Journium {
1273
1279
  this.pageviewTracker = new PageviewTracker(this.client);
1274
1280
  const autocaptureConfig = this.resolveAutocaptureConfig(config.autocapture);
1275
1281
  this.autocaptureTracker = new AutocaptureTracker(this.client, autocaptureConfig);
1282
+ // Store resolved autocapture state for startAutoCapture method
1283
+ this.autocaptureEnabled = config.autocapture !== false;
1276
1284
  }
1277
1285
  resolveAutocaptureConfig(autocapture) {
1278
- if (autocapture === false || autocapture === undefined) {
1286
+ if (autocapture === false) {
1279
1287
  return {
1280
1288
  captureClicks: false,
1281
1289
  captureFormSubmits: false,
@@ -1283,8 +1291,8 @@ class Journium {
1283
1291
  captureTextSelection: false,
1284
1292
  };
1285
1293
  }
1286
- if (autocapture === true) {
1287
- return {}; // Use default configuration
1294
+ if (autocapture === true || autocapture === undefined) {
1295
+ return {}; // Use default configuration (enabled by default)
1288
1296
  }
1289
1297
  return autocapture;
1290
1298
  }
@@ -1296,7 +1304,7 @@ class Journium {
1296
1304
  }
1297
1305
  startAutoCapture() {
1298
1306
  this.pageviewTracker.startAutoCapture();
1299
- if (this.config.autocapture) {
1307
+ if (this.autocaptureEnabled) {
1300
1308
  this.autocaptureTracker.start();
1301
1309
  }
1302
1310
  }
@@ -1326,29 +1334,26 @@ const init = (config) => {
1326
1334
  return new Journium(config);
1327
1335
  };
1328
1336
 
1329
- const JourniumContext = React.createContext({ journium: null });
1337
+ const JourniumContext = createContext({ journium: null });
1330
1338
  const JourniumProvider = ({ children, config, autoCapture = true, }) => {
1331
- const journiumRef = React.useRef(null);
1332
- React.useEffect(() => {
1333
- if (!journiumRef.current) {
1334
- journiumRef.current = new Journium(config);
1335
- if (autoCapture) {
1336
- journiumRef.current.startAutoCapture();
1337
- }
1339
+ const [journium, setJournium] = useState(null);
1340
+ useEffect(() => {
1341
+ const journiumInstance = new Journium(config);
1342
+ if (autoCapture) {
1343
+ journiumInstance.startAutoCapture();
1338
1344
  }
1345
+ setJournium(journiumInstance);
1339
1346
  return () => {
1340
- if (journiumRef.current) {
1341
- journiumRef.current.destroy();
1342
- journiumRef.current = null;
1343
- }
1347
+ journiumInstance.destroy();
1348
+ setJournium(null);
1344
1349
  };
1345
1350
  }, [config, autoCapture]);
1346
1351
  // Note: All pageview tracking is handled by startAutoCapture() when autoCapture=true
1347
1352
  // When autoCapture=false, users should call capturePageview() manually as needed
1348
- return (React.createElement(JourniumContext.Provider, { value: { journium: journiumRef.current } }, children));
1353
+ return (React.createElement(JourniumContext.Provider, { value: { journium } }, children));
1349
1354
  };
1350
1355
  const useJournium = () => {
1351
- const context = React.useContext(JourniumContext);
1356
+ const context = useContext(JourniumContext);
1352
1357
  if (!context) {
1353
1358
  throw new Error('useJournium must be used within a JourniumProvider');
1354
1359
  }
@@ -1357,7 +1362,7 @@ const useJournium = () => {
1357
1362
 
1358
1363
  const useTrackEvent = () => {
1359
1364
  const { journium } = useJournium();
1360
- return React.useCallback((event, properties) => {
1365
+ return useCallback((event, properties) => {
1361
1366
  if (journium) {
1362
1367
  journium.track(event, properties);
1363
1368
  }
@@ -1365,7 +1370,7 @@ const useTrackEvent = () => {
1365
1370
  };
1366
1371
  const useTrackPageview = () => {
1367
1372
  const { journium } = useJournium();
1368
- return React.useCallback((properties) => {
1373
+ return useCallback((properties) => {
1369
1374
  if (journium) {
1370
1375
  journium.capturePageview(properties);
1371
1376
  }
@@ -1373,18 +1378,18 @@ const useTrackPageview = () => {
1373
1378
  };
1374
1379
  const useAutoTrackPageview = (dependencies = [], properties) => {
1375
1380
  const trackPageview = useTrackPageview();
1376
- React.useEffect(() => {
1381
+ useEffect(() => {
1377
1382
  trackPageview(properties);
1378
1383
  }, dependencies);
1379
1384
  };
1380
1385
  const useAutocapture = () => {
1381
1386
  const { journium } = useJournium();
1382
- const startAutocapture = React.useCallback(() => {
1387
+ const startAutocapture = useCallback(() => {
1383
1388
  if (journium) {
1384
1389
  journium.startAutocapture();
1385
1390
  }
1386
1391
  }, [journium]);
1387
- const stopAutocapture = React.useCallback(() => {
1392
+ const stopAutocapture = useCallback(() => {
1388
1393
  if (journium) {
1389
1394
  journium.stopAutocapture();
1390
1395
  }
@@ -1393,7 +1398,7 @@ const useAutocapture = () => {
1393
1398
  };
1394
1399
  const useAutoTrackClicks = (enabled = true, config) => {
1395
1400
  const { journium } = useJournium();
1396
- React.useEffect(() => {
1401
+ useEffect(() => {
1397
1402
  if (journium && enabled) {
1398
1403
  // Use startAutoCapture for consistency (includes both pageview + clicks)
1399
1404
  journium.startAutoCapture();
@@ -1405,27 +1410,5 @@ const useAutoTrackClicks = (enabled = true, config) => {
1405
1410
  }, [journium, enabled]);
1406
1411
  };
1407
1412
 
1408
- exports.AutocaptureTracker = AutocaptureTracker;
1409
- exports.BrowserIdentityManager = BrowserIdentityManager;
1410
- exports.Journium = Journium;
1411
- exports.JourniumClient = JourniumClient;
1412
- exports.JourniumProvider = JourniumProvider;
1413
- exports.PageviewTracker = PageviewTracker;
1414
- exports.fetchRemoteConfig = fetchRemoteConfig;
1415
- exports.generateId = generateId;
1416
- exports.generateUuidv7 = generateUuidv7;
1417
- exports.getCurrentTimestamp = getCurrentTimestamp;
1418
- exports.getCurrentUrl = getCurrentUrl;
1419
- exports.getPageTitle = getPageTitle;
1420
- exports.getReferrer = getReferrer;
1421
- exports.init = init;
1422
- exports.isBrowser = isBrowser;
1423
- exports.isNode = isNode;
1424
- exports.mergeConfigs = mergeConfigs;
1425
- exports.useAutoTrackClicks = useAutoTrackClicks;
1426
- exports.useAutoTrackPageview = useAutoTrackPageview;
1427
- exports.useAutocapture = useAutocapture;
1428
- exports.useJournium = useJournium;
1429
- exports.useTrackEvent = useTrackEvent;
1430
- exports.useTrackPageview = useTrackPageview;
1413
+ export { AutocaptureTracker, BrowserIdentityManager, Journium, JourniumClient, JourniumProvider, PageviewTracker, fetchRemoteConfig, generateId, generateUuidv7, getCurrentTimestamp, getCurrentUrl, getPageTitle, getReferrer, init, isBrowser, isNode, mergeConfigs, useAutoTrackClicks, useAutoTrackPageview, useAutocapture, useJournium, useTrackEvent, useTrackPageview };
1431
1414
  //# sourceMappingURL=index.js.map