rudder-sdk-js 1.2.13 → 1.2.19

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 +23 -0
  2. package/index.js +506 -141
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -29,6 +29,26 @@ declare module "rudder-sdk-js" {
29
29
  maxItems?: number;
30
30
  }
31
31
 
32
+ /**
33
+ * Represents the beacon queue options parameter in loadOptions type
34
+ */
35
+ interface beaconQueueOptions {
36
+ // Maximum number of events in storage
37
+ maxItems?: number;
38
+ // Time in milliseconds to flush the queue autometically
39
+ flushQueueInterval?: number;
40
+ }
41
+
42
+ /**
43
+ * Represents the beacon queue options parameter in loadOptions type
44
+ */
45
+ interface cookieConsentManager {
46
+ // OneTrust
47
+ oneTrust?: {
48
+ enabled: boolean;
49
+ };
50
+ }
51
+
32
52
  /**
33
53
  * Represents the options parameter in the load API
34
54
  */
@@ -51,6 +71,9 @@ declare module "rudder-sdk-js" {
51
71
  sendAdblockPage?: boolean;
52
72
  sendAdblockPageOptions?: apiOptions;
53
73
  clientSuppliedCallbacks?: { string: () => void };
74
+ useBeacon?: boolean; // Defaults to false
75
+ beaconQueueOptions?: beaconQueueOptions;
76
+ cookieConsentManager?: cookieConsentManager;
54
77
  }
55
78
 
