rudder-sdk-js 2.3.8 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/index.d.ts +13 -1
  2. package/index.js +243 -23
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -49,6 +49,16 @@ declare module "rudder-sdk-js" {
49
49
  };
50
50
  }
51
51
 
52
+ /**
53
+ * Represents the options parameter for anonymousId
54
+ */
55
+ interface anonymousIdOptions {
56
+ autoCapture?: {
57
+ enabled?: boolean;
58
+ source?: string;
59
+ };
60
+ }
61
+
52
62
  /**
53
63
  * Represents the options parameter in the load API
54
64
  */
@@ -74,6 +84,7 @@ declare module "rudder-sdk-js" {
74
84
  useBeacon?: boolean; // Defaults to false
75
85
  beaconQueueOptions?: beaconQueueOptions;
76
86
  cookieConsentManager?: cookieConsentManager;
87
+ anonymousIdOptions?: anonymousIdOptions;
77
88
  }
78
89
 
79
90
  /**
@@ -399,7 +410,7 @@ declare module "rudder-sdk-js" {
399
410
  /**
400
411
  * To get anonymousId set in the SDK
401
412
  */
402
- function getAnonymousId(): string;
413
+ function getAnonymousId(options?: anonymousIdOptions): string;
403
414
 
404
415
  /**
405
416
  * To set anonymousId
@@ -429,6 +440,7 @@ declare module "rudder-sdk-js" {
429
440
  queueOptions,
430
441
  apiObject,
431
442
  apiCallback,
443
+ anonymousIdOptions,
432
444
  load,
433
445
  ready,
434
446
  reset,
package/index.js CHANGED
@@ -4371,6 +4371,126 @@
4371
4371
  }
4372
4372
  })(componentUrl);
4373
4373
 
4374
+ var isobject = function isObject(val) {
4375
+ return val != null && _typeof(val) === 'object' && Array.isArray(val) === false;
4376
+ };
4377
+
4378
+ /*!
4379
+ * get-value <https://github.com/jonschlinkert/get-value>
4380
+ *
4381
+ * Copyright (c) 2014-2018, Jon Schlinkert.
4382
+ * Released under the MIT License.
4383
+ */
4384
+ var isObject$1 = isobject;
4385
+
4386
+ var getValue = function getValue(target, path, options) {
4387
+ if (!isObject$1(options)) {
4388
+ options = {
4389
+ default: options
4390
+ };
4391
+ }
4392
+
4393
+ if (!isValidObject(target)) {
4394
+ return typeof options.default !== 'undefined' ? options.default : target;
4395
+ }
4396
+
4397
+ if (typeof path === 'number') {
4398
+ path = String(path);
4399
+ }
4400
+
4401
+ var isArray = Array.isArray(path);
4402
+ var isString = typeof path === 'string';
4403
+ var splitChar = options.separator || '.';
4404
+ var joinChar = options.joinChar || (typeof splitChar === 'string' ? splitChar : '.');
4405
+
4406
+ if (!isString && !isArray) {
4407
+ return target;
4408
+ }
4409
+
4410
+ if (isString && path in target) {
4411
+ return isValid(path, target, options) ? target[path] : options.default;
4412
+ }
4413
+
4414
+ var segs = isArray ? path : split(path, splitChar, options);
4415
+ var len = segs.length;
4416
+ var idx = 0;
4417
+
4418
+ do {
4419
+ var prop = segs[idx];
4420
+
4421
+ if (typeof prop === 'number') {
4422
+ prop = String(prop);
4423
+ }
4424
+
4425
+ while (prop && prop.slice(-1) === '\\') {
4426
+ prop = join([prop.slice(0, -1), segs[++idx] || ''], joinChar, options);
4427
+ }
4428
+
4429
+ if (prop in target) {
4430
+ if (!isValid(prop, target, options)) {
4431
+ return options.default;
4432
+ }
4433
+
4434
+ target = target[prop];
4435
+ } else {
4436
+ var hasProp = false;
4437
+ var n = idx + 1;
4438
+
4439
+ while (n < len) {
4440
+ prop = join([prop, segs[n++]], joinChar, options);
4441
+
4442
+ if (hasProp = prop in target) {
4443
+ if (!isValid(prop, target, options)) {
4444
+ return options.default;
4445
+ }
4446
+
4447
+ target = target[prop];
4448
+ idx = n - 1;
4449
+ break;
4450
+ }
4451
+ }
4452
+
4453
+ if (!hasProp) {
4454
+ return options.default;
4455
+ }
4456
+ }
4457
+ } while (++idx < len && isValidObject(target));
4458
+
4459
+ if (idx === len) {
4460
+ return target;
4461
+ }
4462
+
4463
+ return options.default;
4464
+ };
4465
+
4466
+ function join(segs, joinChar, options) {
4467
+ if (typeof options.join === 'function') {
4468
+ return options.join(segs);
4469
+ }
4470
+
4471
+ return segs[0] + joinChar + segs[1];
4472
+ }
4473
+
4474
+ function split(path, splitChar, options) {
4475
+ if (typeof options.split === 'function') {
4476
+ return options.split(path);
4477
+ }
4478
+
4479
+ return path.split(splitChar);
4480
+ }
4481
+
4482
+ function isValid(key, target, options) {
4483
+ if (typeof options.isValid === 'function') {
4484
+ return options.isValid(key, target);
4485
+ }
4486
+
4487
+ return true;
4488
+ }
4489
+
4490
+ function isValidObject(val) {
4491
+ return isObject$1(val) || Array.isArray(val) || typeof val === 'function';
4492
+ }
4493
+
4374
4494
  var LOG_LEVEL_INFO = 1;
