rudder-sdk-js 2.14.0 → 2.15.0

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.d.ts +16 -0
  2. package/index.js +300 -5
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -89,6 +89,10 @@ declare module "rudder-sdk-js" {
89
89
  anonymousIdOptions?: anonymousIdOptions;
90
90
  // defaults to https://cdn.rudderlabs.com/v1.1/js-integrations
91
91
  destSDKBaseURL?: string;
92
+ sessions?: {
93
+ autoTrack?: boolean; // Defaults to true
94
+ timeout?: number; // Defaults to 30 mins
95
+ }
92
96
  }
93
97
 
94
98
  /**
@@ -452,6 +456,16 @@ declare module "rudder-sdk-js" {
452
456
  */
453
457
  function getGroupTraits(): apiObject;
454
458
 
459
+ /**
460
+ * To manually start user session in the SDK
461
+ */
462
+ function startSession(sessionId?: number): void;
463
+
464
+ /**
465
+ * To manually end user session in the SDK
466
+ */
467
+ function endSession(): void;
468
+
455
469
  export {
456
470
  integrationOptions,
457
471
  loadOptions,
@@ -474,5 +488,7 @@ declare module "rudder-sdk-js" {
474
488
  getUserTraits,
475
489
  getGroupId,
476
490
  getGroupTraits,
491
+ startSession,
492
+ endSession,
477
493
  };
478
494
  }
package/index.js CHANGED
@@ -5016,7 +5016,7 @@
5016
5016
 
5017
5017
  // Reserved Keywords for properties/traits
5018
5018
  var RESERVED_KEYS = ['anonymous_id', 'id', 'sent_at', 'received_at', 'timestamp', 'original_timestamp', 'event_text', 'event'];
5019
- var CONFIG_URL = 'https://api.rudderlabs.com/sourceConfig/?p=npm&v=2.14.0';
5019
+ var CONFIG_URL = 'https://api.rudderlabs.com/sourceConfig/?p=npm&v=2.15.0';
5020
5020
  var CDN_INT_DIR = 'js-integrations';
5021
5021
  var DEST_SDK_BASE_URL = "https://cdn.rudderlabs.com/v1.1/".concat(CDN_INT_DIR);
5022
5022
  var MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
@@ -5026,6 +5026,11 @@
5026
5026
  var DEFAULT_ERROR_REPORT_PROVIDER = 'bugsnag';
5027
5027
  var ERROR_REPORT_PROVIDERS = [DEFAULT_ERROR_REPORT_PROVIDER];
5028
5028
  var SAMESITE_COOKIE_OPTS = ['Lax', 'None', 'Strict'];
5029
+ var DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000; // 30 min in milliseconds
5030
+
5031
+ var MIN_SESSION_TIMEOUT = 10 * 1000; // 10 sec in milliseconds
5032
+
5033
+ var MIN_SESSION_ID_LENGTH = 10;
5029
5034
 
5030
5035
  var aes = {exports: {}};
5031
5036
 
@@ -9892,6 +9897,7 @@
9892
9897
  group_storage_trait: 'rl_group_trait',
9893
9898
  page_storage_init_referrer: 'rl_page_init_referrer',
9894
9899
  page_storage_init_referring_domain: 'rl_page_init_referring_domain',
9900
+ session_info: 'rl_session',
9895
9901
  prefix: 'RudderEncrypt:',
9896
9902
  key: 'Rudder'
9897
9903
  };
@@ -10088,6 +10094,16 @@
10088
10094
  value: function setInitialReferringDomain(value) {
10089
10095
  this.setItem(defaults$1.page_storage_init_referring_domain, value);
10090
10096
  }