56
79
  /**
package/index.js CHANGED
@@ -5464,7 +5464,12 @@
5464
5464
  "Post Affiliate Pro": "POST_AFFILIATE_PRO",
5465
5465
  postaffiliatepro: "POST_AFFILIATE_PRO",
5466
5466
  POSTAFFILIATEPRO: "POST_AFFILIATE_PRO",
5467
- POST_AFFILIATE_PRO: "POST_AFFILIATE_PRO"
5467
+ POST_AFFILIATE_PRO: "POST_AFFILIATE_PRO",
5468
+ LaunchDarkly: "LAUNCHDARKLY",
5469
+ Launch_Darkly: "LAUNCHDARKLY",
5470
+ LAUNCHDARKLY: "LAUNCHDARKLY",
5471
+ "Launch Darkly": "LAUNCHDARKLY",
5472
+ launchDarkly: "LAUNCHDARKLY"
5468
5473
  };
5469
5474
 
5470
5475
  // from client native integration name to server identified display name
@@ -5511,7 +5516,8 @@
5511
5516
  QUALTRICS: "Qualtrics",
5512
5517
  SENTRY: "Sentry",
5513
5518
  GOOGLE_OPTIMIZE: "GoogleOptimize",
5514
- POST_AFFILIATE_PRO: "PostAffiliatePro"
5519
+ POST_AFFILIATE_PRO: "PostAffiliatePro",
5520
+ LAUNCHDARKLY: "LaunchDarkly"
5515
5521
  };
5516
5522
 
5517
5523
  // Reserved Keywords for properties/triats
@@ -5555,7 +5561,7 @@
5555
5561
  PRODUCT_REVIEWED: "Product Reviewed"
5556
5562
  }; // Enumeration for integrations supported
5557
5563
 
5558
- var CONFIG_URL = "https://api.rudderlabs.com/sourceConfig/?p=npm&v=1.2.13";
5564
+ var CONFIG_URL = "https://api.rudderlabs.com/sourceConfig/?p=npm&v=1.2.19";
5559
5565
  var MAX_WAIT_FOR_INTEGRATION_LOAD = 10000;
5560
5566
  var INTEGRATION_LOAD_CHECK_INTERVAL = 1000;
5561
5567
  /* module.exports = {
@@ -11507,6 +11513,10 @@
11507
11513
  if (Store.enabled) {
11508
11514
  this.storage = Store;
11509
11515
  }
11516
+
11517
+ if (!this.storage) {
11518
+ throw Error("Could not initialize the SDK :: no storage is available");
11519
+ }
11510
11520
  }
11511
11521
 
11512
11522
  _createClass(Storage, [{
@@ -29481,8 +29491,7 @@
29481
29491
  function Hotjar(config) {
29482
29492
  _classCallCheck(this, Hotjar);
29483
29493
 
29484
- this.siteId = config.siteID; // 1549611
29485
-
29494
+ this.siteId = config.siteID;
29486
29495
  this.name = "HOTJAR";
29487
29496
  this._ready = false;
29488
29497
  }
@@ -29490,6 +29499,7 @@
29490
29499
  _createClass(Hotjar, [{
29491
29500
  key: "init",
29492
29501
  value: function init() {
29502
+ logger.debug("===In init Hotjar===");
29493
29503
  window.hotjarSiteId = this.siteId;
29494
29504
 
29495
29505
  (function (h, o, t, j, a, r) {
@@ -29509,11 +29519,11 @@
29509
29519
  })(window, document, "https://static.hotjar.com/c/hotjar-", ".js?sv=");
29510
29520
 
29511
29521
  this._ready = true;
29512
- logger.debug("===in init Hotjar===");
29513
29522
  }
29514
29523
  }, {
29515
29524
  key: "identify",
29516
29525
  value: function identify(rudderElement) {
29526
+ logger.debug("===In Hotjar identify===");
29517
29527
  var userId = rudderElement.message.userId || rudderElement.message.anonymousId;
29518
29528
 
29519
29529
  if (!userId) {
@@ -29527,21 +29537,34 @@
29527
29537
  }, {
29528
29538
  key: "track",
29529
29539
  value: function track(rudderElement) {
29530
- logger.debug("[Hotjar] track:: method not supported");
29540
+ logger.debug("===In Hotjar track===");
29541
+ var event = rudderElement.message.event;
29542
+
29543
+ if (!event) {
29544
+ logger.error("Event name not present");
29545
+ return;
29546
+ } // event name must not exceed 750 characters and can only contain alphanumeric, underscores, and dashes.
29547
+ // Ref - https://help.hotjar.com/hc/en-us/articles/4405109971095#the-events-api-call
29548
+
29549
+
29550
+ window.hj("event", event.replace(/\s\s+/g, " ").substring(0, 750).replaceAll(" ", "_"));
29531
29551
  }
29532
29552
  }, {
29533
29553
  key: "page",
29534
- value: function page(rudderElement) {
29554
+ value: function page() {
29555
+ logger.debug("===In Hotjar page===");
29535
29556
  logger.debug("[Hotjar] page:: method not supported");
29536
29557
  }
29537
29558
  }, {
29538
29559
  key: "isLoaded",
29539
29560
  value: function isLoaded() {
29561
+ logger.debug("===In isLoaded Hotjar===");
29540
29562
  return this._ready;
29541
29563
  }
29542
29564
  }, {
29543
29565
  key: "isReady",
29544
29566
  value: function isReady() {
29567
+ logger.debug("===In isReady Hotjar===");
29545
29568
  return this._ready;
29546
29569
  }
29547
29570
  }]);
@@ -32667,10 +32690,11 @@
32667
32690
  this.disableCookie = config.disableCookie || false;
32668
32691
  this.propertyBlackList = [];
32669
32692
  this.xhrHeaders = {};
32693
+ this.enableLocalStoragePersistence = config.enableLocalStoragePersistence;
32670
32694
 
32671
32695
  if (config.xhrHeaders && config.xhrHeaders.length > 0) {
32672
32696
  config.xhrHeaders.forEach(function (header) {
32673
- if (header && header.key && header.value && header.key.trim() != "" && header.value.trim() != "") {
32697
+ if (header && header.key && header.value && header.key.trim() !== "" && header.value.trim() !== "") {
32674
32698
  _this.xhrHeaders[header.key] = header.value;
32675
32699
  }
32676
32700
  });
@@ -32678,7 +32702,7 @@
32678
32702
 
32679
32703
  if (config.propertyBlackList && config.propertyBlackList.length > 0) {
32680
32704
  config.propertyBlackList.forEach(function (element) {
32681
- if (element && element.property && element.property.trim() != "") {
32705
+ if (element && element.property && element.property.trim() !== "") {
32682
32706
  _this.propertyBlackList.push(element.property);
32683
32707
  }
32684
32708
  });
@@ -32726,6 +32750,10 @@
32726
32750
  configObject.xhr_headers = this.xhrHeaders;
32727
32751
  }
32728
32752
 
32753
+ if (this.enableLocalStoragePersistence) {
32754
+ configObject.persistence = "localStorage+cookie";
32755
+ }
32756
+
32729
32757
  posthog.init(this.teamApiKey, configObject);
32730
32758
  }
32731
32759
  /**
@@ -32755,7 +32783,7 @@
32755
32783
 
32756
32784
  if (unsetProperties && unsetProperties.length > 0) {
32757
32785
  unsetProperties.forEach(function (property) {
32758
- if (property && property.trim() != "") {
32786
+ if (property && property.trim() !== "") {
32759
32787
  posthog.unregister(property);
32760
32788
  }
32761
32789
  });
@@ -32787,7 +32815,7 @@
32787
32815
  posthog.capture(event, properties);
32788
32816
  }
32789
32817
  /**
32790
- *
32818
+ *
32791
32819
  *
32792
32820
  * @memberof Posthog
32793
32821
  */
@@ -34187,6 +34215,100 @@
34187
34215
  return PostAffiliatePro;
34188
34216
  }();
34189
34217
 