4375
4495
  var LOG_LEVEL_DEBUG = 2;
4376
4496
  var LOG_LEVEL_WARN = 3;
@@ -4709,7 +4829,7 @@
4709
4829
 
4710
4830
  // Reserved Keywords for properties/traits
4711
4831
  var ReservedPropertyKeywords = ["anonymous_id", "id", "sent_at", "received_at", "timestamp", "original_timestamp", "event_text", "event"]; // ECommerce Parameter Names Enumeration
4712
- var CONFIG_URL = "https://api.rudderlabs.com/sourceConfig/?p=npm&v=2.3.8";
4832
+ var CONFIG_URL = "https://api.rudderlabs.com/sourceConfig/?p=npm&v=2.4.0";
4713
4833
  var CDN_INT_DIR = "js-integrations";
4714
4834
  var DEST_SDK_BASE_URL = "https://cdn.rudderlabs.com/v1.1/".concat(CDN_INT_DIR);
4715
4835
  var MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
@@ -9230,6 +9350,7 @@
9230
9350
 
9231
9351
  this._options = {};
9232
9352
  this.options(options);
9353
+ this.isSupportAvailable = this.checkSupportAvailability();
9233
9354
  }
9234
9355
  /**
9235
9356
  *
@@ -9251,15 +9372,7 @@
9251
9372
  path: "/",
9252
9373
  domain: domain,
9253
9374
  samesite: "Lax"
9254
- }); // try setting a cookie first
9255
-
9256
- this.set("test_rudder", true);
9257
-
9258
- if (!this.get("test_rudder")) {
9259
- this._options.domain = null;
9260
- }
9261
-
9262
- this.remove("test_rudder");
9375
+ });
9263
9376
  }
9264
9377
  /**
9265
9378
  *
@@ -9303,6 +9416,24 @@
9303
9416
  return false;
9304
9417
  }
9305
9418
  }
9419
+ /**
9420
+ * Function to check cookie support exists or not
9421
+ * @returns boolean
9422
+ */
9423
+
9424
+ }, {
9425
+ key: "checkSupportAvailability",
9426
+ value: function checkSupportAvailability() {
9427
+ var name = "test_rudder_cookie";
9428
+ this.set(name, true);
9429
+
9430
+ if (this.get(name)) {
9431
+ this.remove(name);
9432
+ return true;
9433
+ }
9434
+
9435
+ return false;
9436
+ }
9306
9437
  }]);
9307
9438
 
9308
9439
  return CookieLocal;
