saasco-sdk 0.1.3 → 0.1.4

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.
Files changed (3) hide show
  1. package/index.cjs.js +40 -16
  2. package/index.esm.js +40 -16
  3. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -434,19 +434,37 @@ const timezones = {
434
434
  };
435
435
 
436
436
  /* eslint-disable @typescript-eslint/no-explicit-any */
437
+ const isBrowser = typeof window !== 'undefined';
437
438
  let userId = null;
438
439
  const PREF = 'saasco-sdk';
440
+ const data = {};
439
441
  function storeData(key, value, ttl) {
440
- const fullKey = `${PREF}-${key}`;
441
- if (value === undefined) return window.localStorage.removeItem(fullKey);
442
- const now = new Date();
443
442
  const item = {
444
443
  value,
445
- expiry: now.getTime() + ttl
444
+ expiry: new Date().getTime() + ttl
446
445
  };
446
+ if (!isBrowser) {
447
+ if (value === undefined) {
448
+ delete data[key];
449
+ return;
450
+ }
451
+ data[key] = item;
452
+ return;
453
+ }
454
+ const fullKey = `${PREF}-${key}`;
455
+ if (value === undefined) return window.localStorage.removeItem(fullKey);
447
456
  localStorage.setItem(fullKey, JSON.stringify(item));
448
457
  }
