@usermaven/sdk-js 1.0.2 → 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-01-29T08:33:18.619Z',
1723
- version: '1.0.2'
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) {
@@ -1898,14 +1931,14 @@ var UsermavenClientImpl = /** @class */ (function () {
1898
1931
  UsermavenClientImpl.prototype.getCtx = function () {
1899
1932
  var now = new Date();
1900
1933
  var _a = this.sessionManager.getSessionAndWindowId(), sessionId = _a.sessionId, windowId = _a.windowId;
1901
- // extract account details from identity payload
1934
+ // extract company details from identity payload
1902
1935
  var user = __assign({ anonymous_id: this.anonymousId }, this.userProperties);
1903
- var account = user['account'] || {};
1904
- delete user['account'];
1936
+ var company = user['company'] || {};
1937
+ delete user['company'];
1905
1938
  var payload = __assign({ event_id: '', session_id: sessionId, window_id: windowId, user: user, ids: this._getIds(), user_agent: navigator.userAgent, utc_time: reformatDate(now.toISOString()), local_tz_offset: now.getTimezoneOffset(), referer: document.referrer, url: window.location.href, page_title: document.title, doc_path: document.location.pathname, doc_host: document.location.hostname, doc_search: window.location.search, screen_resolution: screen.width + 'x' + screen.height, vp_size: Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0) + 'x' + Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0), user_language: navigator.language, doc_encoding: document.characterSet }, getDataFromParams(parseQuery()));
1906
1939
  // id and name attributes will be checked on backend
1907
- if (Object.keys(account).length) {
1908
- payload['account'] = account;
1940
+ if (Object.keys(company).length) {
1941
+ payload['company'] = company;
1909
1942
  }
1910
1943
  return payload;
1911
1944
  };
@@ -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 || {}, {
@@ -27,7 +27,7 @@ export type UsermavenClient = {
27
27
  rawTrack: (payload: any) => void
28
28
 
29
29
  /**
30
- * Sets a user data including organization/account data
30
+ * Sets a user data including organization/company data
31
31
  * @param userData user data (as map id_type --> value, such as "email": "a@bcd.com"
32
32
  * @param doNotSendEvent if true (false by default), separate "id" event won't be sent to server
33
33
  * @return Promise, see _send3p documentation
@@ -198,15 +198,28 @@ export type UsermavenOptions = {
198
198
  */
199
199
  persistence_name?: string;
200
200
 
201
+ /**
202
+ * Persistent connection version
203
+ */
204
+ project_id?: string;
205
+
206
+ /**
207
+ * Enable cookie across subdomain
208
+ * Default value: true
209
+ */
210
+ cross_subdomain_cookie?: boolean;
211
+
201
212
  //NOTE: If any property is added here, please make sure it's added to browser.ts usermavenProps as well
213
+
202
214
  };
203
215
 
204
216
  /**
205
- * Account Attributes
217
+ * Company Attributes
206
218
  */
207
- export interface AccountProps {
208
- id: string; // Account ID
209
- name: string; // Account Name
219
+ export interface CompanyProps {
220
+ id: string; // Company ID
221
+ name: string; // Company Name
222
+ created_at: string; // Company creation date
210
223
  custom: any; // Optional attributes such as industry, website, employee count etc.
211
224
  }
212
225
 
@@ -218,7 +231,7 @@ export interface UserProps {
218
231
  id?: string //user id (non anonymous). If not set, first known id (from propName below) will be used
219
232
  email?: string //user id (non anonymous). If not set, first known id (from propName below) will be used
220
233
  [propName: string]: any //any other forms of ids
221
- account?: AccountProps
234
+ company?: CompanyProps
222
235
  }
223
236
 
224
237
  /**
@@ -247,7 +260,7 @@ export type Conversion = {
247
260
  export type EventCtx = {
248
261
  event_id: string //unique event id or empty string for generating id on the backend side
249
262
  user: UserProps //user properties
250
- account?: AccountProps //account properties
263
+ company?: CompanyProps //company properties
251
264
  ids?: ThirdpartyIds //user ids from external systems
252
265
  user_agent: string //user
253
266
  utc_time: string //current UTC time in ISO 8601
@@ -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-01-29T08:33:18.619Z',
1719
- version: '1.0.2'
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) {
@@ -1894,14 +1927,14 @@ var UsermavenClientImpl = /** @class */ (function () {
1894
1927
  UsermavenClientImpl.prototype.getCtx = function () {
1895
1928
  var now = new Date();
1896
1929
  var _a = this.sessionManager.getSessionAndWindowId(), sessionId = _a.sessionId, windowId = _a.windowId;
1897
- // extract account details from identity payload
1930
+ // extract company details from identity payload
1898
1931
  var user = __assign({ anonymous_id: this.anonymousId }, this.userProperties);
1899
- var account = user['account'] || {};
1900
- delete user['account'];
1932
+ var company = user['company'] || {};
1933
+ delete user['company'];
1901
1934
  var payload = __assign({ event_id: '', session_id: sessionId, window_id: windowId, user: user, ids: this._getIds(), user_agent: navigator.userAgent, utc_time: reformatDate(now.toISOString()), local_tz_offset: now.getTimezoneOffset(), referer: document.referrer, url: window.location.href, page_title: document.title, doc_path: document.location.pathname, doc_host: document.location.hostname, doc_search: window.location.search, screen_resolution: screen.width + 'x' + screen.height, vp_size: Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0) + 'x' + Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0), user_language: navigator.language, doc_encoding: document.characterSet }, getDataFromParams(parseQuery()));
1902
1935
  // id and name attributes will be checked on backend
1903
- if (Object.keys(account).length) {
1904
- payload['account'] = account;
1936
+ if (Object.keys(company).length) {
1937
+ payload['company'] = company;
1905
1938
  }
1906
1939
  return payload;
1907
1940
  };
@@ -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.2",
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",