10097
+ /**
10098
+ * Set session information
10099
+ * @param {*} value
10100
+ */
10101
+
10102
+ }, {
10103
+ key: "setSessionInfo",
10104
+ value: function setSessionInfo(value) {
10105
+ this.setItem(defaults$1.session_info, value);
10106
+ }
10091
10107
  /**
10092
10108
  *
10093
10109
  * @param {*} key
@@ -10250,6 +10266,15 @@
10250
10266
  value: function getInitialReferringDomain() {
10251
10267
  return this.getItem(defaults$1.page_storage_init_referring_domain);
10252
10268
  }
10269
+ /**
10270
+ * get the stored session info
10271
+ */
10272
+
10273
+ }, {
10274
+ key: "getSessionInfo",
10275
+ value: function getSessionInfo() {
10276
+ return this.getItem(defaults$1.session_info);
10277
+ }
10253
10278
  /**
10254
10279
  *
10255
10280
  * @param {*} key
@@ -10260,6 +10285,11 @@
10260
10285
  value: function removeItem(key) {
10261
10286
  return this.storage.remove(key);
10262
10287
  }
10288
+ }, {
10289
+ key: "removeSessionInfo",
10290
+ value: function removeSessionInfo() {
10291
+ this.removeItem(defaults$1.session_info);
10292
+ }
10263
10293
  /**
10264
10294
  * remove stored keys
10265
10295
  */
@@ -10661,6 +10691,10 @@
10661
10691
  };
10662
10692
  };
10663
10693
 
10694
+ var countDigits = function countDigits(number) {
10695
+ return number ? number.toString().length : 0;
10696
+ };
10697
+
10664
10698
  // Application class
10665
10699
  var RudderApp = /*#__PURE__*/_createClass(function RudderApp() {
10666
10700
  _classCallCheck(this, RudderApp);
@@ -10668,7 +10702,7 @@
10668
10702
  this.build = '1.0.0';
10669
10703
  this.name = 'RudderLabs JavaScript SDK';
10670
10704
  this.namespace = 'com.rudderlabs.javascript';
10671
- this.version = '2.14.0';
10705
+ this.version = '2.15.0';
10672
10706
  });
10673
10707
 
10674
10708
  /* eslint-disable max-classes-per-file */
@@ -10677,7 +10711,7 @@
10677
10711
  _classCallCheck(this, RudderLibraryInfo);
10678
10712
 
10679
10713
  this.name = 'RudderLabs JavaScript SDK';
10680
- this.version = '2.14.0';
10714
+ this.version = '2.15.0';
10681
10715
  }); // Operating System information class
10682
10716
 
10683
10717
 
@@ -13352,7 +13386,7 @@
13352
13386
  var devHosts = ['localhost', '127.0.0.1', '[::1]'];