34218
+ var createUser = function createUser(message) {
34219
+ var user = {};
34220
+ user.key = message.userId || message.anonymousId;
34221
+ var traits = message.context.traits;
34222
+
34223
+ if (traits.anonymous !== undefined) {
34224
+ user.anonymous = traits.anonymous;
34225
+ }
34226
+
34227
+ if (traits.avatar !== undefined) user.avatar = traits.avatar;
34228
+ if (traits.country !== undefined) user.country = traits.country;
34229
+ if (traits.custom !== undefined) user.custom = traits.custom;
34230
+ if (traits.email !== undefined) user.email = traits.email;
34231
+ if (traits.firstName !== undefined) user.firstName = traits.firstName;
34232
+ if (traits.ip !== undefined) user.ip = traits.ip;
34233
+ if (traits.lastName !== undefined) user.lastName = traits.lastName;
34234
+ if (traits.name !== undefined) user.name = traits.name;
34235
+ if (traits.privateAttributeNames !== undefined) user.privateAttributeNames = traits.privateAttributeNames;
34236
+ if (traits.secondary !== undefined) user.secondary = traits.secondary;
34237
+ return user;
34238
+ };
34239
+
34240
+ var LaunchDarkly = /*#__PURE__*/function () {
34241
+ function LaunchDarkly(config) {
34242
+ _classCallCheck(this, LaunchDarkly);
34243
+
34244
+ this.name = "LaunchDarkly";
34245
+ this.clientSideId = config.clientSideId;
34246
+ }
34247
+
34248
+ _createClass(LaunchDarkly, [{
34249
+ key: "init",
34250
+ value: function init() {
34251
+ logger.debug("===in init LaunchDarkly===");
34252
+
34253
+ if (!this.clientSideId) {
34254
+ logger.error("".concat(this.name, " :: Unable to initialize destination - clientSideId is missing in config"));
34255
+ return;
34256
+ }
34257
+
34258
+ ScriptLoader(null, "https://unpkg.com/launchdarkly-js-client-sdk@2");
34259
+ }
34260
+ }, {
34261
+ key: "isLoaded",
34262
+ value: function isLoaded() {
34263
+ logger.debug("===In isLoaded LaunchDarkly===");
34264
+ return !!window.LDClient;
34265
+ }
34266
+ }, {
34267
+ key: "isReady",
34268
+ value: function isReady() {
34269
+ logger.debug("===In isReady LaunchDarkly===");
34270
+ return this.isLoaded();
34271
+ }
34272
+ }, {
34273
+ key: "identify",
34274
+ value: function identify(rudderElement) {
34275
+ var message = rudderElement.message;
34276
+ window.user = createUser(message);
34277
+
34278
+ if (window.ldclient) {
34279
+ window.ldclient.identify(window.user);
34280
+ } else {
34281
+ window.ldclient = window.LDClient.initialize(this.clientSideId, window.user);
34282
+ }
34283
+ }
34284
+ }, {
34285
+ key: "track",
34286
+ value: function track(rudderElement) {
34287
+ var _rudderElement$messag = rudderElement.message,
34288
+ event = _rudderElement$messag.event,
34289
+ properties = _rudderElement$messag.properties;
34290
+
34291
+ if (window.ldclient) {
34292
+ window.ldclient.track(event, properties);
34293
+ } else logger.error("=== In LaunchDarkly, track is not supported before identify ===");
34294
+ }
34295
+ }, {
34296
+ key: "alias",
34297
+ value: function alias(rudderElement) {
34298
+ var message = rudderElement.message;
34299
+ var newUser = {
34300
+ key: message.userId
34301
+ };
34302
+
34303
+ if (window.ldclient) {
34304
+ window.ldclient.alias(newUser, window.user);
34305
+ } else logger.error("=== In LaunchDarkly, alias is not supported before identify ===");
34306
+ }
34307
+ }]);
34308
+
34309
+ return LaunchDarkly;
34310
+ }();
34311
+
34190
34312
  // (config-plan name, native destination.name , exported integration name(this one below))
34191
34313
 
34192
34314
  var integrations = {
@@ -34233,7 +34355,8 @@
34233
34355
  TVSQUARED: TVSquared,
34234
34356
  VWO: VWO,
34235
34357
  GOOGLE_OPTIMIZE: GoogleOptimize,
34236
- POST_AFFILIATE_PRO: PostAffiliatePro
34358
+ POST_AFFILIATE_PRO: PostAffiliatePro,
34359
+ LAUNCHDARKLY: LaunchDarkly
34237
34360
  };
34238
34361
 
34239
34362
  // Application class
@@ -34243,7 +34366,7 @@
34243
34366
  this.build = "1.0.0";
34244
34367
  this.name = "RudderLabs JavaScript SDK";
34245
34368
  this.namespace = "com.rudderlabs.javascript";
34246
- this.version = "1.2.13";
34369
+ this.version = "1.2.19";
34247
34370
  });
34248
34371
 
34249
34372
  // Library information class
@@ -34251,7 +34374,7 @@
34251
34374
  _classCallCheck(this, RudderLibraryInfo);
34252
34375
 
34253
34376
  this.name = "RudderLabs JavaScript SDK";
34254
- this.version = "1.2.13";
34377
+ this.version = "1.2.19";
34255
34378
  }); // Operating System information class
34256
34379
 
34257
34380
 
@@ -35940,14 +36063,6 @@
35940
36063
 
35941
36064
  var lib$2 = Queue;
35942
36065
 
