saasco-sdk 0.1.3 → 0.1.5

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/README.md CHANGED
@@ -4,7 +4,6 @@
4
4
 
5
5
  - [Privacy](#privacy)
6
6
  - [Getting Started](#initiate-the-lib)
7
- - [Naming Conflict](#naming-conflict)
8
7
  - [Automatic Page View Tracking](#automatic-page-view-tracking)
9
8
  - [Tracking Events](#tracking-events)
10
9
  - [Identifying Users](#identifying-users)
@@ -17,7 +16,7 @@
17
16
 
18
17
  By default Saasco is privacy friendly, using no cookies and obscuring all activity behind Anonymous IDs. However if you want to power more complex user tracking you can identify users and then all events they do will be attributed to them in your CRM. This can assist with customer support but also for things like drip campaigns and other marketing activities.
19
18
 
20
- ## Getting Started:
19
+ ## Getting Started
21
20
 
22
21
  Install by running:
23
22
  `npm install saasco-sdk`
@@ -247,14 +246,18 @@ Our SDKs automatically collect certain properties on every event or user profile
247
246
  | $latitude | Latitude | Latitude of the user's IP location. |
248
247
  | $longitude | Longitude | Longitude of the user's IP location. |
249
248
  | $timezone | Timezone | Timezone of the user parsed from the IP. |
249
+ | $locale | Locale | The preferred language of the user. |
250
+ | $pathname | Pathname | The path of the page on which the event was tracked. |
251
+ | $title | Page Title | The title of the page on which the event was tracked. |
252
+ | $userAgent | User Agent | The user agent string of the browser. |
250
253
  | $screenHeight | Screen Height | The height of the device screen in pixels |
251
254
  | $screenWidth | Screen Width | The width of the device screen in pixels |
252
255
  | $screenDpi | Screen DPI | Pixel density of the device screen. |
253
256
  | $currentUrl | Current URL | The URL of the page on which the event was tracked. |
254
257
  | $os | Operating System | The most recent OS of the user. |
255
258
  | $browser | Browser | The most recent browser of the user. |
256
- | $browserVersion | Browser Version | The most recent bowser version of the user. |
257
- | $device | Device Type | The most recent bowser version of the user. eg `Mobile`, `Tablet`, `Desktop` |
259
+ | $browserVersion | Browser Version | The most recent browser version of the user. |
260
+ | $device | Device Type | The most recent device type of the user. eg `Mobile`, `Tablet`, `Desktop` |
258
261
  | $referrer | Last Touch Referrer | Referring URL when the user last interacted with your site. Defaults to "direct" |
259
262
  | $referringDomain | Last Touch Referring Domain | Referring domain at the user's last interaction. Defaults to "direct" |
260
263
  | $utmSource | UTM Source | The UTM source tag from the URL a customer clicked to arrive at your domain. |
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,55 +546,49 @@ class Saasco {
528
546
  var _a;
529
547
  // Prevent duplicate Page View tracking
530
548
  if (name === 'Page View') {
549
+ if (!isBrowser) {
550
+ console.warn('Saasco page tracking is only available in the browser');
551
+ return;
552
+ }
531
553
  const pageViewHref = window.location.href;
532
554
  if (pageViewHref === this.lastPageViewHref) return;
533
555
  this.lastPageViewHref = pageViewHref;
534
556
  }
535
557
  setSessionId();
536
558
  setAnonmousId();
537
- const customNavigator = navigator;
538
- const locale = customNavigator.languages && customNavigator.languages.length ? customNavigator.languages[0] : customNavigator.userLanguage || customNavigator.language || customNavigator.browserLanguage || 'en';
559
+ const customNavigator = isBrowser ? navigator : undefined;
560
+ 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
561
  // https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone
540
562
  // Only has 96.63% global support, so we need to check for undefined
541
563
  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;
547
- const $utm_source = this.getSearchParam('utm_source');
548
- const $utm_medium = this.getSearchParam('utm_medium');
549
- const $utm_campaign = this.getSearchParam('utm_campaign');
550
- const $utm_content = this.getSearchParam('utm_content');
551
- const $utm_term = this.getSearchParam('utm_term');
552
- const $gclid = this.getSearchParam('gclid');
553
- const $fbclid = this.getSearchParam('fbclid');
564
+ 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;
565
+ const $userAgent = isBrowser ? window.navigator.userAgent : undefined;
566
+ const $title = isBrowser ? document.title : undefined;
567
+ const $pathname = isBrowser ? window.location.pathname : undefined;
568
+ const $href = isBrowser ? window.location.href : undefined;
569
+ const $screenHeight = isBrowser ? screen.height : undefined;
570
+ const $screenWidth = isBrowser ? screen.width : undefined;
571
+ const $screenDPI = isBrowser ? window.devicePixelRatio : undefined;
572
+ const $referrer = isBrowser ? document.referrer : undefined;
554
573
  const data = {
555
574
  id: uuid.v4(),
556
575
  timestamp: new Date().toISOString(),
557
576
  action: name,
558
- version: '1',
577
+ version: '2',
559
578
  sessionId: getSessionId(),
560
579
  anonymousId: getAnonymousId(),
561
580
  projectId: this.config.projectId,
562
581
  payload: JSON.stringify({
563
- 'user-agent': window.navigator.userAgent,
564
- locale,
565
- location,
566
- title: document.title,
567
- pathname: window.location.pathname,
568
- href: window.location.href,
582
+ $href,
583
+ $locale,
584
+ $location,
585
+ $pathname,
586
+ $referrer,
587
+ $screenDPI,
569
588
  $screenHeight,
570
589
  $screenWidth,
571
- $screenDPI,
572
- $referrer,
573
- $utm_source,
574
- $utm_medium,
575
- $utm_campaign,
576
- $utm_content,
577
- $utm_term,
578
- $gclid,
579
- $fbclid,
590
+ $title,
591
+ $userAgent,
580
592
  properties: properties || {}
581
593
  })
582
594
  };
@@ -617,7 +629,7 @@ class Saasco {
617
629
  projectId: this.config.projectId,
618
630
  distinctId,
619
631
  anonymousId,
620
- version: '1',
632
+ version: '2',
621
633
  payload: properties || {},
622
634
  options: options || {}
623
635
  };
@@ -662,6 +674,7 @@ class Saasco {
662
674
  ...args]);
663
675
  }
664
676
  getSearchParam(param) {
677
+ if (typeof window === 'undefined') return undefined;
665
678
  return new URLSearchParams(window.location.search).get(param);
666
679
  }
667
680
  /**
@@ -674,7 +687,7 @@ class Saasco {
674
687
  // Will run if undefined or true
675
688
  if (this.config.autoPageTracking === false) return;
676
689
  // Prevent running on the server
677
- if (typeof window === 'undefined') return;
690
+ if (!isBrowser) return console.warn('Saasco auto page tracking is only available in the browser');
678
691
  // Prevent intitializing auto page tracking more than once
679
692
  if (window.saascoAutoPageTrackingActive) {
680
693
  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,55 +542,49 @@ class Saasco {
524
542
  var _a;
525
543
  // Prevent duplicate Page View tracking
526
544
  if (name === 'Page View') {
545
+ if (!isBrowser) {
546
+ console.warn('Saasco page tracking is only available in the browser');
547
+ return;
548
+ }
527
549
  const pageViewHref = window.location.href;
528
550
  if (pageViewHref === this.lastPageViewHref) return;
529
551
  this.lastPageViewHref = pageViewHref;
530
552
  }
531
553
  setSessionId();
532
554
  setAnonmousId();
533
- const customNavigator = navigator;
534
- const locale = customNavigator.languages && customNavigator.languages.length ? customNavigator.languages[0] : customNavigator.userLanguage || customNavigator.language || customNavigator.browserLanguage || 'en';
555
+ const customNavigator = isBrowser ? navigator : undefined;
556
+ 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
557
  // https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone
536
558
  // Only has 96.63% global support, so we need to check for undefined
537
559
  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;
543
- const $utm_source = this.getSearchParam('utm_source');
544
- const $utm_medium = this.getSearchParam('utm_medium');
545
- const $utm_campaign = this.getSearchParam('utm_campaign');
546
- const $utm_content = this.getSearchParam('utm_content');
547
- const $utm_term = this.getSearchParam('utm_term');
548
- const $gclid = this.getSearchParam('gclid');
549
- const $fbclid = this.getSearchParam('fbclid');
560
+ 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;
561
+ const $userAgent = isBrowser ? window.navigator.userAgent : undefined;
562
+ const $title = isBrowser ? document.title : undefined;
563
+ const $pathname = isBrowser ? window.location.pathname : undefined;
564
+ const $href = isBrowser ? window.location.href : undefined;
565
+ const $screenHeight = isBrowser ? screen.height : undefined;
566
+ const $screenWidth = isBrowser ? screen.width : undefined;
567
+ const $screenDPI = isBrowser ? window.devicePixelRatio : undefined;
568
+ const $referrer = isBrowser ? document.referrer : undefined;
550
569
  const data = {
551
570
  id: v4(),
552
571
  timestamp: new Date().toISOString(),
553
572
  action: name,
554
- version: '1',
573
+ version: '2',
555
574
  sessionId: getSessionId(),
556
575
  anonymousId: getAnonymousId(),
557
576
  projectId: this.config.projectId,
558
577
  payload: JSON.stringify({
559
- 'user-agent': window.navigator.userAgent,
560
- locale,
561
- location,
562
- title: document.title,
563
- pathname: window.location.pathname,
564
- href: window.location.href,
578
+ $href,
579
+ $locale,
580
+ $location,
581
+ $pathname,
582
+ $referrer,
583
+ $screenDPI,
565
584
  $screenHeight,
566
585
  $screenWidth,
567
- $screenDPI,
568
- $referrer,
569
- $utm_source,
570
- $utm_medium,
571
- $utm_campaign,
572
- $utm_content,
573
- $utm_term,
574
- $gclid,
575
- $fbclid,
586
+ $title,
587
+ $userAgent,
576
588
  properties: properties || {}
577
589
  })
578
590
  };
@@ -613,7 +625,7 @@ class Saasco {
613
625
  projectId: this.config.projectId,
614
626
  distinctId,
615
627
  anonymousId,
616
- version: '1',
628
+ version: '2',
617
629
  payload: properties || {},
618
630
  options: options || {}
619
631
  };
@@ -658,6 +670,7 @@ class Saasco {
658
670
  ...args]);
659
671
  }
660
672
  getSearchParam(param) {
673
+ if (typeof window === 'undefined') return undefined;
661
674
  return new URLSearchParams(window.location.search).get(param);
662
675
  }
663
676
  /**
@@ -670,7 +683,7 @@ class Saasco {
670
683
  // Will run if undefined or true
671
684
  if (this.config.autoPageTracking === false) return;
672
685
  // Prevent running on the server
673
- if (typeof window === 'undefined') return;
686
+ if (!isBrowser) return console.warn('Saasco auto page tracking is only available in the browser');
674
687
  // Prevent intitializing auto page tracking more than once
675
688
  if (window.saascoAutoPageTrackingActive) {
676
689
  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.5",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "@lukeed/uuid": "^2.0.1",