@usermaven/sdk-js 1.0.4 → 1.0.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
@@ -5,6 +5,7 @@ Usermaven.js is a JavaScript SDK for [Usermaven](https://usermaven.com).
5
5
  ## Capabilities
6
6
 
7
7
  - Session Capability via `persistence` and `persistence_name` options.
8
+ - Cross sub-domain compatibility added
8
9
 
9
10
  ## Maintainers Guide
10
11
 
@@ -31,6 +32,20 @@ npmjs account beforehand (make sure you have access to Usermaven team)
31
32
  * `npm link @usermaven/sdk-js` --- use npm package locally whose symlink is just published
32
33
  * `npm start` --- start the application and monitor events
33
34
 
35
+ ### Checking Cross Domain Session locally
36
+ Setup a custom domain and sub-domain locally to test cross domain session persistence.
37
+
38
+ ```bash
39
+ sudo nano /etc/hosts
40
+ ```
41
+ Add the following lines in the hosts file
42
+ ```bash
43
+ 127.0.0.1 localhost.com
44
+ 127.0.0.1 app.localhost.com
45
+ ```
46
+
47
+ You will be able to access the domains at [localhost domain](http://localhost.com:8081/test-case/embed.html) and [localhost sub-domain](http://app.localhost.com:8081/test-case/embed.html)
48
+
34
49
  ### Publishing new version
35
50
 
36
51
  * Login with your *personal* credentials with `npm login`
@@ -38,8 +38,41 @@ function __spreadArray(to, from, pack) {
38
38
  return to.concat(ar || Array.prototype.slice.call(from));
39
39
  }
40
40
 
41
+ // Courtesy: https://stackoverflow.com/a/23945027
42
+ function extractHostname(url) {
43
+ var hostname;
44
+ //find & remove protocol (http, ftp, etc.) and get hostname
45
+ if (url.indexOf("//") > -1) {
46
+ hostname = url.split('/')[2];
47
+ }
48
+ else {
49
+ hostname = url.split('/')[0];
50
+ }
51
+ //find & remove port number
52
+ hostname = hostname.split(':')[0];
53
+ //find & remove "?"
54
+ hostname = hostname.split('?')[0];
55
+ return hostname;
56
+ }
57
+ // Warning: you can use this function to extract the "root" domain, but it will not be as accurate as using the psl package.
58
+ // https://www.npmjs.com/package/psl
59
+ var extractRootDomain = function (url) {
60
+ var domain = extractHostname(url), splitArr = domain.split('.'), arrLen = splitArr.length;
61
+ //extracting the root domain here
62
+ //if there is a subdomain
63
+ if (arrLen > 2) {
64
+ domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
65
+ //check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
66
+ if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
67
+ //this is using a ccTLD
68
+ domain = splitArr[arrLen - 3] + '.' + domain;
69
+ }
70
+ }
71
+ return domain;
72
+ };
41
73
  var getCookieDomain = function () {
42
- return location.hostname.replace('www.', '');
74
+ return ".".concat(extractRootDomain(location.hostname)); // .localhost
75
+ // return location.hostname.replace('www.', '');
43
76
  };
44
77
  var cookieParsingCache;
45
78
  var getCookies = function (useCache) {
@@ -1719,8 +1752,8 @@ class SessionIdManager {
1719
1752
 
1720
1753
  var VERSION_INFO = {
1721
1754
  env: 'production',
1722
- date: '2022-02-18T12:44:44.465Z',
1723
- version: '1.0.4'
1755
+ date: '2022-03-29T11:24:13.204Z',
1756
+ version: '1.0.5'
1724
1757
  };
1725
1758
  var USERMAVEN_VERSION = "".concat(VERSION_INFO.version, "/").concat(VERSION_INFO.env, "@").concat(VERSION_INFO.date);
1726
1759
  var beaconTransport = function (url, json) {
@@ -2090,10 +2123,13 @@ var UsermavenClientImpl = /** @class */ (function () {
2090
2123
  * @param options
2091
2124
  */
2092
2125
  UsermavenClientImpl.prototype.manageSession = function (options) {
2126
+ options = options || {};
2093
2127
  getLogger().debug('Options', options);
2128
+ // cross_subdomain_cookie: whether to keep cookie across domains and subdomains
2094
2129
  var defaultConfig = {
2095
- persistence: options ? options.persistence || 'cookie' : 'cookie',
2096
- persistence_name: options ? options.persistence_name || 'session' : 'session',
2130
+ persistence: options.persistence || 'cookie',
2131
+ persistence_name: options.persistence_name || 'session',
2132
+ cross_subdomain_cookie: options.cross_subdomain_cookie || true
2097
2133
  };
2098
2134
  // TODO: Default session name would be session_
2099
2135
  this.config = _.extend(defaultConfig, this.config || {}, {
@@ -203,6 +203,12 @@ export type UsermavenOptions = {
203
203
  */
204
204
  project_id?: string;
205
205
 
206
+ /**
207
+ * Enable cookie across subdomain
208
+ * Default value: true
209
+ */
210
+ cross_subdomain_cookie?: boolean;
211
+
206
212
  //NOTE: If any property is added here, please make sure it's added to browser.ts usermavenProps as well
207
213
 
208
214
  };
@@ -34,8 +34,41 @@ function __spreadArray(to, from, pack) {
34
34
  return to.concat(ar || Array.prototype.slice.call(from));
35
35
  }
36
36
 
37
+ // Courtesy: https://stackoverflow.com/a/23945027
38
+ function extractHostname(url) {
39
+ var hostname;
40
+ //find & remove protocol (http, ftp, etc.) and get hostname
41
+ if (url.indexOf("//") > -1) {
42
+ hostname = url.split('/')[2];
43
+ }
44
+ else {
45
+ hostname = url.split('/')[0];
46
+ }
47
+ //find & remove port number
48
+ hostname = hostname.split(':')[0];
49
+ //find & remove "?"
50
+ hostname = hostname.split('?')[0];
51
+ return hostname;
52
+ }
53
+ // Warning: you can use this function to extract the "root" domain, but it will not be as accurate as using the psl package.
54
+ // https://www.npmjs.com/package/psl
55
+ var extractRootDomain = function (url) {
56
+ var domain = extractHostname(url), splitArr = domain.split('.'), arrLen = splitArr.length;
57
+ //extracting the root domain here
58
+ //if there is a subdomain
59
+ if (arrLen > 2) {
60
+ domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
61
+ //check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
62
+ if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
63
+ //this is using a ccTLD
64
+ domain = splitArr[arrLen - 3] + '.' + domain;
65
+ }
66
+ }
67
+ return domain;
68
+ };
37
69
  var getCookieDomain = function () {
38
- return location.hostname.replace('www.', '');
70
+ return ".".concat(extractRootDomain(location.hostname)); // .localhost
71
+ // return location.hostname.replace('www.', '');
39
72
  };
40
73
  var cookieParsingCache;
41
74
  var getCookies = function (useCache) {
@@ -1715,8 +1748,8 @@ class SessionIdManager {
1715
1748
 
1716
1749
  var VERSION_INFO = {
1717
1750
  env: 'production',
1718
- date: '2022-02-18T12:44:44.465Z',
1719
- version: '1.0.4'
1751
+ date: '2022-03-29T11:24:13.204Z',
1752
+ version: '1.0.5'
1720
1753
  };
1721
1754
  var USERMAVEN_VERSION = "".concat(VERSION_INFO.version, "/").concat(VERSION_INFO.env, "@").concat(VERSION_INFO.date);
1722
1755
  var beaconTransport = function (url, json) {
@@ -2086,10 +2119,13 @@ var UsermavenClientImpl = /** @class */ (function () {
2086
2119
  * @param options
2087
2120
  */
2088
2121
  UsermavenClientImpl.prototype.manageSession = function (options) {
2122
+ options = options || {};
2089
2123
  getLogger().debug('Options', options);
2124
+ // cross_subdomain_cookie: whether to keep cookie across domains and subdomains
2090
2125
  var defaultConfig = {
2091
- persistence: options ? options.persistence || 'cookie' : 'cookie',
2092
- persistence_name: options ? options.persistence_name || 'session' : 'session',
2126
+ persistence: options.persistence || 'cookie',
2127
+ persistence_name: options.persistence_name || 'session',
2128
+ cross_subdomain_cookie: options.cross_subdomain_cookie || true
2093
2129
  };
2094
2130
  // TODO: Default session name would be session_
2095
2131
  this.config = _.extend(defaultConfig, this.config || {}, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usermaven/sdk-js",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Usermaven JavaScript SDK.",
5
5
  "main": "dist/npm/usermaven.cjs.js",
6
6
  "module": "dist/npm/dist/usermaven.esm.js",