13353
13387
  window.rsBugsnagClient = window.Bugsnag.start({
13354
13388
  apiKey: API_KEY,
13355
- appVersion: '2.14.0',
13389
+ appVersion: '2.15.0',
13356
13390
  // Set SDK version as the app version
13357
13391
  metadata: META_DATA,
13358
13392
  onError: function onError(event) {
@@ -13416,6 +13450,226 @@
13416
13450
  }
13417
13451
  };
13418
13452
 
13453
+ var UserSession = /*#__PURE__*/function () {
13454
+ function UserSession() {
13455
+ _classCallCheck(this, UserSession);
13456
+
13457
+ this.storage = Storage;
13458
+ this.timeout = DEFAULT_SESSION_TIMEOUT;
13459
+ this.sessionInfo = {
13460
+ autoTrack: true
13461
+ };
13462
+ }
13463
+ /**
13464
+ * A function to initialize session information
13465
+ * @param {object} options load call options
13466
+ */
13467
+
13468
+
13469
+ _createClass(UserSession, [{
13470
+ key: "initialize",
13471
+ value: function initialize(options) {
13472
+ try {
13473
+ var _options$sessions;
13474
+
13475
+ // Fetch session information from storage if any or initialize with an empty object
13476
+ this.sessionInfo = this.storage.getSessionInfo() || this.sessionInfo;
13477
+ /**
13478
+ * By default this.autoTrack will be true
13479
+ * Cases where this.autoTrack will be false:
13480
+ * 1. User explicitly set autoTrack load option to false
13481
+ * 2. When user is manually tracking the session
13482
+ *
13483
+ * Depending on the use case, this.autoTrack is set to true/false.
13484
+ */
13485
+
13486
+ this.sessionInfo.autoTrack = !((options === null || options === void 0 ? void 0 : (_options$sessions = options.sessions) === null || _options$sessions === void 0 ? void 0 : _options$sessions.autoTrack) === false || this.sessionInfo.manualTrack);
13487
+ /**
13488
+ * Validate "timeout" input. Should be provided in milliseconds.
13489
+ * Session timeout: By default, a session lasts until there's 30 minutes of inactivity,
13490
+ * but you can configure this limit using "timeout" load option
13491
+ */
13492
+
13493
+ if (options !== null && options !== void 0 && options.sessions && !isNaN(options.sessions.timeout)) {
13494
+ var timeout = options.sessions.timeout; // In case user provides 0 as the timeout, auto session tracking will be disabled
13495
+
13496
+ if (timeout === 0) {
13497
+ logger.warn('[Session]:: Provided timeout value 0 will disable the auto session tracking feature.');
13498
+ this.sessionInfo.autoTrack = false;
13499
+ } // In case user provides a setTimeout value greater than 0 but less than 10 seconds SDK will show a warning
13500
+ // and will proceed with it
13501
+
13502
+
13503
+ if (timeout > 0 && timeout < MIN_SESSION_TIMEOUT) {
13504
+ logger.warn('[Session]:: It is not advised to set "timeout" less than 10 seconds');
13505
+ }
13506
+
13507
+ this.timeout = timeout;
13508
+ } // If auto session tracking is enabled start the session tracking
13509
+
13510
+
13511
+ if (this.sessionInfo.autoTrack) {
13512
+ this.startAutoTracking();
13513
+ } else if (this.sessionInfo.autoTrack === false && !this.sessionInfo.manualTrack) {
13514
+ /**
13515
+ * Use case:
13516
+ * By default user session is enabled which means storage will have session data.
13517
+ * In case user wanted to opt out and set auto track to false through load option,
13518
+ * clear stored session info.
13519
+ */
13520
+ this.end();
13521
+ }
13522
+ } catch (e) {
13523
+ handleError(e);
13524
+ }
13525
+ }
13526
+ /**
13527
+ * A function to validate current session and return true/false depending on that
13528
+ * @param {number} timestamp
13529
+ * @returns boolean
13530
+ */
13531
+
13532
+ }, {
13533
+ key: "isValidSession",
13534
+ value: function isValidSession(timestamp) {
13535
+ return timestamp <= this.sessionInfo.expiresAt;
13536
+ }
13537
+ /**
13538
+ * A function to generate session id
13539
+ * @returns number
13540
+ */
13541
+
13542
+ }, {
13543
+ key: "generateSessionId",
13544
+ value: function generateSessionId() {
13545
+ return Date.now();
13546
+ }
13547
+ /**
13548
+ * A function to check for existing session details and depending on that create a new session.
13549
+ */
13550
+
13551
+ }, {
13552
+ key: "startAutoTracking",
13553
+ value: function startAutoTracking() {
13554
+ var timestamp = Date.now();
13555
+
13556
+ if (!this.isValidSession(timestamp)) {
13557
+ this.sessionInfo = {};
13558
+ this.sessionInfo.id = timestamp; // set the current timestamp
13559
+
13560
+ this.sessionInfo.expiresAt = timestamp + this.timeout; // set the expiry time of the session
13561
+
13562
+ this.sessionInfo.sessionStart = true;
13563
+ this.sessionInfo.autoTrack = true;
13564
+ }
13565
+
13566
+ this.storage.setSessionInfo(this.sessionInfo);
13567
+ }
13568
+ /**
13569
+ * Function to validate user provided sessionId
13570
+ * @param {number} sessionId
13571
+ * @returns
13572
+ */
13573
+
13574
+ }, {
13575
+ key: "validateSessionId",
13576
+ value: function validateSessionId(sessionId) {
13577
+ if (typeof sessionId !== 'number' || sessionId % 1 !== 0) {
13578
+ logger.error("[Session]:: \"sessionId\" should only be a positive integer");
13579
+ return;
13580
+ }
13581
+
13582
+ if (countDigits(sessionId) < MIN_SESSION_ID_LENGTH) {
13583
+ logger.error("[Session]:: \"sessionId\" should at least be \"".concat(MIN_SESSION_ID_LENGTH, "\" digits long"));
13584
+ return;
13585
+ }
13586
+
13587
+ return sessionId;
13588
+ }
13589
+ /**
13590
+ * A public method to start a session
13591
+ * @param {number} sessionId session identifier
13592
+ * @returns
13593
+ */
13594
+
13595
+ }, {
13596
+ key: "start",
13597
+ value: function start(id) {
13598
+ var sessionId = id ? this.validateSessionId(id) : this.generateSessionId();
13599
+ this.sessionInfo = {
13600
+ id: sessionId || this.generateSessionId(),
13601
+ sessionStart: true,
13602
+ manualTrack: true
13603
+ };
13604
+ this.storage.setSessionInfo(this.sessionInfo);
13605
+ }
13606
+ /**
13607
+ * A public method to end an ongoing session.
13608
+ */
13609
+
13610
+ }, {
13611
+ key: "end",
13612
+ value: function end() {
13613
+ this.sessionInfo = {};
13614
+ this.storage.removeSessionInfo();
13615
+ }
13616
+ /**
13617
+ * A function get ongoing sessionId.
13618
+ */
13619
+
13620
+ }, {
13621
+ key: "getSessionInfo",
13622
+ value: function getSessionInfo() {
13623
+ var session = {};
13624
+
13625
+ if (this.sessionInfo.autoTrack || this.sessionInfo.manualTrack) {
13626
+ // renew or create a new auto-tracking session
13627
+ if (this.sessionInfo.autoTrack) {
13628
+ var timestamp = Date.now();
13629
+
13630
+ if (!this.isValidSession(timestamp)) {
13631
+ this.startAutoTracking();
13632
+ } else {
13633
+ this.sessionInfo.expiresAt = timestamp + this.timeout; // set the expiry time of the session
13634
+ }
13635
+ }
13636
+
13637
+ if (this.sessionInfo.sessionStart) {
13638
+ session.sessionStart = true;
13639
+ this.sessionInfo.sessionStart = false;
13640
+ }
13641
+
13642
+ session.sessionId = this.sessionInfo.id;
13643
+ this.storage.setSessionInfo(this.sessionInfo);
13644
+ }
13645
+
13646
+ return session;
13647
+ }
13648
+ /**
13649
+ * Refresh session info on reset API call
13650
+ */
13651
+
13652
+ }, {
13653
+ key: "reset",
13654
+ value: function reset() {
13655
+ var _this$sessionInfo = this.sessionInfo,
13656
+ manualTrack = _this$sessionInfo.manualTrack,
13657
+ autoTrack = _this$sessionInfo.autoTrack;
13658
+
13659
+ if (autoTrack) {
13660
+ this.sessionInfo = {};
13661
+ this.startAutoTracking();
13662
+ } else if (manualTrack) {
13663
+ this.start();
13664
+ }
13665
+ }
13666
+ }]);
13667
+
13668
+ return UserSession;
13669
+ }();
13670
+
13671
+ var userSession = new UserSession();
13672
+
13419
13673
  /**
13420
13674
  * class responsible for handling core
13421
13675
  * event tracking functionalities
@@ -13455,6 +13709,7 @@
13455
13709
  this.logLevel = undefined; // flag to indicate client integrations` ready status
13456
13710
 
13457
13711
  this.clientIntegrationsReady = false;
13712
+ this.uSession = userSession;
13458
13713
  }
13459
13714
  /**
13460
13715
  * initialize the user after load config
@@ -14042,6 +14297,18 @@
14042
14297
  if (this.groupTraits) {
14043
14298
  rudderElement.message.traits = _objectSpread2({}, this.groupTraits);
14044
14299
  }
14300
+ } // If auto/manual session tracking is enabled sessionId will be sent in the context
14301
+
14302
+
14303
+ try {
14304
+ var _this$uSession$getSes = this.uSession.getSessionInfo(),
14305
+ sessionId = _this$uSession$getSes.sessionId,
14306
+ sessionStart = _this$uSession$getSes.sessionStart;
14307
+
14308
+ rudderElement.message.context.sessionId = sessionId;
14309
+ if (sessionStart) rudderElement.message.context.sessionStart = true;
14310
+ } catch (e) {
14311
+ handleError(e);
14045
14312
  }
14046
14313
 
14047
14314
  this.processOptionsParam(rudderElement, options); // logger.debug(JSON.stringify(rudderElement))
@@ -14220,6 +14487,7 @@
14220
14487
  this.userTraits = {};
14221
14488
  this.groupId = '';
14222
14489
  this.groupTraits = {};
14490
+ this.uSession.reset();
14223
14491
  this.storage.clear(flag);
14224
14492
  }
14225
14493
  }, {
@@ -14360,7 +14628,10 @@
14360
14628
 
14361
14629
  if (options && options.sendAdblockPageOptions && _typeof(options.sendAdblockPageOptions) === 'object') {
14362
14630
  this.sendAdblockPageOptions = options.sendAdblockPageOptions;
14363
- }
14631
+ } // Session initialization
14632
+
14633
+
14634
+ this.uSession.initialize(options);
14364
14635
 
14365
14636
  if (options && options.clientSuppliedCallbacks) {
14366
14637
  // convert to rudder recognized method names
@@ -14548,6 +14819,26 @@
14548
14819
  value: function sendSampleRequest() {
14549
14820
  ScriptLoader('ad-block', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js');
14550
14821
  }
14822
+ /**
14823
+ * A public method to start a session
14824
+ * @param {string} sessionId session identifier
14825
+ * @returns
14826
+ */
14827
+
14828
+ }, {
14829
+ key: "startSession",
14830
+ value: function startSession(sessionId) {
14831
+ this.uSession.start(sessionId);
14832
+ }
14833
+ /**
14834
+ * A public method to end an ongoing session.
14835
+ */
14836
+
14837
+ }, {
14838
+ key: "endSession",
14839
+ value: function endSession() {
14840
+ this.uSession.end();
14841
+ }
14551
14842
  }]);