35943
- // Payload class, contains batch of Elements
35944
- var RudderPayload = /*#__PURE__*/_createClass(function RudderPayload() {
35945
- _classCallCheck(this, RudderPayload);
35946
-
35947
- this.batch = null;
35948
- this.writeKey = null;
35949
- });
35950
-
35951
36066
  var queueOptions = {
35952
36067
  maxRetryDelay: 360000,
35953
36068
  minRetryDelay: 1000,
@@ -35955,34 +36070,21 @@
35955
36070
  maxAttempts: 10,
35956
36071
  maxItems: 100
35957
36072
  };
35958
- var MESSAGE_LENGTH = 32 * 1000; // ~32 Kb
35959
-
35960
- /**
35961
- *
35962
- * @class EventRepository responsible for adding events into
35963
- * flush queue and sending data to rudder backend
35964
- * in batch and maintains order of the event.
35965
- */
35966
36073
 
35967
- var EventRepository = /*#__PURE__*/function () {
35968
- /**
35969
- *Creates an instance of EventRepository.
35970
- * @memberof EventRepository
35971
- */
35972
- function EventRepository(options) {
35973
- _classCallCheck(this, EventRepository);
36074
+ var XHRQueue = /*#__PURE__*/function () {
36075
+ function XHRQueue() {
36076
+ _classCallCheck(this, XHRQueue);
35974
36077
 
35975
- this.eventsBuffer = [];
35976
- this.writeKey = "";
35977
36078
  this.url = "";
35978
- this.state = "READY";
35979
- this.batchSize = 0; // previous implementation
35980
- // setInterval(this.preaparePayloadAndFlush, FLUSH_INTERVAL_DEFAULT, this);
36079
+ this.writeKey = "";
35981
36080
  }
35982
36081
 
35983
- _createClass(EventRepository, [{
35984
- key: "startQueue",
35985
- value: function startQueue(options) {
36082
+ _createClass(XHRQueue, [{
36083
+ key: "init",
36084
+ value: function init(writeKey, url, options) {
36085
+ this.url = url;
36086
+ this.writeKey = writeKey;
36087
+
35986
36088
  if (options) {
35987
36089
  // TODO: add checks for value - has to be +ve?
35988
36090
  _extends(queueOptions, options);
@@ -35991,79 +36093,20 @@
35991
36093
  this.payloadQueue = new lib$2("rudder", queueOptions, function (item, done) {
35992
36094
  // apply sentAt at flush time and reset on each retry
35993
36095
  item.message.sentAt = getCurrentTimeFormatted(); // send this item for processing, with a callback to enable queue to get the done status
36096
+ // eslint-disable-next-line no-use-before-define
35994
36097
 
35995
- eventRepository.processQueueElement(item.url, item.headers, item.message, 10 * 1000, function (err, res) {
36098
+ this.processQueueElement(item.url, item.headers, item.message, 10 * 1000, // eslint-disable-next-line consistent-return
36099
+ function (err, res) {
35996
36100
  if (err) {
35997
36101
  return done(err);
35998
36102
  }
35999
36103
 
36000
36104
  done(null, res);
36001
36105
  });
36002
- }); // start queue
36106
+ }.bind(this)); // start queue
36003
36107
 
36004
36108
  this.payloadQueue.start();
36005
36109
  }
36006
- /**
36007
- *
36008
- *
36009
- * @param {EventRepository} repo
36010
- * @returns
36011
- * @memberof EventRepository
36012
- */
36013
-
36014
- }, {
36015
- key: "preaparePayloadAndFlush",
36016
- value: function preaparePayloadAndFlush(repo) {
36017
- // construct payload
36018
- logger.debug("==== in preaparePayloadAndFlush with state: ".concat(repo.state));
36019
- logger.debug(repo.eventsBuffer);
36020
-
36021
- if (repo.eventsBuffer.length == 0 || repo.state === "PROCESSING") {
36022
- return;
36023
- }
36024
-
36025
- var eventsPayload = repo.eventsBuffer;
36026
- var payload = new RudderPayload();
36027
- payload.batch = eventsPayload;
36028
- payload.writeKey = repo.writeKey;
36029
- payload.sentAt = getCurrentTimeFormatted(); // add sentAt to individual events as well
36030
-
36031
- payload.batch.forEach(function (event) {
36032
- event.sentAt = payload.sentAt;
36033
- });
36034
- repo.batchSize = repo.eventsBuffer.length; // server-side integration, XHR is node module
36035
-
36036
- if (true) {
36037
- var xhr = new XMLHttpRequest();
36038
- } else {
36039
- var xhr;
36040
- }
36041
-
36042
- logger.debug("==== in flush sending to Rudder BE ====");
36043
- logger.debug(JSON.stringify(payload, replacer));
36044
- xhr.open("POST", repo.url, true);
36045
- xhr.setRequestHeader("Content-Type", "application/json");
36046
-
36047
- {
36048
- xhr.setRequestHeader("Authorization", "Basic ".concat(btoa("".concat(payload.writeKey, ":"))));
36049
- } // register call back to reset event buffer on successfull POST
36050
-
36051
-
36052
- xhr.onreadystatechange = function () {
36053
- if (xhr.readyState === 4 && xhr.status === 200) {
36054
- logger.debug("====== request processed successfully: ".concat(xhr.status));
36055
- repo.eventsBuffer = repo.eventsBuffer.slice(repo.batchSize);
36056
- logger.debug(repo.eventsBuffer.length);
36057
- } else if (xhr.readyState === 4 && xhr.status !== 200) {
36058
- handleError(new Error("request failed with status: ".concat(xhr.status, " for url: ").concat(repo.url)));
36059
- }
36060
-
36061
- repo.state = "READY";
36062
- };
36063
-
36064
- xhr.send(JSON.stringify(payload, replacer));
36065
- repo.state = "PROCESSING";
36066
- }
36067
36110
  /**
36068
36111
  * the queue item proceesor
36069
36112
  * @param {*} url to send requests to
@@ -36105,6 +36148,216 @@
36105
36148
  queueFn(error);
36106
36149
  }
36107
36150
  }
36151
+ }, {
36152
+ key: "enqueue",
36153
+ value: function enqueue(message, type) {
36154
+ var headers = {
36155
+ "Content-Type": "application/json",
36156
+ Authorization: "Basic ".concat(btoa("".concat(this.writeKey, ":"))),
36157
+ AnonymousId: btoa(message.anonymousId)
36158
+ }; // add items to the queue
36159
+
36160
+ this.payloadQueue.addItem({
36161
+ url: "".concat(this.url, "/v1/").concat(type),
36162
+ headers: headers,
36163
+ message: message
36164
+ });
36165
+ }
36166
+ }]);
36167
+
36168
+ return XHRQueue;
36169
+ }();
36170
+
36171
+ var defaults$3 = {
36172
+ queue: "queue",
36173
+ maxPayloadSize: 64 * 1000
36174
+ };
36175
+
36176
+ var BeaconQueue = /*#__PURE__*/function () {
36177
+ function BeaconQueue() {
36178
+ _classCallCheck(this, BeaconQueue);
36179
+
36180
+ this.storage = Store;
36181
+ this.maxItems = 10;
36182
+ this.flushQueueTimeOut = undefined;
36183
+ this.timeOutActive = false;
36184
+ this.flushQueueTimeOutInterval = 1000 * 60 * 10; // 10 mins
36185
+
36186
+ this.url = "";
36187
+ this.writekey = "";
36188
+ this.queueName = "".concat(defaults$3.queue, ".").concat(Date.now());
36189
+ }
36190
+
36191
+ _createClass(BeaconQueue, [{
36192
+ key: "sendQueueDataForBeacon",
36193
+ value: function sendQueueDataForBeacon() {
36194
+ this.sendDataFromQueueAndDestroyQueue();
36195
+ }
36196
+ }, {
36197
+ key: "init",
36198
+ value: function init(writekey, url, options) {
36199
+ this.url = url;
36200
+ this.writekey = writekey;
36201
+ if (options.maxItems) this.maxItems = options.maxItems;
36202
+ if (options.flushQueueInterval) this.flushQueueTimeOutInterval = options.flushQueueInterval;
36203
+ var sendQueueData = this.sendQueueDataForBeacon.bind(this);
36204
+ window.addEventListener("unload", sendQueueData);
36205
+ }
36206
+ }, {
36207
+ key: "getQueue",
36208
+ value: function getQueue() {
36209
+ return this.storage.get(this.queueName);
36210
+ }
36211
+ }, {
36212
+ key: "setQueue",
36213
+ value: function setQueue(value) {
36214
+ this.storage.set(this.queueName, value);
36215
+ }
36216
+ /**
36217
+ *
36218
+ * Utility method for excluding null and empty values in JSON
36219
+ * @param {*} _key
36220
+ * @param {*} value
36221
+ * @returns
36222
+ */
36223
+
36224
+ }, {
36225
+ key: "replacer",
36226
+ value: function replacer(_key, value) {
36227
+ if (value === null || value === undefined) {
36228
+ return undefined;
36229
+ }
36230
+
36231
+ return value;
36232
+ }
36233
+ }, {
36234
+ key: "enqueue",
36235
+ value: function enqueue(message) {
36236
+ var queue = this.getQueue() || [];
36237
+ queue = queue.slice(-(this.maxItems - 1));
36238
+ queue.push(message);
36239
+ var batch = queue.slice(0);
36240
+ var data = {
36241
+ batch: batch
36242
+ };
36243
+ var dataToSend = JSON.stringify(data, this.replacer);
36244
+
36245
+ if (dataToSend.length > defaults$3.maxPayloadSize) {
36246
+ batch = queue.slice(0, queue.length - 1);
36247
+ this.flushQueue(batch);
36248
+ queue = this.getQueue();
36249
+ queue.push(message);
36250
+ }
36251
+
36252
+ this.setQueue(queue);
36253
+ this.setTimer();
36254
+
36255
+ if (queue.length === this.maxItems) {
36256
+ this.flushQueue(batch);
36257
+ }
36258
+ }
36259
+ }, {
36260
+ key: "sendDataFromQueueAndDestroyQueue",
36261
+ value: function sendDataFromQueueAndDestroyQueue() {
36262
+ this.sendDataFromQueue();
36263
+ this.storage.remove(this.queueName);
36264
+ }
36265
+ }, {
36266
+ key: "sendDataFromQueue",
36267
+ value: function sendDataFromQueue() {
36268
+ var queue = this.getQueue();
36269
+
36270
+ if (queue && queue.length > 0) {
36271
+ var batch = queue.slice(0, queue.length);
36272
+ this.flushQueue(batch);
36273
+ }
36274
+ }
36275
+ }, {
36276
+ key: "flushQueue",
36277
+ value: function flushQueue(batch) {
36278
+ batch.map(function (event) {
36279
+ event.sentAt = new Date().toISOString();
36280
+ });
36281
+ var data = {
36282
+ batch: batch
36283
+ };
36284
+ var payload = JSON.stringify(data, this.replacer);
36285
+ var blob = new Blob([payload], {
36286
+ type: "application/json"
36287
+ });
36288
+ var isPushed = navigator.sendBeacon("".concat(this.url, "?writeKey=").concat(this.writekey), blob);
36289
+
36290
+ if (!isPushed) {
36291
+ logger.debug("Unable to send data");
36292
+ }
36293
+
36294
+ this.setQueue([]);
36295
+ this.clearTimer();
36296
+ }
36297
+ }, {
36298
+ key: "setTimer",
36299
+ value: function setTimer() {
36300
+ if (!this.timeOutActive) {
36301
+ this.flushQueueTimeOut = setTimeout(this.sendDataFromQueue.bind(this), this.flushQueueTimeOutInterval);
36302
+ this.timeOutActive = true;
36303
+ }
36304
+ }
36305
+ }, {
36306
+ key: "clearTimer",
36307
+ value: function clearTimer() {
36308
+ if (this.timeOutActive) {
36309
+ clearTimeout(this.flushQueueTimeOut);
36310
+ this.timeOutActive = false;
36311
+ }
36312
+ }
36313
+ }]);
36314
+
36315
+ return BeaconQueue;
36316
+ }();
36317
+
36318
+ var MESSAGE_LENGTH = 32 * 1000; // ~32 Kb
36319
+
36320
+ /**
36321
+ *
36322
+ * @class EventRepository responsible for adding events into
36323
+ * flush queue and sending data to rudder backend
36324
+ * in batch and maintains order of the event.
36325
+ */
36326
+
36327
+ var EventRepository = /*#__PURE__*/function () {
36328
+ /**
36329
+ *Creates an instance of EventRepository.
36330
+ * @memberof EventRepository
36331
+ */
36332
+ function EventRepository() {
36333
+ _classCallCheck(this, EventRepository);
36334
+
36335
+ this.queue = undefined;
36336
+ }
36337
+
36338
+ _createClass(EventRepository, [{
36339
+ key: "initialize",
36340
+ value: function initialize(writeKey, url, options) {
36341
+ var queueOptions = {};
36342
+ var targetUrl = url.slice(-1) === "/" ? url.slice(0, -1) : url;
36343
+
36344
+ if (options && options.useBeacon) {
36345
+ if (options && options.beaconQueueOptions && options.beaconQueueOptions != null && _typeof(options.beaconQueueOptions) === "object") {
36346
+ queueOptions = options.beaconQueueOptions;
36347
+ }
36348
+
36349
+ targetUrl = "".concat(targetUrl, "/beacon/v1/batch");
36350
+ this.queue = new BeaconQueue();
36351
+ } else {
36352
+ if (options && options.queueOptions && options.queueOptions != null && _typeof(options.queueOptions) === "object") {
36353
+ queueOptions = options.queueOptions;
36354
+ }
36355
+
36356
+ this.queue = new XHRQueue();
36357
+ }
36358
+
36359
+ this.queue.init(writeKey, targetUrl, queueOptions);
36360
+ }
36108
36361
  /**
36109
36362
  *
36110
36363
  *
@@ -36116,34 +36369,22 @@
36116
36369
  key: "enqueue",
36117
36370
  value: function enqueue(rudderElement, type) {
36118
36371
  var message = rudderElement.getElementContent();
36119
- var headers = {
36120
- "Content-Type": "application/json",
36121
- Authorization: "Basic ".concat(btoa("".concat(this.writeKey, ":"))),
36122
- AnonymousId: btoa(message.anonymousId)
36123
- };
36124
36372
  message.originalTimestamp = getCurrentTimeFormatted();
36125
36373
  message.sentAt = getCurrentTimeFormatted(); // add this, will get modified when actually being sent
36126
36374
  // check message size, if greater log an error
36127
36375
 
36128
36376
  if (JSON.stringify(message).length > MESSAGE_LENGTH) {
36129
36377
  logger.error("[EventRepository] enqueue:: message length greater 32 Kb ", message);
36130
- } // modify the url for event specific endpoints
36131
-
36132
-
36133
- var url = this.url.slice(-1) == "/" ? this.url.slice(0, -1) : this.url; // add items to the queue
36378
+ }
36134
36379
 
36135
- this.payloadQueue.addItem({
36136
- url: "".concat(url, "/v1/").concat(type),
36137
- headers: headers,
36138
- message: message
36139
- });
36380
+ this.queue.enqueue(message, type);
36140
36381
  }
36141
36382
  }]);
36142
36383
 
36143
36384
  return EventRepository;
36144
36385
  }();
36145
36386
 
36146
- var eventRepository = new EventRepository();
36387
+ var eventRepository = new EventRepository(); // eslint-disable-next-line import/prefer-default-export
36147
36388
 
36148
36389
  function addDomEventHandlers(rudderanalytics) {
36149
36390
  var handler = function handler(e) {
@@ -36770,6 +37011,130 @@
36770
37011
  return Math.floor(Date.now() / 60000);
36771
37012
  }
36772
37013
 
37014
+ /* eslint-disable class-methods-use-this */
37015
+
37016
+ var OneTrust = /*#__PURE__*/function () {
37017
+ function OneTrust(sourceConfig) {
37018
+ var _this = this;
37019
+
37020
+ _classCallCheck(this, OneTrust);
37021
+
37022
+ this.sourceConfig = sourceConfig; // If user does not load onetrust sdk before loading rudderstack sdk
37023
+ // we will not be filtering any of the destinations.
37024
+
37025
+ if (!window.OneTrust || !window.OnetrustActiveGroups) {
37026
+ throw new Error("OneTrust resources are not accessible. Thus all the destinations will be loaded");
37027
+ } // OneTrust Cookie Compliance populates a data layer object OnetrustActiveGroups with
37028
+ // the cookie categories that the user has consented to.
37029
+ // Eg: ',C0001,C0003,'
37030
+ // We split it and save it as an array.
37031
+
37032
+
37033
+ var userSetConsentGroupIds = window.OnetrustActiveGroups.split(","); // Ids user has consented
37034
+ // Get information about the cookie script - data includes, consent models, cookies in preference centre, etc.
37035
+ // We get the groups(cookie categorization), user has created in one trust account.
37036
+
37037
+ var oneTrustAllGroupsInfo = window.OneTrust.GetDomainData().Groups;
37038
+ this.userSetConsentGroupNames = []; // Get the names of the cookies consented by the user in the browser.
37039
+
37040
+ oneTrustAllGroupsInfo.forEach(function (group) {
37041
+ var CustomGroupId = group.CustomGroupId,
37042
+ GroupName = group.GroupName;
37043
+
37044
+ if (userSetConsentGroupIds.includes(CustomGroupId)) {
37045
+ _this.userSetConsentGroupNames.push(GroupName.toUpperCase().trim());
37046
+ }
37047
+ });
37048
+ }
37049
+
37050
+ _createClass(OneTrust, [{
37051
+ key: "isEnabled",
37052
+ value: function isEnabled(destConfig) {
37053
+ var _this2 = this;
37054
+
37055
+ try {
37056
+ /**
37057
+ * Structure of onetrust consent group destination config.
37058
+ *
37059
+ * "oneTrustConsentGroup": [
37060
+ {
37061
+ "oneTrustConsentGroup": "Performance Cookies"
37062
+ },
37063
+ {
37064
+ "oneTrustConsentGroup": "Functional Cookies"
37065
+ },
37066
+ {
37067
+ "oneTrustConsentGroup": ""
37068
+ }
37069
+ ]
37070
+ *
37071
+ */
37072
+ var oneTrustCookieCategories = destConfig.oneTrustCookieCategories; // mapping of the destination with the consent group name
37073
+ // If the destination do not have this mapping events will be sent.
37074
+
37075
+ if (!oneTrustCookieCategories) {
37076
+ return true;
37077
+ } // Change the structure of oneTrustConsentGroup as an array and filter values if empty string
37078
+ // Eg:
37079
+ // ["Performance Cookies", "Functional Cookies"]
37080
+
37081
+
37082
+ var oneTrustConsentGroupArr = oneTrustCookieCategories.map(function (c) {
37083
+ return c.oneTrustCookieCategory;
37084
+ }).filter(function (n) {
37085
+ return n;
37086
+ });
37087
+ var containsAllConsent = true; // Check if all the destination's mapped cookie categories are consented by the user in the browser.
37088
+
37089
+ containsAllConsent = oneTrustConsentGroupArr.every(function (element) {
37090
+ return _this2.userSetConsentGroupNames.includes(element.toUpperCase().trim());
37091
+ });
37092
+ return containsAllConsent;
37093
+ } catch (e) {
37094
+ logger.error("Error during onetrust cookie consent management ".concat(e));
37095
+ return true;
37096
+ }
37097
+ }
37098
+ }]);
37099
+
37100
+ return OneTrust;
37101
+ }();
37102
+
37103
+ var CookieConsentFactory = /*#__PURE__*/function () {
37104
+ function CookieConsentFactory() {
37105
+ _classCallCheck(this, CookieConsentFactory);
37106
+ }
37107
+
37108
+ _createClass(CookieConsentFactory, null, [{
37109
+ key: "initialize",
37110
+ value: function initialize(cookieConsentOptions) {
37111
+ var _cookieConsentOptions;
37112
+
37113
+ /**
37114
+ *
37115
+ * check which type of cookie consent manager needs to be called if enabled
37116
+ * for now we have only OneTrust.
37117
+ * But if new cookie consent manager options are implemented,
37118
+ * we need to make sure only one of them is enabled by the user in the
37119
+ * load options
37120
+ *
37121
+ */
37122
+ if (cookieConsentOptions !== null && cookieConsentOptions !== void 0 && (_cookieConsentOptions = cookieConsentOptions.oneTrust) !== null && _cookieConsentOptions !== void 0 && _cookieConsentOptions.enabled) {
37123
+ // This is P1. When we have an ui in source side to turn on/off of cookie consent
37124
+ // if (sourceConfig &&
37125
+ // sourceConfig.cookieConsentManager &&
37126
+ // sourceConfig.cookieConsentManager.oneTrust &&
37127
+ // sourceConfig.cookieConsentManager.oneTrustenabled) {
37128
+ return new OneTrust(); // }
37129
+ }
37130
+
37131
+ return null;
37132
+ }
37133
+ }]);
37134
+
37135
+ return CookieConsentFactory;
37136
+ }();
37137
+
36773
37138
  var queryDefaults = {
36774
37139
  trait: "ajs_trait_",
36775
37140
  prop: "ajs_prop_"
@@ -36829,6 +37194,7 @@
36829
37194
  };
36830
37195
  this.loaded = false;
36831
37196
  this.loadIntegration = true;
37197
+ this.cookieConsentOptions = {};
36832
37198
  }
36833
37199
  /**
36834
37200
  * initialize the user after load config
@@ -36900,10 +37266,21 @@
36900
37266
  }, this);
36901
37267
  logger.debug("this.clientIntegrations: ", this.clientIntegrations); // intersection of config-plane native sdk destinations with sdk load time destination list
36902
37268
 
36903
- this.clientIntegrations = findAllEnabledDestinations(this.loadOnlyIntegrations, this.clientIntegrations); // remove from the list which don't have support yet in SDK
37269
+ this.clientIntegrations = findAllEnabledDestinations(this.loadOnlyIntegrations, this.clientIntegrations);
37270
+ var cookieConsent; // Call the cookie consent factory to initialize and return the type of cookie
37271
+ // consent being set. For now we only support OneTrust.
37272
+
37273
+ try {
37274
+ cookieConsent = CookieConsentFactory.initialize(this.cookieConsentOptions);
37275
+ } catch (e) {
37276
+ logger.error(e);
37277
+ } // If cookie consent object is return we filter according to consents given by user
37278
+ // else we do not consider any filtering for cookie consent.
37279
+
36904
37280
 
36905
37281
  this.clientIntegrations = this.clientIntegrations.filter(function (intg) {
36906
- return integrations[intg.name] != undefined;
37282
+ return integrations[intg.name] != undefined && (!cookieConsent || // check if cookie consent object is present and then do filtering
37283
+ cookieConsent && cookieConsent.isEnabled(intg.config));
36907
37284
  });
36908
37285
  this.init(this.clientIntegrations);
36909
37286
  } catch (error) {
@@ -37567,6 +37944,7 @@
37567
37944
  var _this3 = this;
37568
37945
 
37569
37946
  logger.debug("inside load ");
37947
+ if (options && options.cookieConsentManager) this.cookieConsentOptions = lodash_clonedeep(options.cookieConsentManager);
37570
37948
  if (this.loaded) return;
37571
37949
  var configUrl = CONFIG_URL;
37572
37950
 
@@ -37595,9 +37973,7 @@
37595
37973
  });
37596
37974
  }
37597
37975
 
37598
- if (isNotEmpty(storageOptions)) {
37599
- this.storage.options(storageOptions);
37600
- }
37976
+ this.storage.options(storageOptions);
37601
37977
 
37602
37978
  if (options && options.integrations) {
37603
37979
  _extends(this.loadOnlyIntegrations, options.integrations);
@@ -37635,22 +38011,11 @@
37635
38011
  this.registerCallbacks(true);
37636
38012
  }
37637
38013
 
37638
- if (options && options.queueOptions && options.queueOptions != null && _typeof(options.queueOptions) == "object") {
37639
- this.eventRepository.startQueue(options.queueOptions);
37640
- } else {
37641
- this.eventRepository.startQueue({});
37642
- }
37643
-
37644
38014
  if (options && options.loadIntegration != undefined) {
37645
38015
  this.loadIntegration = !!options.loadIntegration;
37646
38016
  }
37647
38017
 
37648
- this.eventRepository.writeKey = writeKey;
37649
-
37650
- if (serverUrl) {
37651
- this.eventRepository.url = serverUrl;
37652
- }
37653
-
38018
+ this.eventRepository.initialize(writeKey, serverUrl, options);
37654
38019
  this.initializeUser();
37655
38020
  this.setInitialPageProperties();
37656
38021
  this.loaded = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rudder-sdk-js",
3
- "version": "1.2.13",
3
+ "version": "1.2.19",
4
4
  "description": "RudderStack Javascript SDK",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",