@@ -10647,6 +10778,9 @@
10647
10778
  prefix: "RudderEncrypt:",
10648
10779
  key: "Rudder"
10649
10780
  };
10781
+ var anonymousIdKeyMap = {
10782
+ segment: "ajs_anonymous_id"
10783
+ };
10650
10784
  /**
10651
10785
  * Json stringify the given value
10652
10786
  * @param {*} value
@@ -10720,10 +10854,7 @@
10720
10854
  _classCallCheck(this, Storage);
10721
10855
 
10722
10856
  // First try setting the storage to cookie else to localstorage
10723
- Cookie.set("rudder_cookies", true);
10724
-
10725
- if (Cookie.get("rudder_cookies")) {
10726
- Cookie.remove("rudder_cookies");
10857
+ if (Cookie.isSupportAvailable) {
10727
10858
  this.storage = Cookie;
10728
10859
  return;
10729
10860
  } // localStorage is enabled.
@@ -10886,14 +11017,103 @@
10886
11017
  value: function getGroupTraits() {
10887
11018
  return this.getItem(defaults$1.group_storage_trait);
10888
11019
  }
11020
+ /**
11021
+ * Function to fetch anonymousId from external source
11022
+ * @param {string} key source of the anonymousId
11023
+ * @returns string
11024
+ */
11025
+
11026
+ }, {
11027
+ key: "fetchExternalAnonymousId",
11028
+ value: function fetchExternalAnonymousId(source) {
11029
+ var anonId;
11030
+ var key = source.toLowerCase();
11031
+
11032
+ if (!Object.keys(anonymousIdKeyMap).includes(key)) {
11033
+ return anonId;
11034
+ }
11035
+
11036
+ switch (key) {
11037
+ case "segment":
11038
+ /**
11039
+ * First check the local storage for anonymousId
11040
+ * Ref: https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#identify
11041
+ */
11042
+ if (Store$2.enabled) {
11043
+ anonId = Store$2.get(anonymousIdKeyMap[key]);
11044
+ } // If anonymousId is not present in local storage and check cookie support exists
11045
+ // fetch it from cookie
11046
+
11047
+
11048
+ if (!anonId && Cookie.IsCookieSupported()) {
11049
+ anonId = Cookie.get(anonymousIdKeyMap[key]);
11050
+ }
11051
+
11052
+ return anonId;
11053
+
11054
+ default:
11055
+ return anonId;
11056
+ }
11057
+ }
10889
11058
  /**
10890
11059
  * get stored anonymous id
11060
+ *
11061
+ * Use cases:
11062
+ * 1. getAnonymousId() -> anonymousIdOptions is undefined this function will return rl_anonymous_id
11063
+ * if present otherwise undefined
11064
+ *
11065
+ * 2. getAnonymousId(anonymousIdOptions) -> In case anonymousIdOptions is present this function will check
11066
+ * if rl_anonymous_id is present then it will return that
11067
+ *
11068
+ * otherwise it will validate the anonymousIdOptions and try to fetch the anonymous Id from the provided source.
11069
+ * Finally if no anonymous Id is present in the source it will return undefined.
11070
+ *
11071
+ * anonymousIdOptions example:
11072
+ * {
11073
+ autoCapture: {
11074
+ enabled: true,
11075
+ source: "segment",
11076
+ },
11077
+ }
11078
+ *
10891
11079
  */
10892
11080
 
10893
11081
  }, {
10894
11082
  key: "getAnonymousId",
10895
- value: function getAnonymousId() {
10896
- return this.getItem(defaults$1.user_storage_anonymousId);
11083
+ value: function getAnonymousId(anonymousIdOptions) {
11084
+ // fetch the rl_anonymous_id from storage
11085
+ var rlAnonymousId = parse(decryptValue(this.storage.get(defaults$1.user_storage_anonymousId)));
11086
+ /**
11087
+ * If RS's anonymous ID is available, return from here.
11088
+ *
11089
+ * The user, while migrating from a different analytics SDK,
11090
+ * will only need to auto-capture the anonymous ID when the RS SDK
11091
+ * loads for the first time.
11092
+ *
11093
+ * The captured anonymous ID would be available in RS's persistent storage
11094
+ * for all the subsequent SDK runs.
11095
+ * So, instead of always grabbing the ID from the migration source when
11096
+ * the options are specified, it is first checked in the RS's persistent storage.
11097
+ *
11098
+ * Moreover, the user can also clear the anonymous ID from the storage via
11099
+ * the 'reset' API, which renders the migration source's data useless.
11100
+ */
11101
+
11102
+ if (rlAnonymousId) {
11103
+ return rlAnonymousId;
11104
+ } // validate the provided anonymousIdOptions argument
11105
+
11106
+
11107
+ var source = getValue(anonymousIdOptions, "autoCapture.source");
11108
+
11109
+ if (getValue(anonymousIdOptions, "autoCapture.enabled") === true && typeof source === "string") {
11110
+ // fetch the anonymousId from the external source
11111
+ // ex - segment
11112
+ var anonId = this.fetchExternalAnonymousId(source);
11113
+ if (anonId) return anonId; // return anonymousId if present
11114
+ }
11115
+
11116
+ return rlAnonymousId; // return undefined
10897
11117
  }
10898
11118
  /**
10899
11119
  * get stored initial referrer
@@ -11306,7 +11526,7 @@
11306
11526
  this.build = "1.0.0";
11307
11527
  this.name = "RudderLabs JavaScript SDK";
11308
11528
  this.namespace = "com.rudderlabs.javascript";
11309
- this.version = "2.3.8";
11529
+ this.version = "2.4.0";
11310
11530
  });
11311
11531
 
11312
11532
  /* eslint-disable max-classes-per-file */
@@ -11315,7 +11535,7 @@
11315
11535
  _classCallCheck(this, RudderLibraryInfo);
11316
11536
 
11317
11537
  this.name = "RudderLabs JavaScript SDK";
11318
- this.version = "2.3.8";
11538
+ this.version = "2.4.0";
11319
11539
  }); // Operating System information class