449
458
  function retrieveData(key) {
459
+ if (!isBrowser) {
460
+ const item = data[key];
461
+ if (!item) return null;
462
+ if (Date.now() > item.expiry) {
463
+ delete data[key];
464
+ return null;
465
+ }
466
+ return item.value;
467
+ }
450
468
  const fullKey = `${PREF}-${key}`;
451
469
  const itemStr = localStorage.getItem(fullKey);
452
470
  if (!itemStr) {
@@ -528,22 +546,27 @@ class Saasco {
528
546
  var _a;
529
547
  // Prevent duplicate Page View tracking
530
548
  if (name === 'Page View') {
549
+ if (!isBrowser) return;
531
550
  const pageViewHref = window.location.href;
532
551
  if (pageViewHref === this.lastPageViewHref) return;
533
552
  this.lastPageViewHref = pageViewHref;
534
553
  }
535
554
  setSessionId();
536
555
  setAnonmousId();
537
- const customNavigator = navigator;
538
- const locale = customNavigator.languages && customNavigator.languages.length ? customNavigator.languages[0] : customNavigator.userLanguage || customNavigator.language || customNavigator.browserLanguage || 'en';
556
+ const customNavigator = isBrowser ? navigator : undefined;
557
+ const locale = isBrowser ? (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.languages) && (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.languages.length) ? customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.languages[0] : (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.userLanguage) || (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.language) || (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.browserLanguage) || 'en' : undefined;
539
558
  // https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone
540
559
  // Only has 96.63% global support, so we need to check for undefined
541
560
  const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
542
- const location = timezones[timezone] || ((_a = locale === null || locale === void 0 ? void 0 : locale.split('-')) === null || _a === void 0 ? void 0 : _a[1]) || 'undefined';
543
- const $screenHeight = screen.height;
544
- const $screenWidth = screen.width;
545
- const $screenDPI = window.devicePixelRatio;
546
- const $referrer = document.referrer;
561
+ const location = isBrowser ? timezones[timezone] || ((_a = locale === null || locale === void 0 ? void 0 : locale.split('-')) === null || _a === void 0 ? void 0 : _a[1]) || 'undefined' : undefined;
562
+ const userAgent = isBrowser ? window.navigator.userAgent : undefined;
563
+ const title = isBrowser ? document.title : undefined;
564
+ const pathname = isBrowser ? window.location.pathname : undefined;
565
+ const href = isBrowser ? window.location.href : undefined;
566
+ const $screenHeight = isBrowser ? screen.height : undefined;
567
+ const $screenWidth = isBrowser ? screen.width : undefined;
568
+ const $screenDPI = isBrowser ? window.devicePixelRatio : undefined;
569
+ const $referrer = isBrowser ? document.referrer : undefined;
547
570
  const $utm_source = this.getSearchParam('utm_source');
548
571
  const $utm_medium = this.getSearchParam('utm_medium');
549
572
  const $utm_campaign = this.getSearchParam('utm_campaign');
@@ -560,12 +583,12 @@ class Saasco {
560
583
  anonymousId: getAnonymousId(),
561
584
  projectId: this.config.projectId,
562
585
  payload: JSON.stringify({
563
- 'user-agent': window.navigator.userAgent,
586
+ 'user-agent': userAgent,
564
587
  locale,
565
588
  location,
566
- title: document.title,
567
- pathname: window.location.pathname,
568
- href: window.location.href,
589
+ title,
590
+ pathname,
591
+ href,
569
592
  $screenHeight,
570
593
  $screenWidth,
571
594
  $screenDPI,
@@ -662,6 +685,7 @@ class Saasco {
662
685
  ...args]);
663
686
  }
664
687
  getSearchParam(param) {
688
+ if (typeof window === 'undefined') return undefined;
665
689
  return new URLSearchParams(window.location.search).get(param);
666
690
  }
667
691
  /**
@@ -674,7 +698,7 @@ class Saasco {
674
698
  // Will run if undefined or true
675
699
  if (this.config.autoPageTracking === false) return;
676
700
  // Prevent running on the server
677
- if (typeof window === 'undefined') return;
701
+ if (!isBrowser) return console.warn('Saasco auto page tracking is only available in the browser');
678
702
  // Prevent intitializing auto page tracking more than once
679
703
  if (window.saascoAutoPageTrackingActive) {
680
704
  this.log('Auto Page Tracking already enabled');
package/index.esm.js CHANGED
@@ -430,19 +430,37 @@ const timezones = {
430
430
  };
431
431
 
432
432
  /* eslint-disable @typescript-eslint/no-explicit-any */
433
+ const isBrowser = typeof window !== 'undefined';
433
434
  let userId = null;
434
435
  const PREF = 'saasco-sdk';
436
+ const data = {};
435
437
  function storeData(key, value, ttl) {
436
- const fullKey = `${PREF}-${key}`;
437
- if (value === undefined) return window.localStorage.removeItem(fullKey);
438
- const now = new Date();
439
438
  const item = {
440
439
  value,
441
- expiry: now.getTime() + ttl
440
+ expiry: new Date().getTime() + ttl
442
441
  };
442
+ if (!isBrowser) {
443
+ if (value === undefined) {
444
+ delete data[key];
445
+ return;
446
+ }
447
+ data[key] = item;
448
+ return;
449
+ }
450
+ const fullKey = `${PREF}-${key}`;
451
+ if (value === undefined) return window.localStorage.removeItem(fullKey);
443
452
  localStorage.setItem(fullKey, JSON.stringify(item));
444
453
  }
445
454
  function retrieveData(key) {
455
+ if (!isBrowser) {
456
+ const item = data[key];
457
+ if (!item) return null;
458
+ if (Date.now() > item.expiry) {
459
+ delete data[key];
460
+ return null;
461
+ }
462
+ return item.value;
463
+ }
446
464
  const fullKey = `${PREF}-${key}`;
447
465
  const itemStr = localStorage.getItem(fullKey);
448
466
  if (!itemStr) {
@@ -524,22 +542,27 @@ class Saasco {
524
542
  var _a;
525
543
  // Prevent duplicate Page View tracking
526
544
  if (name === 'Page View') {
545
+ if (!isBrowser) return;
527
546
  const pageViewHref = window.location.href;
528
547
  if (pageViewHref === this.lastPageViewHref) return;
529
548
  this.lastPageViewHref = pageViewHref;
530
549
  }
531
550
  setSessionId();
532
551
  setAnonmousId();
533
- const customNavigator = navigator;
534
- const locale = customNavigator.languages && customNavigator.languages.length ? customNavigator.languages[0] : customNavigator.userLanguage || customNavigator.language || customNavigator.browserLanguage || 'en';
552
+ const customNavigator = isBrowser ? navigator : undefined;
553
+ const locale = isBrowser ? (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.languages) && (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.languages.length) ? customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.languages[0] : (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.userLanguage) || (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.language) || (customNavigator === null || customNavigator === void 0 ? void 0 : customNavigator.browserLanguage) || 'en' : undefined;
535
554
  // https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone
536
555
  // Only has 96.63% global support, so we need to check for undefined
537
556
  const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
538
- const location = timezones[timezone] || ((_a = locale === null || locale === void 0 ? void 0 : locale.split('-')) === null || _a === void 0 ? void 0 : _a[1]) || 'undefined';
539
- const $screenHeight = screen.height;
540
- const $screenWidth = screen.width;
541
- const $screenDPI = window.devicePixelRatio;
542
- const $referrer = document.referrer;
557
+ const location = isBrowser ? timezones[timezone] || ((_a = locale === null || locale === void 0 ? void 0 : locale.split('-')) === null || _a === void 0 ? void 0 : _a[1]) || 'undefined' : undefined;
558
+ const userAgent = isBrowser ? window.navigator.userAgent : undefined;
559
+ const title = isBrowser ? document.title : undefined;
560
+ const pathname = isBrowser ? window.location.pathname : undefined;
561
+ const href = isBrowser ? window.location.href : undefined;
562
+ const $screenHeight = isBrowser ? screen.height : undefined;
563
+ const $screenWidth = isBrowser ? screen.width : undefined;
564
+ const $screenDPI = isBrowser ? window.devicePixelRatio : undefined;
565
+ const $referrer = isBrowser ? document.referrer : undefined;
543
566
  const $utm_source = this.getSearchParam('utm_source');
544
567
  const $utm_medium = this.getSearchParam('utm_medium');
545
568
  const $utm_campaign = this.getSearchParam('utm_campaign');
@@ -556,12 +579,12 @@ class Saasco {
556
579
  anonymousId: getAnonymousId(),
557
580
  projectId: this.config.projectId,
558
581
  payload: JSON.stringify({
559
- 'user-agent': window.navigator.userAgent,
582
+ 'user-agent': userAgent,
560
583
  locale,
561
584
  location,
562
- title: document.title,
563
- pathname: window.location.pathname,
564
- href: window.location.href,
585
+ title,
586
+ pathname,
587
+ href,
565
588
  $screenHeight,
566
589
  $screenWidth,
567
590
  $screenDPI,
@@ -658,6 +681,7 @@ class Saasco {
658
681
  ...args]);
659
682
  }
660
683
  getSearchParam(param) {
684
+ if (typeof window === 'undefined') return undefined;
661
685
  return new URLSearchParams(window.location.search).get(param);
662
686
  }
663
687
  /**
@@ -670,7 +694,7 @@ class Saasco {
670
694
  // Will run if undefined or true
671
695
  if (this.config.autoPageTracking === false) return;
672
696
  // Prevent running on the server
673
- if (typeof window === 'undefined') return;
697
+ if (!isBrowser) return console.warn('Saasco auto page tracking is only available in the browser');
674
698
  // Prevent intitializing auto page tracking more than once
675
699
  if (window.saascoAutoPageTrackingActive) {
676
700
  this.log('Auto Page Tracking already enabled');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasco-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "@lukeed/uuid": "^2.0.1",