14552
14843
 
14553
14844
  return Analytics;
@@ -14654,8 +14945,11 @@
14654
14945
  var setAnonymousId = instance.setAnonymousId.bind(instance);
14655
14946
  var getGroupId = instance.getGroupId.bind(instance);
14656
14947
  var getGroupTraits = instance.getGroupTraits.bind(instance);
14948
+ var startSession = instance.startSession.bind(instance);
14949
+ var endSession = instance.endSession.bind(instance);
14657
14950
 
14658
14951
  exports.alias = alias;
14952
+ exports.endSession = endSession;
14659
14953
  exports.getAnonymousId = getAnonymousId;
14660
14954
  exports.getGroupId = getGroupId;
14661
14955
  exports.getGroupTraits = getGroupTraits;
@@ -14669,6 +14963,7 @@
14669
14963
  exports.ready = ready;
14670
14964
  exports.reset = reset;
14671
14965
  exports.setAnonymousId = setAnonymousId;
14966
+ exports.startSession = startSession;
14672
14967
  exports.track = track;
14673
14968
 
14674
14969
  Object.defineProperty(exports, '__esModule', { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rudder-sdk-js",
3
- "version": "2.14.0",
3
+ "version": "2.15.0",
4
4
  "description": "RudderStack Javascript SDK",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",