11320
11540
 
11321
11541
 
@@ -13594,7 +13814,7 @@
13594
13814
 
13595
13815
  _createClass(Analytics, [{
13596
13816
  key: "initializeUser",
13597
- value: function initializeUser() {
13817
+ value: function initializeUser(anonymousIdOptions) {
13598
13818
  // save once for storing older values to encrypted
13599
13819
  this.userId = this.storage.getUserId() || "";
13600
13820
  this.storage.setUserId(this.userId);
@@ -13604,7 +13824,7 @@
13604
13824
  this.storage.setGroupId(this.groupId);
13605
13825
  this.groupTraits = this.storage.getGroupTraits() || {};
13606
13826
  this.storage.setGroupTraits(this.groupTraits);
13607
- this.anonymousId = this.getAnonymousId();
13827
+ this.anonymousId = this.getAnonymousId(anonymousIdOptions);
13608
13828
  this.storage.setAnonymousId(this.anonymousId);
13609
13829
  }
13610
13830
  }, {
@@ -14282,9 +14502,9 @@
14282
14502
  }
14283
14503
  }, {
14284
14504
  key: "getAnonymousId",
14285
- value: function getAnonymousId() {
14505
+ value: function getAnonymousId(anonymousIdOptions) {
14286
14506
  // if (!this.loaded) return;
14287
- this.anonymousId = this.storage.getAnonymousId();
14507
+ this.anonymousId = this.storage.getAnonymousId(anonymousIdOptions);
14288
14508
 
14289
14509
  if (!this.anonymousId) {
14290
14510
  this.setAnonymousId();
@@ -14423,7 +14643,7 @@
14423
14643
  }
14424
14644
 
14425
14645
  this.eventRepository.initialize(writeKey, serverUrl, options);
14426
- this.initializeUser();
14646
+ this.initializeUser(options ? options.anonymousIdOptions : undefined);
14427
14647
  this.setInitialPageProperties();
14428
14648
  this.loaded = true;
14429
14649
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rudder-sdk-js",
3
- "version": "2.3.8",
3
+ "version": "2.4.0",
4
4
  "description": "RudderStack Javascript SDK",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",